Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-4094: [C#] Updating mapped namespaces referenced in types #3247

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Shawson
Copy link

@Shawson Shawson commented Nov 28, 2024

What is the purpose of the change

Fixing AVRO-4094 - The use of namespace mappings currently updates namespaces, but not types that reference the namespace in the type name. This PR resolves that.

Due to the complexity of the type attribute, being either simple strings, arrays or complex types- potentially even nested combinations of the above, I use newtonsoft to build the object and recursively walk properties as opposed to the regex approach used in the namespace replacement. This feels a far more maintainable approach given this level of potential complexity.

Verifying this change

This change added tests and can be verified as follows:

  • Introduced new test GenerateSchemaWithMultipleNamespaceMapping which demonstrates two entities in different namespaces which both have their own unique mappings.

Documentation

  • Does this pull request introduce a new feature? no
  • If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)

@github-actions github-actions bot added the C# label Nov 28, 2024
@Shawson Shawson marked this pull request as ready for review November 28, 2024 21:58
{
if (token is JObject obj)
{
if (obj.ContainsKey("type"))

Check notice

Code scanning / CodeQL

Inefficient use of ContainsKey Note

Inefficient use of 'ContainsKey' and
indexer
.
{
return mapping.Value;
}
else if (originalNamespace.StartsWith($"{mapping.Key}."))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This calls String.StartsWith(String), which uses StringComparison.CurrentCulture; but should instead use StringComparison.Ordinal.

With StringComparison.CurrentCulture, it is possible that originalNamespace.StartsWith($"{mapping.Key}.") returns true but originalNamespace.Length < $"{mapping.Key}.".Length, which would then cause the subsequent Substring call to throw. This would happen if mapping.Key includes characters that are ignored in linguistic comparisons.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point- I've updated this to use Ordinal comparison now

Comment on lines 1335 to 1339
// Recurse into all properties of the object
foreach (var property in obj.Properties())
{
UpdateNamespacesInJToken(property.Value, namespaceMapping);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will replace namespace-looking strings in "type" properties even within the "default" property of a field. Those should not be modified because they affect the meaning of the schema, rather than only the local code generation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a list of properties which shouldn't be considered for the namespace mapping such as default/ doc/ symbols and a few others- However I'd disagree on type - this is a property which could have a namespace and I've an example of this in the test I included as part of this PR

Changing string comparison in ReplaceNamespace to use Ordinal string comparison
Adding blacklist for unqualified props which we shouldn't be looking to do any namespace replaces on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants