swagger-api / swagger-api/swagger-codegen

[PYTHON] Duplicate method definitions generated when requestBody has multiple content types (F811)

Open
#12,662 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

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 fields

Command line used for generation

swagger-codegen generate -i openapi.json -l python -o ./python-client

Steps to reproduce

  1. Create an OpenAPI 3.0 spec with an endpoint that accepts both application/json and application/x-www-form-urlencoded content types
  2. Generate a Python client using swagger-codegen
  3. Run a Python linter (flake8, ruff, pylint) on the generated code

Related issues

This is the same root cause as:

  • #11848 - [JAVA] requestBody's with multiple options for content generate duplicate methods
  • #10331 - [SPRING] Multiple Content Types in APIs cause ambiguous mapping
  • swagger-api/swagger-codegen-generators#441 - [Spring] Incorrect code generated for endpoints with multiple request types

Suggest a fix

Possible solutions:

  1. Generate methods with unique names based on content type (e.g., exchange_o_auth_code_json, exchange_o_auth_code_form)
  2. Generate a single method that can handle multiple content types via a parameter
  3. 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the supplied OpenAPI declaration and swagger-codegen generate -i openapi.json -l python -o ./python-client, then inspect the generated authentication_api.py around lines 356-634. Determine how the Python generator handles multiple requestBody content types; done means the generated client has no duplicate method definition and passes a Python linter.

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
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.