OpenAPITools / OpenAPITools/openapi-generator

OpenAPI Generator duplicates models when using OAS 3.1 $ref with sibling annotations

Open
#22,560 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

When using OpenAPI 3.1 with JSON Schema 2020-12 and external $defs, OpenAPI Generator generates duplicate C# models (e.g. HeightCharacteristic and HeightCharacteristic1) and incorrect documentation.

This occurs when:

  • Multiple schemas reference the same $defs alias
  • The $ref usage sites include different sibling annotations (e.g. description)
  • The generator is run on a sufficiently large schema graph (e.g. DATEX II)

Although the schemas are valid JSON Schema 2020-12, OpenAPI Generator:

  1. Incorrectly merges $ref sibling annotations into the resolved target schema and caches it without context.
  2. Treats later traversals of the same canonical schema (with different merged annotations) as distinct schema identities, leading to *1 suffixed duplicate models.
  3. Copies XML documentation from the first encountered context into later models (documentation bleed).

This is deterministic with large schemas in DATEX and not user error, below a reproducable example.

Using 7.17.x for NET CORE

Steps to reproduce:

java -jar openapi-generator-cli.jar generate -i repro.json -g aspnetcore -o out --additional-properties=aspnetCoreVersion=6.0 --skip-operation-example

Schema files:

repro.json

DATEXII_3_MessageContainer.json

DATEXII_3_Common.json

Observed result:

  • Duplicate models generated (HeightCharacteristic, HeightCharacteristic1)
  • XML documentation copied from the first-encountered context

Possible fixes:

  • Treat $ref sibling annotations as usage-site only, not part of the resolved schema cache.
  • Deduplicate models based on canonical schema identity, not merged/annotated instances.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied openapi-generator-cli command with repro.json, then compare the generated HeightCharacteristic and HeightCharacteristic1 models and their XML documentation. Trace how the external $defs references and sibling descriptions are resolved and cached during generation. Done means the same canonical schema produces one C# model and usage-site documentation does not bleed between contexts.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.