Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
1.11.0
-
None
-
None
Description
We used Apache.Avro.Tools for generating our schema from a message.
Let's suppose our message only contains an enum "data_op" with the values "C" / "U" / "D" / "S".
The schema generated looks like this:
{ "type": "record", "name": "TestEvent", "namespace": "de.test", "fields": [ { "name": "data_op", "type": { "type": "enum", "name": "Operation", "namespace": "de.test", "symbols": [ "C", "U", "D", "S" ] } } ] }
The issue now is with the Put method.
It will be generated like this:
public virtual void Put(int fieldPos, object fieldValue) { switch (fieldPos) { case 0: this.data_op = de.test.Operation.C; break; default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()"); }; }
As you can see, the generated Put method always uses the first value, which was not even defined as default.
It seems that the Put method is also called when parsing the object.
This leads to our deserialized objects always having "C" as operation, although the message contained a different value.
This seems to be a severe bug, as the parsed data is incorrect.
The code line causing this issue is possibly the following one: https://github.com/apache/avro/blob/6ef5709e6c53629b41890f9df3047554663d3727/lang/csharp/src/apache/main/CodeGen/CodeGen.cs#L812
In my opinion the default value should not be applied at all, as it is not defined as default.
In addition to that, even with it being the default value this would be a bug, as it should only be used if no value was passed. Right now it is always the default value.
I changed the generated code to
public virtual void Put(int fieldPos, object fieldValue) { switch (fieldPos) { case 0: this.data_op = (de.test.Operation) fieldValue; break; default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()"); }; }
And it started to work as expected