You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Kalle Niemitalo (Jira)" <ji...@apache.org> on 2022/08/23 06:36:00 UTC

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

    [ https://issues.apache.org/jira/browse/AVRO-3576?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17583396#comment-17583396 ] 

Kalle Niemitalo commented on AVRO-3576:
---------------------------------------

I cannot reproduce this bug with version 1.11.0 or 1.11.1 of Apache.Avro.Tools. I copied the Avro schema from the description of this issue to a file "AVRO-3576.avsc" and executed on .NET SDK 6.0.303:

{code:none}
DOTNET_ROLL_FORWARD=Major dotnet avrogen -s  AVRO-3576.avsc .
{code}

In the generated de/test/TestEvent.cs file, the Put method was as follows:

{code:C#}
                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}

i.e. it stores the value of the {{fieldValue}} parameter, rather than a constant.

bq. 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]

No, that line affects the field definition rather than the Put method, i.e. it would add a default value here:

{code:C#}
                private de.test.Operation _data_op;
{code}

But not even that happened now, because no default value was defined in the schema.

[~DigitalFlow], can you show how to reproduce the bug?

> [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
>            Priority: Major
>
> 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)