openapi-generators / openapi-generators/openapi-python-client
Multiple body-types in an endpoint with same model (referenced via `$ref`) results in ambiguity in generated code
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2k
- Forks
- 293
- Avg merge
- 34m
- Merged PRs (30d)
- 1
Description
| Python Version | OpenAPI Client Generator Version |
|---|---|
| 3.11.6 | 0.18.0 |
Actual Behavior
When an endpoint has multiple body types, each referencing the same model (via a $ref key), the generator generates the same class name for each body types, which results then in the following generated code:
def _get_kwargs(
# ...
):
# ...
if isinstance(body, WritableJournalEntryRequest):
_json_body = body.to_dict()
_kwargs["json"] = _json_body
headers["Content-Type"] = "application/json"
if isinstance(body, WritableJournalEntryRequest):
_files_body = body.to_multipart()
_kwargs["files"] = _files_body
headers["Content-Type"] = "multipart/form-data"
# ...
As you can see, both if-statement's conditions will resolve to True, therefore only the last body type will be used.
Desired Behavior
The body-type should be part of the generated class name to avoid such ambiguities.
Or an if/elseif/else should be used in the template (which would result in the first one being applied ONLY).
OpenAPI Spec File
The OpenAPI spec is from Netbox, but here is the relevant snippet:
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/WritableJournalEntryRequest"
}
},
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/WritableJournalEntryRequest"
}
}
},
"required": true
},
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 by tracing how the generator handles the OpenAPI requestBody content entries and produces the generated _get_kwargs function. Reproduce the issue with the provided JSON snippet, then add coverage for both application/json and multipart/form-data; done means the generated client selects the correct body handling without ambiguous identical conditions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, 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