GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Multiple body-types in an endpoint with same model (referenced via `$ref`) results in ambiguity in generated code · Issue #1004 · openapi-generators/openapi-python-client

Multiple body-types in an endpoint with same model (referenced via $ref) results in ambiguity in generated code #1004

New issue
Copy link
New issue
Copy link
Open
Open
Multiple body-types in an endpoint with same model (referenced via $ref) results in ambiguity in generated code#1004
Copy link

Description

@linkdd
linkdd
opened on Mar 15, 2024
Issue body actions
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
},
Reacted by Vincent Simonin, Tim Schupp, sfowl and Viktor Sokolov
Reacted by sfowl

Activity

  1. sfowl commented on May 17, 2024

    @sfowl
    sfowl
    on May 17, 2024
    Contributor
    More actions

    I worked around this by using a custom template for endpoint_module.py.jinja. Gist, Patch:

    $ diff /tmp/orig/endpoint_module.py.jinja custom-templates/endpoint_module.py.jinja
    21c21
    <     {{ arguments(endpoint, include_client=False) | indent(4) }}
    ---
    >     {{ arguments(endpoint, include_client=True) | indent(4) }}
    48a49
    >     content_type = client._headers.get("Content-Type") or client._headers.get("content-type")
    50c51
    <     if isinstance(body, {{body.prop.get_type_string() }}):
    ---
    >     if isinstance(body, {{body.prop.get_type_string() }}) and (content_type is None or content_type == "{{ body.content_type }}"):
    108c109
    <         {{ kwargs(endpoint, include_client=False) }}
    ---
    >         {{ kwargs(endpoint, include_client=True) }}
    134c135
    <         {{ kwargs(endpoint, include_client=False) }}
    ---
    >         {{ kwargs(endpoint, include_client=True) }}
    

    With this custom template in a location like custom-templates/endpoint_module.py.jinja, openapi-python-client generate --custom-template-path=custom-templates generates the patched code. Using the patch requires setting the content-type header to the desired type when creating a Client object.

    I suspect this is not a proper fix, which probably needs a better way of handling differing body types with the same class names. I'm not really knowledgable enough about this codebase to do that, just sharing in case it helps someone else.

    Reacted by David Delassus
  2. iloveitaly
    added a commit that references this issue on May 31, 2026

    fix: Explicitly set boundary for multipart/form-data (openapi-generat…

    fb3f866
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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.