Skip to content

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

Open

Description

@ericfitz

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:

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions