OpenAPITools / OpenAPITools/openapi-generator
[BUG] [python] Circular imports on inherited discriminators.
Open
Nobody has claimed this yet.
Issue: Bug
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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 viapython -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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied Swagger definition and the Python generator command, then inspect the generated Group and PresentationElement models. Run python -m pytest . on the generated client and compare the imports with PR #16809; done means the reproduced client imports successfully and its tests pass without the circular-import error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100