swagger-api / swagger-api/swagger-codegen
[PYTHON] Cross-references in definitions leads to circular reference in api
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When generating a python client api from a swagger specification containing a cross reference, i.e. type A references type B and type B references type A - the resulting python client cannot be used.
Swagger-codegen version
2.3.1
Swagger declaration file content or url
{
"swagger": "2.0",
"paths": {},
"definitions": {
"MyType1": {
"properties": {
"friend": {
"$ref": "#/definitions/MyType2"
}
}
},
"MyType2": {
"properties": {
"friend": {
"$ref": "#/definitions/MyType1"
}
}
}
}
}
Command line used for generation
java -jar swagger-codegen-cli.jar generate -i python-swagger-bug.json -l python -o python-bug
Steps to reproduce
java -jar swagger-codegen-cli.jar generate -i python-swagger-bug.json -l python -o python-bug
cd python-bug
python
import swagger_client
gives an ImportError:
ImportError: cannot import name 'MyType1'
Related issues/PRs
Probably related to the javascript issue with circular references
https://github.com/swagger-api/swagger-codegen/issues/8037
Suggest a fix/enhancement
The MyType1 python file refers to MyType2 which in turn refers to MyType1 (which hasn't been initialized). Removing the import in MyType2 to MyType1 fixes the problem.
I suppose the code generator should keep track of the order of the imports in the dependencies and prevent the circular imports.
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
Reproduce the issue with python-swagger-bug.json using the stated swagger-codegen CLI command, then inspect the generated MyType1 and MyType2 Python modules and their imports. Confirm the fix by running import swagger_client successfully for the mutually referenced definitions.
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