Skip to content

[BUG][rust-axum][python-fastapi] Duplicate model generated when same external schema is referenced via allOf chains across multiple files #23854

@Shaun-3adesign

Description

@Shaun-3adesign

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?
Description

When the same external schema file is referenced via $ref from multiple external JSON schema files that are themselves composed via allOf, the generator creates duplicate model classes instead of reusing the same named model. The duplicate is suffixed with 1 (e.g. ContainerMapping and ContainerMapping1). All field usages reference ContainerMapping1, leaving ContainerMapping as an unused orphan.

openapi-generator version

openapitools/openapi-generator-cli:v7.22.0

OpenAPI declaration file content or url

Full spec: https://github.com/bbc/tams/blob/main/api/TimeAddressableMediaStore.yaml (multi-file, references schemas in api/schemas/)

Minimal structure to reproduce:

TimeAddressableMediaStore.yaml (root spec):

paths:
  /flows:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: schemas/flow.json
components: {}

schemas/flow.json:

{
    "title": "Flow",
    "type": "object",
    "oneOf": [
        { "$ref": "flow-video.json" },
        { "$ref": "flow-audio.json" }
    ]
}

schemas/flow-video.json:

{
    "title": "Video Flow",
    "type": "object",
    "allOf": [
        { "$ref": "flow-core.json" },
        {
            "type": "object",
            "properties": {
                "format": { "type": "string" }
            }
        }
    ]
}

schemas/flow-core.json:

{
    "title": "Flow Core",
    "type": "object",
    "properties": {
        "container_mapping": {
            "$ref": "container-mapping.json"
        },
        "flow_collection": {
            "$ref": "flow-collection.json"
        }
    }
}

schemas/flow-collection.json:

{
    "title": "Flow Collection",
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "container_mapping": {
                "$ref": "container-mapping.json"
            }
        }
    }
}

schemas/container-mapping.json:

{
    "title": "Container Mapping",
    "type": "object",
    "properties": {
        "track_index": { "type": "integer" }
    }
}
Generation Details
docker run --rm \
    -v $(pwd):/local \
    openapitools/openapi-generator-cli:v7.22.0 generate \
    -i /local/TimeAddressableMediaStore.yaml \
    -g rust-axum \
    -o /local/rust_server \
    --additional-properties=packageName=tams-api-server,generateAliasAsModel=true

Same behaviour observed with -g python-fastapi.

Steps to reproduce
  1. Create the multi-file spec structure above.
  2. Run the generator against the root YAML.
  3. Inspect the generated models — ContainerMapping and ContainerMapping1 are both present. All container_mapping fields reference ContainerMapping1.
Related issues/PRs

Both describe the same root cause and remain unresolved.

Suggest a fix

The generator correctly deduplicates schemas registered in components/schemas when referenced via $ref: '#/components/schemas/<Name>'. The limitation is specific to schemas encountered indirectly through cross-file allOf chains — the generator resolves each external $ref independently rather than checking whether the resolved file path has already been named.

Attempts to work around in the spec:

  • Adding container-mapping.json to components/schemas in the root spec: ContainerMapping1 is still generated when walking the allOf chains.
  • Changing the external JSON files to back-reference the root spec via "$ref": "../root.yaml#/components/schemas/ContainerMapping": the generator treats the cross-document path as a distinct external reference and still inlines the content.

Workaround: generating from a pre-bundled spec (e.g. via Redocly CLI bundle) resolves the duplicates, as the bundler collapses all external $refs into a single components/schemas entry per schema before the generator processes the spec.

The fix would be for the generator to deduplicate schemas by resolved absolute file path, so that the same file encountered in different resolution contexts produces one named model rather than multiple.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions