OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][SPRING] OpenAPI 3.1 external $ref schemas generate operation-based model names

Open
#22,753 7 comments 4 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

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

After upgrading an API specification from OpenAPI 3.0.x to 3.1.0, model naming
changes when response schemas are defined in external files and referenced via $ref.

Instead of reusing the referenced schema name, openapi-generator generates
operation-based response models such as:

  • GetItems200Response
  • GetItems200ResponseDataInner

This occurs even when the referenced schema is explicitly named and reusable.

Related OpenAPI Specification discussion:
👉 <LINK TO MY OPENAPI-SPEC ISSUE>

openapi-generator version

7.16.0

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Example API
  version: 1.0.0

paths:
  /items:
    get:
      operationId: getItems
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: schemas/common.yaml#/components/schemas/ListResponse

External schema (schemas/common.yaml):

components:
  schemas:
    ListResponse:
      type: object
      properties:
        data:
          type: array
          items:
            $ref: '#/components/schemas/Item'
        total:
          type: integer
      required:
        - data
        - total

    Item:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
Generation Details

Generator: spring
Library: spring-boot
Options:

  • interfaceOnly=true
  • useSpringBoot3=true
  • generateAliasAsModel=true
Steps to reproduce
  1. Define schemas in an external YAML file
  2. Reference the schema in a response using $ref
  3. Generate models using OpenAPI 3.1.0
Actual Output
GetItems200Response
GetItems200ResponseDataInner
Expected Output
ListResponse
Item
Related issues

OpenApi Specification issue: https://github.com/OAI/OpenAPI-Specification/issues/5181

Suggest a fix

This may not be a bug but expected behavior per OpenAPI 3.1.

Possible improvements:

  • Document this behavior clearly
  • Provide a generator option to prefer referenced schema names when resolvable
  • Clarify recommended structuring for external schema reuse in 3.1

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 reproducing the supplied OpenAPI 3.1 YAML with the spring generator, spring-boot library, and listed options, then compare the generated model names with the expected ListResponse and Item names. Investigate how external $ref schemas are resolved and named, and establish whether the finished work should change generation, add an option, or document the behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.