You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Florian Krönert (Jira)" <ji...@apache.org> on 2022/07/13 10:27:00 UTC

[jira] [Created] (AVRO-3576) [C#] Apache.Avro.Tools / CodeGen Enum Bug

Florian Krönert created AVRO-3576:
-------------------------------------

             Summary: [C#] Apache.Avro.Tools / CodeGen Enum Bug
                 Key: AVRO-3576
                 URL: https://issues.apache.org/jira/browse/AVRO-3576
             Project: Apache Avro
          Issue Type: Bug
          Components: csharp
    Affects Versions: 1.11.0
            Reporter: Florian Krönert


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:

 
{code:java}
{
    "type": "record",
    "name": "TestEvent",
    "namespace": "de.test",
    "fields": [
        {
            "name": "data_op",
            "type": {
                "type": "enum",
                "name": "Operation",
                "namespace": "de.test",
                "symbols": [
                    "C",
                    "U",
                    "D",
                    "S"
                ]
            }
        }
    ]
} {code}
 

 

The issue now is with the Put method.
It will be generated like this:

 
{code:java}
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()");
    };
} {code}
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
{code:java}
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()");
    };
} {code}
And it started to work as expected



--
This message was sent by Atlassian Jira
(v8.20.10#820010)