Skip to content

JSON marshalling and omitempty for empty strings and bools #1332

Closed

Description

@kayrus

Certain gophercloud implementations omit empty strings and false bools.

Examples:

  1. invert in loadbalancer/v2/l7policies/requests.go . You can set true to invert, but cannot set false.
  2. Similar issue occurs for name and description fields, e.g. you can set non-empty description, but you cannot remove it.

ref: golang/go#13284
#1314

Activity

  1. jtopjian commented on Nov 30, 2018

    @jtopjian
    Contributor

    @kayrus If you want to use this issue as a global catch-all issue for this type of problem, that works for me.

    This is something we run into fairly frequently - it's usually an oversight in the initial modeling of the API call. But often the solution needs to be considered case-by-case.

    For most occurrences, we want to change the type to a pointer-type. As you mentioned in #1314, there is a concern about breaking compatibility, but that's OK. In general, we try not to make breaking changes, but if the change is for the long-term better, then break away. Right now, Gophercloud makes no guarantees about backwards compatibility for reasons like this.

    I wish it would be as simple as changing the type to a pointer-type, but there are edge cases where it won't work. I can't think of any off the top of my head, but they're around (thankfully not many).

    So the best course of action is to take each occurrence one-by-one and fully understand the implications of each value:

    • bool,omitempty: will never be able to send false
    • string,omitempty: will never be able to send ""
    • *bool,omitempty: will be able to send false, will be omitted if not set
    • *string,omitempty: will be able to send "", will be omitted if not set
    • *bool: will be able to send false, will send null if not set.
    • *string: will be able to send "", will send null if not set.

    The last examples are the trickiest to determine if they are needed. These are for cases when the API wants 3 values: set, not set, null. The Neutron Port API is notorious for this pattern.

    Please let me know if you have any questions :)

  2. kayrus commented on Nov 30, 2018

    @kayrus
    ContributorAuthor

    If backward compatibility could be ignored, then my idea is to update at least all UpdateOpts' string,omitempty to *string,omitempty. We should not touch CreateOpts, correct? It could be done within one PR.

    Then identify all the cases for *bool and walk through them one by one. Collect obvious green light fixes, create another PR.

    For the rest tricky bools, we can create a separate PR.

  3. jtopjian commented on Nov 30, 2018

    @jtopjian
    Contributor

    I think that's a safe plan. If you're able to update the acceptance tests to prove these changes are valid, that would be the best way to confirm the changes were correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions