Skip to content
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

[BUG][Python] client's from_dict and to_dict set optional but non-nullable fields to None #18394

Open
alfechner opened this issue Apr 15, 2024 · 2 comments

Comments

@alfechner
Copy link

alfechner commented Apr 15, 2024

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?

@alfechner alfechner changed the title [BUG] [] [BUG] Python client's from_dict and to_dict set optional but non-nullable fields to None Apr 15, 2024
@wing328
Copy link
Member

wing328 commented Apr 16, 2024

 MyObject:
  type: object
  properties:
    retentionTimeInDays:
      type: integer
      nullable: false

if you remove nullable: false, does it work correctly?

@alfechner
Copy link
Author

Hi @wing328 , thanks for the quick reply.

Removing nullable: false doesn't work unfortunately. I only put it in the example to make my point.

@alfechner alfechner changed the title [BUG] Python client's from_dict and to_dict set optional but non-nullable fields to None [BUG][Python] client's from_dict and to_dict set optional but non-nullable fields to None Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants