-
Notifications
You must be signed in to change notification settings - Fork 6k
Open
Description
Description
When an OpenAPI 3.0 endpoint defines a requestBody with multiple content types (e.g., application/json and application/x-www-form-urlencoded), swagger-codegen generates duplicate Python methods with the same name but different signatures. This causes Python linting errors (F811: redefinition of unused name) and makes the first method unreachable since the second definition shadows it.
swagger-codegen version
swagger-codegen 3.0.75
OpenAPI declaration file content or url
/oauth2/token:
post:
operationId: exchangeOAuthCode
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Oauth2TokenBody'
application/x-www-form-urlencoded:
schema:
type: object
properties:
grant_type:
type: string
code:
type: string
client_id:
type: string
# ... additional form fieldsCommand line used for generation
swagger-codegen generate -i openapi.json -l python -o ./python-clientSteps to reproduce
- Create an OpenAPI 3.0 spec with an endpoint that accepts both
application/jsonandapplication/x-www-form-urlencodedcontent types - Generate a Python client using swagger-codegen
- Run a Python linter (flake8, ruff, pylint) on the generated code
Related issues
This is the same root cause as:
- [JAVA] requestBody's with multiple options for content generate duplicate methods in Java classes #11848 - [JAVA] requestBody's with multiple options for content generate duplicate methods
- [SPRING] Multiple Content Types in APIs cause ambiguous mapping. #10331 - [SPRING] Multiple Content Types in APIs cause ambiguous mapping
- [Spring] Incorrect code generated for endpoints with multiple request types swagger-codegen-generators#441 - [Spring] Incorrect code generated for endpoints with multiple request types
Suggest a fix
Possible solutions:
- Generate methods with unique names based on content type (e.g.,
exchange_o_auth_code_json,exchange_o_auth_code_form) - Generate a single method that can handle multiple content types via a parameter
- Skip generating duplicate methods and only generate one (preferring JSON body)
Generated code example
The generator produces:
# First definition (JSON body) - lines 356-473
def exchange_o_auth_code(self, body, **kwargs): # noqa: E501
"""Exchange OAuth credentials for JWT tokens"""
...
# Second definition (form params) - lines 475-634 - SHADOWS THE FIRST!
def exchange_o_auth_code(self, grant_type, code, client_id, client_secret, refresh_token, redirect_uri, code_verifier, state, **kwargs): # noqa: E501
"""Exchange OAuth credentials for JWT tokens"""
...Linter output:
F811 Redefinition of unused `exchange_o_auth_code` from line 356
--> tmi_client/api/authentication_api.py:475:9
Metadata
Metadata
Assignees
Labels
No labels