Skip to content

Conversation

@DavidNeukamXXXLutz
Copy link

@DavidNeukamXXXLutz DavidNeukamXXXLutz commented Oct 13, 2025

What is the purpose of the change

This PR fixes the issue AVRO-4187

When using avrogen for generating C# models from a schemafile, the --namespace option doesn't behave as expected.
When running 

avrogen -s file.avsc . --namespace my.avro.ns:my.csharp.ns

the namespace within the SCHEMA property of the generated C# files is changed to my.csharp.ns, but should be left with the value my.avro.ns.

public partial class TestModel : ISpecificRecord
{
	public static Schema _SCHEMA = Avro.Schema.Parse("{\"type\":\"record\",\"name\":\"TestModel\",\"namespace\":\"my.avro.ns\",\"fields\":[{\"name\":\"e" +
			"ventType\",\"type\":{\"type\":\"enum\",\"name\":\"EventType\",\"namespace\":\"my.avro.ns\",\"sym" +
			"bols\":[\"CREATE\",\"UPDATE\",\"DELETE\"]}}]}");
	private my.csharp.ns.EventType _eventType;
           // More generated code
    }

Verifying this change

This change added tests and can be verified as follows:

  • Extended existing namespace tests to assert that the avro namespace is contained in the SCHEMA property
  • Extended existing namespace tests to assert that the csharp namespace is not contained in the SCHEMA property

Documentation

  • Does this pull request introduce a new feature? no

@github-actions github-actions bot added the C# label Oct 13, 2025
@DavidNeukamXXXLutz DavidNeukamXXXLutz force-pushed the avro-4187_fix_namespaces_in_schema_property_of_csharp_classes_with_namespace_flag branch from 87b4276 to f7fa681 Compare October 13, 2025 12:58
…namespaces

The  Avrogen generated "SCHEMA" properties contain csharp instead of
avro namespaces, leading to the serializer not finding the schema in
the schema registry when using --namespace

With this commit, the SCHEMA property again contains the avro
namespace in this case.
@DavidNeukamXXXLutz DavidNeukamXXXLutz force-pushed the avro-4187_fix_namespaces_in_schema_property_of_csharp_classes_with_namespace_flag branch from f7fa681 to f92d01c Compare October 13, 2025 13:04
@DavidNeukamXXXLutz DavidNeukamXXXLutz marked this pull request as ready for review October 13, 2025 13:34
@DavidNeukamXXXLutz
Copy link
Author

I identified an issue with this change.
Though being a correct fix, people could currently rely on the buggy behaviour of having the C# names in the SCHEMA files.
For them, this could be a breaking change.

Another issue is, that the Avro deserializer does not support mapping form Avro Namespaces to C# namespaces.
The issue is, that the ObjectCreator does not have an extension point to provide the (reverse) namespace mappings.
It searches for the types with the namespaces given in the Avro schema.
By a hacky solution, I could make this work by adding the types to typeCacheByName via reflection.

I checked the Java implementation and it looks like they also dont support namespace mapping.

I thend to come to the conclusion, that the avrogen should not have a --namespace flag as the other Avro tooling does not support namespace mappings - so we are depending on a bug in the code generation to be able to deserialize the types again (affecting the contract).

I would suggest deprecating the --namespace flag with that information.

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.

2 participants