Skip to content

Optional enum fields are generated with the wrong type #311

Closed
@frjonsen

Description

@frjonsen

Describe the bug
An endpoint which takes a body which has enum fields is being incorrectly generated with the type Union[Unset, None]. Of not here is that the enum itself is correctly generated in its own file, but are not used (or even imported) in the model for the wrapping type.

To Reproduce
This is a minimal specification I came up with, which reproduces the problem:

We generate the client using simply openapi-python-client generate and the specification below, with no additional configuration.

The result of this is

@attr.s(auto_attribs=True)
class EnumWrapper:
    """  """

    myenum: Union[Unset, None] = UNSET
    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        myenum = None

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if myenum is not UNSET:
            field_dict["myenum"] = myenum

        return field_dict

    @staticmethod
    def from_dict(src_dict: Dict[str, Any]) -> "EnumWrapper":
        d = src_dict.copy()
        myenum = None

        enum_wrapper = EnumWrapper(
            myenum=myenum,
        )

        enum_wrapper.additional_properties = d
        return enum_wrapper

   # Rest of the file omitted for brevity

Expected behavior
We only just upgraded to the version which uses Unset, so I am not quite sure what exactly the type should be, but I assume something along the lines of either Optional[MyEnum], Union[MyEnum, Unset] or Union[Optional[MyEnum], Unset]

OpenAPI Spec File

openapi: 3.0.2
info:
  title: bar
  version: unknown
paths:
  /foo:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EnumWrapper'
        required: true
      responses:
        '204':
          description: Successful Response
components:
  schemas:
    MyEnum:
      title: MyEnum
      enum:
        - TypeA
        - TypeB
      type: string
      description: An enumeration.
    EnumWrapper:
      title: EnumWrapper
      type: object
      properties:
        myenum:
          allOf:
            - $ref: '#/components/schemas/MyEnum'

Desktop (please complete the following information):

  • OS: [Ubuntu 20.04]
  • Python Version: 3.7.9
  • openapi-python-client version: 0.7.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions