openapi-python-client icon indicating copy to clipboard operation
openapi-python-client copied to clipboard

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

Open keshamin opened this issue 1 year ago • 4 comments

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):

  • OS: macOS 14.1
  • Python Version: 3.10
  • openapi-python-client version: reproduced on v0.13.4 (which I use because I need Python 3.7 support) and on the latest v0.17.2

keshamin avatar Feb 14 '24 13:02 keshamin

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 [].

rrshen avatar Apr 25 '24 20:04 rrshen

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

  • OS: masOS 14.5
  • Python version: 3.11.8
  • openapi-python-client: 0.21.1

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

ssoto avatar Jul 22 '24 07:07 ssoto

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

ssoto avatar Jul 22 '24 07:07 ssoto

Are you only seeing this with non-primitive types?

I don't have any lists of enums, but I do have a list of a class of objects, where I see this behaviour. However, I also have a list of integers and that doesn't have this issue.

stereofuture avatar Dec 17 '24 17:12 stereofuture