Skip to content

[BUG] [python-nextgen] (De)serialization of enums fails due to missing conversion methods #14524

Description

@TimoGuenther

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The python-nextgen generator assumes the existence of (from|to)_(dict|json) methods on generated enums, which causes enum (de)serialization to fail with an exception.

Affected:

  • singular allOf: The parent expects (from|to)_dict from the enum.
  • anyOf, oneOf, plural allOf: The composite model expects (from|to)_(dict|json) from the wrapped enum(s).

Impact:

openapi-generator version

6.3.0-20230125.125049-89

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: ""
  version: ""
paths: {}
components:
  schemas:
    Parent:
      type: object
      properties:
        number:
          allOf:
          - $ref: '#/components/schemas/Number'
    Number:
      enum:
      - one
      - two
      - three
      type: string
Generation Details
java -jar openapi-generator-cli-6.3.0-20230125.125049-89.jar generate -i openapi-schema.yaml -g python-nextgen
Steps to reproduce

Assuming the import problem described in #14523 is fixed:

>>> from openapi_client import Parent, Number
>>> Parent.from_dict(dict(number="one"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "openapi_client/models/parent.py", line 73, in from_dict
    "number": Number.from_dict(obj.get("number")) if obj.get("number") is not None else None
AttributeError: type object 'Number' has no attribute 'from_dict'
>>> Parent(number=Number.ONE).to_dict()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "openapi_client/models/parent.py", line 60, in to_dict
    _dict['number'] = self.number.to_dict()
AttributeError: 'Number' object has no attribute 'to_dict'
Related issues/PRs
Suggest a fix

Manually replacing the from_dict call with an enum value lookup (by calling the enum class directly) and removing the block that calls to_dict on the enum from parent.py fixes (de)serialization for singular allOf:

>>> from openapi_client import Parent, Number
>>> Parent.from_dict(dict(number="one"))
Parent(number=<Number.ONE: 'one'>)
>>> Parent(number=Number.ONE).to_dict()
{'number': <Number.ONE: 'one'>}

This must be automated.

The fix for composite models seems more involved (e.g., checking for existence of the required methods on the wrapped value).

Alternatively, the enums could feature trivial implementations of the required methods.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions