Skip to content

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

@keshamin

Description

@keshamin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions