Skip to content

[BUG] [python] Circular imports on inherited discriminators. #16808

@tom300z

Description

@tom300z

When generating a python client using the current snapshot of the openapi generator, an circular import occurs when a discriminator is inherited via AllOf.

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?
    Expected output:
    Running tests via python -m pytest . passes
    Acutal output:
ImportError while importing test module '/home/thoerberg/Documents/Work/repositories/vcd_cloudapi/test/test_group.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
../test/test_group.py:18: in <module>
    from vcd_cloudapi.models.group import Group  # noqa: E501
../vcd_cloudapi/models/group.py:23: in <module>
    from vcd_cloudapi.models.presentation_element import PresentationElement
../vcd_cloudapi/models/presentation_element.py:105: in <module>
    from vcd_cloudapi.models.group import Group
E   ImportError: cannot import name 'Group' from partially initialized module 'vcd_cloudapi.models.group' (most likely due to a circular import) (/home/thoerberg/Documents/Work/repositories/vcd_cloudapi/vcd_cloudapi/models/group.py)
Description

see above

openapi-generator version

the latest Snapshot/freshly built binary

OpenAPI declaration file content or url
{
  "swagger": "2.0",
  "info": {
    "version": "36.3",
    "title": "VMware Cloud Director OpenAPI"
  },
  "basePath": "/cloudapi",
  "tags": [],
  "schemes": [
    "https"
  ],
  "paths": {},
  "definitions": {
    "PresentationElement": {
      "type": "object",
      "required": [
        "elementType"
      ],
      "discriminator": "elementType",
      "properties": {
        "elementType": {
          "type": "string"
        }
      }
    },
    "Group": {
      "allOf": [
        {
          "$ref": "#/definitions/PresentationElement"
        },
        {
          "type": "object",
          "properties": {}
        }
      ]
    }
  }
}
Generation Details
/opt/jdk11/bin/java -jar  /home/thoerberg/Downloads/openapi-generator-cli-7.1.0-20231012.113715-77.jar generate \
  -i input.json \
  -g python \
  -o ...
Suggest a fix

See: PR #16809

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