Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4290

C# nullable option generates invalid code for non-required enum field with default value

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 0.9.3, 0.10.0
    • Fix Version/s: 0.11.0
    • Component/s: C# - Compiler
    • Labels:
      None
    • Environment:

      Windows

      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:

      struct Work {
        1: i32 num1 = 0,
        2: i32 num2,
        3: Operation op = Operation.ADD,
        4: optional string comment,
      }
      

      Then the generated code in Work.cs looks like this:

        public Operation? Op
        {
          get
          {
            return _op;
          }
          set
          {
            __isset.op = true;
            this._op = value;
          }
        }
      

      This code is invalid, because value is of type Operation?, the correct code should be:

        public Operation? Op
        {
          get
          {
            return _op;
          }
          set
          {
            __isset.op = value.HasValue;
            if (value.HasValue) this._op = value.Value;
          }
        }
      

      I believe the error is located in file t_csharp_generator.cc in function t_csharp_generator::generate_csharp_property:

            if (ttype->is_base_type()) {
              use_nullable = ((t_base_type*)ttype)->get_base() != t_base_type::TYPE_STRING;
            }
      

      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

            else if (ttype->is_enum()) {
              use_nullable = true;
            }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                jking3 James E. King III
                Reporter:
                dsandbrink Dirk Sandbrink
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: