openapi-generators / openapi-python-client

Generate modern Python clients from OpenAPI
MIT License
1.33k stars 202 forks source link

Optional property of type list is initialized with empty list instead of UNSET when using from_dict() #961

Open keshamin opened 8 months ago

keshamin commented 8 months ago

Describe the bug A model with a property of type Union[Unset, List[SomeEnum]] initializes correctly when using __init__ initialization:

>>> # with non-empty list
>>> SomeRequestBody(my_optional_list=[SomeEnum.FOO])
SomeRequestBody(my_optional_list=[<SomeEnum.FOO: 'FOO'>], additional_properties={})

>>> # with empty list
>>> SomeRequestBody()
SomeRequestBody(my_optional_list=<fake_spec_client.types.Unset object at 0x7ff2001db9d0>, additional_properties={})

BUT, when I initialize it using .from_dict() method, unexpectedly it provides an empty list as a default value:

>>> # everything's fine when passing an explicit value
>>> SomeRequestBody.from_dict(dict(my_optional_list=['FOO']))
SomeRequestBody(my_optional_list=[<SomeEnum.FOO: 'FOO'>], additional_properties={})

>>> # but this is not expected. I'd expect the same result as from regular initialization SomeRequestBody() 
>>> SomeRequestBody.from_dict(dict())
SomeRequestBody(my_optional_list=[], additional_properties={})

From my point of view, .from_dict() initialization should keep align with the logic in __init__ initialization.

OpenAPI Spec File

{"openapi": "3.0.2", "info": {"title": "fake_spec", "version": "0.0.0"},
  "paths": {},
  "components": {
    "schemas": {
      "SomeRequestBody": {
        "title": "SomeRequestBody",
        "type": "object",
        "properties": {
          "my_optional_list": {"type": "array", "items": {"$ref": "#/components/schemas/SomeEnum"}}
         }
      },
      "SomeEnum": {
        "title": "SomeEnum",
        "enum": ["FOO", "BAR"],
        "type": "string"
      }
    }
  }
}

Desktop (please complete the following information):

rrshen commented 6 months ago

I'm having this problem too :'(

I have two optional lists, and I want to be able to set one to [] without doing anything to the other. However, if I do a MyModel.from_dict({'myoptionallist': None}), both 'myoptionallist' and 'myotheroptionallist' get set to [].

ssoto commented 3 months ago

I tried last week and I'm having the same issue.

I look for some examples but I couldn't find any, Could any maintener help us?

ssoto commented 3 months ago

I'm gonna try datamodel_code_generator/ an alternative supported by Pydantic. Maybe it is helpful for others.