[ https://issues.apache.org/jira/browse/AVRO-2883?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17563170#comment-17563170 ]
Kalle Niemitalo commented on AVRO-2883: --------------------------------------- This was fixed by making avrogen generate a new schema with the mapped namespaces and then use that for everything instead of the original. There are some potential problems with that: - Calling Schema.ToString() on the generated _SCHEMA property will use the mapped namespaces. I suspect this could cause [Confluent.SchemaRegistry.Serdes.SpecificSerializerImpl<T>.Serialize|https://github.com/confluentinc/confluent-kafka-dotnet/blob/0f44064fd7ff2513e5fb01230f0faab9f9715a64/src/Confluent.SchemaRegistry.Serdes.Avro/SpecificSerializerImpl.cs#L228] not to find the schema in the schema registry, if it is configured to neither register schemas automatically nor use the latest schema. I'm not sure whether their schema registry distinguishes between schemas using different namespaces. - The mapping was implemented using a regular expression on the JSON text. If a field of a record has a default value that has a "namespace" property, the default value can also get mapped even though it shouldn't. That seems unlikely to happen in practice, though. If the JSON encoder requested in AVRO-3274 were implemented, it would fortunately be unaffected by namespace mappings because [JSON Encoding|https://avro.apache.org/docs/1.11.0/spec.html#json_encoding] uses "the type's name" rather than the fullname. The Java implementation seems to [use the fullname|https://github.com/apache/avro/blob/80c04a32dcbc0165c4e7f15f79c83afc6937193a/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ValidatingGrammarGenerator.java#L104] though…? > Avrogen (csharp) namespace mapping missing for references > --------------------------------------------------------- > > Key: AVRO-2883 > URL: https://issues.apache.org/jira/browse/AVRO-2883 > Project: Apache Avro > Issue Type: Bug > Components: csharp > Reporter: Thomas Vigh Sørensen > Assignee: Zoltan Csizmadia > Priority: Major > Labels: pull-request-available > Fix For: 1.11.1, 1.12.0 > > Time Spent: 2h > Remaining Estimate: 0h > > When using *avrogen* for generating C# models from a schemafile, the > *--namespace* option doesn't behave as expected, if the model refers to other > types (currently only tested with enums). > When running > {code:java} > avrogen -s file.avsc . --namespace my.avro.ns:my.csharp.ns > {code} > the actual namespace of the generated C# files is changed to *my.csharp.ns*, > but any references between the models are not updated with the correct > namespace. > > Given a schemafile containing: > {code:json} > { > "type" : "record", > "name" : "TestModel", > "namespace" : "my.avro.ns", > "fields" : [ { > "name" : "eventType", > "type" : { > "type" : "enum", > "name" : "EventType", > "symbols" : [ "CREATE", "UPDATE", "DELETE" ] > } > } ] > } > {code} > And running > {code:java} > avrogen -s models.avsc . --namespace my.avro.ns:my.csharp.ns > {code} > > Will produce the following *EventType.cs* > {code:c#} > namespace my.csharp.ns > { > using System; > using System.Collections.Generic; > using System.Text; > using Avro; > using Avro.Specific; > > public enum EventType > { > CREATE, > UPDATE, > DELETE, > } > } > {code} > > and the following *TestModel.cs* > {code:c#} > namespace my.csharp.ns > { > using System; > using System.Collections.Generic; > using System.Text; > using Avro; > using Avro.Specific; > > 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.avro.ns.EventType _eventType; > // More generated code > } > } > {code} > Notice how the namespace is set correctly to *my.csharp.ns* in both files, > but the namespace of the referenced *EventType* in *TestModel.cs* is > incorrect. > > Example of expected output (*TestModel.cs*): > {code:c#} > namespace my.csharp.ns > { > using System; > using System.Collections.Generic; > using System.Text; > using Avro; > using Avro.Specific; > > 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 > } > } > {code} > > Code for reproducing the issue can be found at: > [https://github.com/FuKe/avrogen-namespace-bug] -- This message was sent by Atlassian Jira (v8.20.10#820010)