Repository navigation
Description
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=trueSame behaviour observed with -g python-fastapi.
Steps to reproduce
- Create the multi-file spec structure above.
- Run the generator against the root YAML.
- Inspect the generated models —
ContainerMappingandContainerMapping1are both present. Allcontainer_mappingfields referenceContainerMapping1.
Related issues/PRs
- Duplicate classes / Models are generated if ( $ref) referenced externally. #2701 — "Duplicate classes/Models are generated if ($ref) referenced externally" (open since 2019)
- [BUG] Schema $ref with same name is not producing models properly #2729 — "[BUG] Schema $ref with same name is not producing models properly" (open since 2019)
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.jsontocomponents/schemasin the root spec:ContainerMapping1is still generated when walking theallOfchains. - 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.
Activity