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

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

Open
keshamin opened this issue Feb 14, 2024 · 1 comment

Comments

@keshamin
Copy link

keshamin commented Feb 14, 2024

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
@rrshen
Copy link

rrshen commented Apr 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants