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 fields
Command line used for generation
swagger-codegen generate -i openapi.json -l python -o ./python-client
Steps to reproduce
- Create an OpenAPI 3.0 spec with an endpoint that accepts both
application/json and application/x-www-form-urlencoded content 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:
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
Description
When an OpenAPI 3.0 endpoint defines a
requestBodywith multiple content types (e.g.,application/jsonandapplication/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
Command line used for generation
Steps to reproduce
application/jsonandapplication/x-www-form-urlencodedcontent typesRelated issues
This is the same root cause as:
Suggest a fix
Possible solutions:
exchange_o_auth_code_json,exchange_o_auth_code_form)Generated code example
The generator produces:
Linter output: