You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Dirk Sandbrink (JIRA)" <ji...@apache.org> on 2017/08/21 19:26:00 UTC

[jira] [Updated] (THRIFT-4290) C# nullable option generates invalid code for non-required enum field with default value

     [ https://issues.apache.org/jira/browse/THRIFT-4290?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Dirk Sandbrink updated THRIFT-4290:
-----------------------------------
    Description: 
When generating C# code with the nullable option invalid setter code is generated for enum fields which have a default value (and thus still need an isset flag).

For example use the Work struct from tutorial.thrift and add a default value to the enum field:
{code:none}
struct Work {
  1: i32 num1 = 0,
  2: i32 num2,
  3: Operation op = Operation.ADD,
  4: optional string comment,
}
{code}

The the generated code in Work.cs looks like this:
{code:none}
  public Operation? Op
  {
    get
    {
      return _op;
    }
    set
    {
      __isset.op = true;
      this._op = value;
    }
  }
{code}

This code is invalid, because value is of type _Operation?_, the correct code should be:
{code:none}
  public Operation? Op
  {
    get
    {
      return _op;
    }
    set
    {
      __isset.num1 = value.HasValue;
      if (value.HasValue) this._num1 = value.Value;
    }
  }
{code}

I believe the error is located in file _t_csharp_generator.cc_ in function _t_csharp_generator::generate_csharp_property_:
{code:none}
      if (ttype->is_base_type()) {
        use_nullable = ((t_base_type*)ttype)->get_base() != t_base_type::TYPE_STRING;
      }
{code}
Here use_nullable is set to true for all base types other then string, but not for enums.
A quick fix might be to add
{code:none}
      else if (ttype->is_enum()) {
        use_nullable = true;
      }
{code}

  was:
When generating C# code with the nullable option invalid setter code is generated for enum fields which have a default value (and thus still need an isset flag).

For example use the Work struct from tutorial.thrift and add a default value to the enum field:
{code:thrift}
struct Work {
  1: i32 num1 = 0,
  2: i32 num2,
  3: Operation op = Operation.ADD,
  4: optional string comment,
}
{code}

The the generated code in Work.cs looks like this:
{code:csharp}
  public Operation? Op
  {
    get
    {
      return _op;
    }
    set
    {
      __isset.op = true;
      this._op = value;
    }
  }
{code}

This code is invalid, because value is of type _Operation?_, the correct code should be:
{code:csharp}
  public Operation? Op
  {
    get
    {
      return _op;
    }
    set
    {
      __isset.num1 = value.HasValue;
      if (value.HasValue) this._num1 = value.Value;
    }
  }
{code}

I believe the error is located in file _t_csharp_generator.cc_ in function _t_csharp_generator::generate_csharp_property_:
{code:cpp}
      if (ttype->is_base_type()) {
        use_nullable = ((t_base_type*)ttype)->get_base() != t_base_type::TYPE_STRING;
      }
{code}
Here use_nullable is set to true for all base types other then string, but not for enums.
A quick fix might be to add
{code:cpp}
      else if (ttype->is_enum()) {
        use_nullable = true;
      }
{code}


> C# nullable option generates invalid code for non-required enum field with default value
> ----------------------------------------------------------------------------------------
>
>                 Key: THRIFT-4290
>                 URL: https://issues.apache.org/jira/browse/THRIFT-4290
>             Project: Thrift
>          Issue Type: Bug
>          Components: C# - Compiler
>    Affects Versions: 0.9.3, 0.10.0
>         Environment: Windows
>            Reporter: Dirk Sandbrink
>            Priority: Minor
>
> When generating C# code with the nullable option invalid setter code is generated for enum fields which have a default value (and thus still need an isset flag).
> For example use the Work struct from tutorial.thrift and add a default value to the enum field:
> {code:none}
> struct Work {
>   1: i32 num1 = 0,
>   2: i32 num2,
>   3: Operation op = Operation.ADD,
>   4: optional string comment,
> }
> {code}
> The the generated code in Work.cs looks like this:
> {code:none}
>   public Operation? Op
>   {
>     get
>     {
>       return _op;
>     }
>     set
>     {
>       __isset.op = true;
>       this._op = value;
>     }
>   }
> {code}
> This code is invalid, because value is of type _Operation?_, the correct code should be:
> {code:none}
>   public Operation? Op
>   {
>     get
>     {
>       return _op;
>     }
>     set
>     {
>       __isset.num1 = value.HasValue;
>       if (value.HasValue) this._num1 = value.Value;
>     }
>   }
> {code}
> I believe the error is located in file _t_csharp_generator.cc_ in function _t_csharp_generator::generate_csharp_property_:
> {code:none}
>       if (ttype->is_base_type()) {
>         use_nullable = ((t_base_type*)ttype)->get_base() != t_base_type::TYPE_STRING;
>       }
> {code}
> Here use_nullable is set to true for all base types other then string, but not for enums.
> A quick fix might be to add
> {code:none}
>       else if (ttype->is_enum()) {
>         use_nullable = true;
>       }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)