-
-
Notifications
You must be signed in to change notification settings - Fork 227
Remove optional generated class attributes set to None from serialized payload #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove optional generated class attributes set to None from serialized payload #205
Comments
None
from serialized payload
Speaking about this with @dtkav I think a solution for places where None/null is semantically meaningful might be to use a sentinel type like: optional_property: Optional[Union[Dict[Any, Any], NullValue]] This would allow the user to explicitly set |
I like the general idea, but I'd go in the opposite direction. class MyModel:
normal: str
nullable: Optional[str]
optional: Union[str, Unset] = UNSET
optional_nullable: Union[str, Unset, None] = UNSET
optional_with_default: Union[str, Unset] = "default" Not completely sure on how to do a type annotation for unset- if UNSET is an object I believe Unset needs to be the type of it, so we might have to have a dummy type? |
I think dataclasses captures this concept with Our use case for explicit null is pretty rare. It comes up for fields that are Most json REST APIs that I've worked with don't distinguish between Either way, it seems wise here to follow the patterns used by dataclasses/attrs and follow their interpretation of I agree the naming of Here's a related stackoverflow answer from hynek (author of attrs) that also confirms that Optional means |
My first thought was to use the attrs |
Accidentally closed this when merging out version to the fork. Leaving open here to evaluate if we want to port that design. |
Did some testing using |
Is your feature request related to a problem? Please describe.
Given a generated class like:
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: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 sendingNone
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.The text was updated successfully, but these errors were encountered: