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

The 'omitempty' tag should not be appended to optional fields that have a default value

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 0.13.0
    • Go - Compiler
    • None
    • Patch Available
    • Patch

    Description

      When an optional field has a default value, the field won't have a pointer type anymore but will still keep the `omitempty` tag.

      For example the following thrift file:

      struct Node {
          1: optional bool is_live = false
      }
      

      Will be generated as:

      type Node struct {
          IsLive bool `thrift:"is_live,1" db:"is_live" json:"is_live,omitempty"`
      }
      

      If you create an instance of this struct and set the value to `false`:

      node := Node{IsLive: false}

      And then json.Marshal `node`, the output will be:

      {}
      

      Where it must be:

      {is_live: false}
      

      This happens because Go Marshal will ignore `zero values` if there is an `omitempty` tag attached to it.

      Attachments

        Issue Links

          Activity

            People

              jking3 James E. King III
              bit2pixel Renan Cakirerk
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m