Thrift
  1. Thrift
  2. THRIFT-1499

Generated javascript code doesn't properly write optional falsy values

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 0.8
    • Fix Version/s: None
    • Component/s: JavaScript - Compiler
    • Labels:

      Description

      The generated javascript code doesn't write out "falsy" values, which seems to be incorrect behavior for optional fields. Falsy values such as the empty string or the number 0 should be explicitly set, so that they are read out on the other end.

      The current behavior results in serialized data where fields are left unset, when really they should have been set to 0 (or the empty string).

      For example, the generated code for a .write looks like this:

      
      DataRunFilter.prototype.write = function(output) {
        output.writeStructBegin('DataRunFilter');
        if (this.runType) {
          output.writeFieldBegin('runType', Thrift.Type.I32, 2);
          output.writeI32(this.runType);
          output.writeFieldEnd();
        }
        output.writeFieldStop();
        output.writeStructEnd();
        return;
      };
      
      

      If runType (an enum) happens to be 0, the field won't be set at all and readers of the data will think it wasn't specified.

      Since the constructor explicitly nulls out all fields, it should be perfectly okay to instead test for null explicitly (e.g. " if (this.runType === null) {" above).

        Issue Links

          Activity

          Hide
          Wade Simmons added a comment -

          This is a duplicate of THRIFT-1297

          Show
          Wade Simmons added a comment - This is a duplicate of THRIFT-1297

            People

            • Assignee:
              Henrique Mendonça
              Reporter:
              Darin Tay
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development