OpenAPITools / OpenAPITools/openapi-generator

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

Open
#14,524 20 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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
  • #14523
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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Generate the provided openapi-schema.yaml with the python-nextgen command and inspect the generated parent.py and Number enum model. Reproduce from_dict and to_dict for singular allOf, then cover anyOf, oneOf, and plural allOf; done means enum serialization works without missing-method exceptions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.