OpenAPITools / OpenAPITools/openapi-generator
[BUG][Python] client's `from_dict` and `to_dict` set optional but non-nullable fields to `None`
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
The python client's from_dict sets optional but non-nullable fields to None.
My spec defines MyObject like this:
MyObject:
type: object
properties:
retentionTimeInDays:
type: integer
nullable: false
The retentionTimeInDays property is optional (not required) but not nullable. The expectation is that the property doesn't have to exist but if it does it cannot be null.
The generated Pydantic class's from_dict method however sets the retentionTimeInDays property to None if it's not present in the dict:
"retentionTimeInDays": obj.get("retentionTimeInDays")
It behaves the same way when dumping to_dict:
# set to None if retention_time_in_days (nullable) is None
# and model_fields_set contains the field
if self.retention_time_in_days is None and "retention_time_in_days" in self.model_fields_set:
_dict['retentionTimeInDays'] = None
In my case I want to save the payload for my endpoint in a file without the retentionTimeInDays property since it's optional. I load the file with the from_dict method which puts None for retentionTimeInDays. The server correctly validates that this property cannot be null if present and fails 400.
I'd like to check with you if you as well consider this wrong behaviour before digging deeper.
What do you think?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue from the provided OpenAPI schema and inspect the generated Python Pydantic class's from_dict and to_dict methods. Verify that an absent optional, non-nullable retentionTimeInDays field stays absent rather than becoming None, and that the resulting payload is accepted by the server.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, python
- Domain
- api, devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100