Skip to content

[BUG] [PYTHON] Schema name normalization collision generates invalid model imports #24880

Description

@j-steuer

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using snake case names for OpenAPI components such as operation IDs and schema names, the Python generator seems to normalize segments that contain a single character by combining them (e.g. test_s_t becomes test_st).

This can also lead to name collisions, which can break generation for otherwise valid schemas. While there seems to be a fallback for colliding operation IDs, when schema names collide in this way, only one of the schemas is generated and the API code attempts to import the missing model.

openapi-generator version

v7.25.0

OpenAPI declaration file content or url
openapi: 3.0.0

info:
  title: Schema Name Collision Reproducer
  version: 1.0.0

paths:
  /first:
    get:
      operationId: get_first_model
      responses:
        '200':
          description: First model
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/test_st'

  /second:
    get:
      operationId: get_second_model
      responses:
        '200':
          description: Second model
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/test_s_t'

components:
  schemas:
    test_st:
      type: object
      properties:
        first:
          type: string

    test_s_t:
      type: object
      properties:
        second:
          type: integer
Generation Details

From the languages I tested, this issue only seems to occur with Python.

Steps to reproduce

Generate a Python client using the provided OpenAPI declaration.

Only one TestSt model is generated, while default_api.py contains an invalid import for TestST:

from openapi_client.models.test_st import TestST
from openapi_client.models.test_st import TestSt

There is no TestST model generated, so the resulting client cannot be imported.

The issue can also be observed in the generated response type for the second operation, which references TestST even though only TestSt exists.

Related issues/PRs
Suggest a fix

I am not familiar enough with the generator code to suggest an appropriate fix, but applying the same fallback solution used for operation IDs to schema models would at least fix the specific issue described here.

This case is obviously unlikely to occur naturally, but a simple workaround is to rename the affected components so that their normalized model names do not collide.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions