OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Referenced components with same name not distinguished when one starts with lowercase

Open
#20,814 1 comment 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

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

The generator does not generate classes correctly when there are referenced components with the same name but one of them starts with lowercase and the other one with uppercase.

Here is an example

cat.yaml

schemas:
    Cat:
      type: object
      properties:
        age:
          type: integer
        color:
          type: string
        sound:
          $ref: '#/components/schemas/sound'

    sound:
      type: string
      enum: [MEOUW, MRRR]

dog.yaml

schemas:
    Dog:
      type: object
      properties:
        age:
          type: integer
        color:
          type: string
        sound:
          $ref: '#/components/schemas/Sound'

    Sound:
      type: string
      enum: [WOOF, RRRR]

pet-shop.yaml

paths:
  /cat/{catId}:
    get:
      operationId: getCat
      description: Gets a cat
      parameters:
        - in: path
          name: catId
          description: Id of the cat
          schema:
            type: integer
          required: true
      responses:
        '200':
          description: 'OK'
          content:
            application/json:
              schema:
                $ref: 'cat.yaml#/components/schemas/Cat'
 
  /dog/{dogId}:
    get:
      operationId: getDog
      description: Gets a dog
      parameters:
        - in: path
          name: dogId
          description: Id of the dog
          schema:
            type: integer
          required: true
      responses:
        '200':
          description: 'OK'
          content:
            application/json:
              schema:
                $ref: 'dog.yaml#/components/schemas/Dog'

When we run the generate-sources for pet-shop.yaml only one SoundDto.java enum is generated - the one that starts with lowercase and cosequently the DogDto contains an enum [MEOUW, MRRR].

If both enums are called Sound or sound, then the generator correctly generates two enums - SoundDto and Sound1Dto, but when one starts with lowercase and the other with uppercase, the lowercase takes precedence and as a result the Dog object gets an incorrect type for it's sound property.

The expected behavior is that the generator recognizes these are two different types and generates two different classes

openapi-generator version

7.11.0 but is also reproducible with earlier versions (6.6.0). Last known version that worked was 4.3.1 but there might be more versions that worked between 4.3.1 and 6.6.0

Steps to reproduce

Attached is the example project, it can be reproduced by running maven

pet-shop.zip

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 Maven with the attached pet-shop.yaml project and compare the generated SoundDto.java and DogDto outputs. Trace how references from cat.yaml and dog.yaml are resolved and named. Done means lowercase sound and uppercase Sound are treated as distinct types, with separate generated classes and the correct enum used by DogDto.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.