Skip to content

[BUG] [python-nextgen] Deserialization with singular allOf and readOnly fails due to missing import #14523

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 fails to generate an import for a schema property with a single entry in allOf and some further attributes (such as readOnly or nullable) on the property as opposed to the reference.

Affected:

  • both references and inline objects; anything that results in a new module to import.
  • true readOnly and/or true nullable

Unaffected (generates a wrapper class (e.g., ParentChild) that gets imported):

  • anyOf, oneOf
  • multiple entries in allOf
  • false/missing readOnly and false/missing nullable
  • additionalProperties, description, properties, type, writeOnly; only existence of the attribute matters, not its value (e.g., even if it is the same or different in the reference).

Unaffected (imports an alias):

  • title

Unaffected (imports correctly (e.g., Child)):

  • items

Impact:

  • That is how DRF Spectacular generates read-only/nullable references, for example.
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:
        child:
          allOf:
          - $ref: '#/components/schemas/Child'
          readOnly: true
    Child:
      type: object
      properties:
        property_of_child: {}
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
>>> from openapi_client import Parent
>>> Parent.from_dict(dict(child=dict(property_of_child=42)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "openapi_client/models/parent.py", line 71, in from_dict
    "child": Child.from_dict(obj.get("child")) if obj.get("child") is not None else None
NameError: name 'Child' is not defined
Related issues/PRs
Suggest a fix

Manually adding the import to parent.py:

from openapi_client.models.child import Child

Fixes deserialization:

>>> from openapi_client import Parent
>>> Parent.from_dict(dict(child=dict(property_of_child=42)))
Parent(child=Child(property_of_child=42))

This must be automated.

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