Skip to content

Remove optional generated class attributes set to None from serialized payload #205

@bowenwr

Description

@bowenwr

Is your feature request related to a problem? Please describe.

Given a generated class like:

class MyModel:
  required_property: str
  optional_property: Optional[Dict[Any, Any]]

When communicating with an API, the requests are getting rejected for certain optional properties in which we're sending None / null which causes 400 Bad Request like:

Invalid input at "optional_property": None is not of type 'object'

The idea being that the API is expecting if a field is present, it has a non-null value. Not sending optional_property solves the problem.

Describe the solution you'd like

I'd like to have an option to only serialize fields which have a value unless they are marked required, where it's assumed None/null would have semantic value. I'm suggesting a configuration option as I realize this behavior may break other cases where folks may be relying on always sending None now.

The result would be that the generated to_dict() method in the classes only adds the key/value if the key is required or is not None.

Describe alternatives you've considered

Currently we are extending the generated classes and overriding the to_dict() method to perform the desired behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions