Skip to content

[BUG][Java][Spring] Models not generated correctly when identical names used from schemas #22627

@miller79

Description

@miller79

Description

When generating models using the spring or java generator, if the spec uses external references and there are name conflicts internally, it will not generate the models you would expect.

openapi-generator version

I am using the openapi-generator-maven-plugin version 7.18.0

OpenAPI declaration file content or url

The following is a minimum example:

openapi.yaml

openapi: 3.1.0
info:
  title: Example API
  version: "1.0.0"
  description: A small OpenAPI 3.1.0 document exposing two endpoints for managing items.
paths:
  /items:
    get:
      summary: List all items
      operationId: listItems
      responses:
        "200":
          description: The Item
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                  item:
                    $ref: './test1.yaml#/components/schemas/Item'
  /items2:
    get:
      summary: List all items 2
      operationId: listItems2
      responses:
        "200":
          description: The Item
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                  item:
                    $ref: './test2.yaml#/components/schemas/Item'

test1.yaml

openapi: 3.1.0
info:
  title: Test 1
  version: "1.0.0"
  description: Test 1
components:
  schemas:
    Item:
      title: Item
      type: object
      properties:
        weightUnitOfMeasure:
          $ref: '#/components/schemas/WeightUnitOfMeasure'
    WeightUnitOfMeasure:
      title: Weight Unit of Measure
      type: string
      description: A weight unit of measure
      enum:
        - Grams
        - Kilograms
        - MetricTons
        - Ounces
        - Pounds
        - Tons
      default: Pounds

test2.yaml

openapi: 3.1.0
info:
  title: Test 2
  version: "1.0.0"
  description: Test 2
components:
  schemas:
    Item:
      title: Item
      type: object
      properties:
        weightUnitOfMeasure:
          $ref: '#/components/schemas/WeightUnitOfMeasure'
    WeightUnitOfMeasure:
      title: Weight Unit of Measure
      type: object
      description: A weight unit of measure
      properties:
        unit:
          type: string
        description:
          type: string

Generation Details

The plugin configuration is the following:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.18.0</version>
                <executions>
                      <execution>
                        <id>test-spring</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                src/main/resources/openapi.yaml</inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>spring.api</apiPackage>
                            <modelPackage>spring.model</modelPackage>
                            <modelNamePrefix>Spring</modelNamePrefix>
                            <modelNameSuffix>Model</modelNameSuffix>
                            <configOptions>
                                <useSpringBoot3>true</useSpringBoot3>
                                <openApiNullable>false</openApiNullable>
                                <interfaceOnly>true</interfaceOnly>
                            </configOptions>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-java</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                src/main/resources/openapi.yaml</inputSpec>
                            <generatorName>java</generatorName>
                            <apiPackage>java.api</apiPackage>
                            <modelPackage>java.model</modelPackage>
                            <modelNamePrefix>Java</modelNamePrefix>
                            <modelNameSuffix>Model</modelNameSuffix>
                            <configOptions>
                                <library>restclient</library>
                                <annotationLibrary>none</annotationLibrary>
                                <openApiNullable>false</openApiNullable>
                                <generateBuilders>true</generateBuilders>
                                <useJakartaEe>true</useJakartaEe>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Steps to reproduce

After running, you will notice it will only create 2 model files with both the spring and java generators.

  • java
    • JavaItemModel
    • JavaListItems200ResponseModel
  • spring
    • SpringItemModel
    • SpringListItems200ResponseModel

But you would expect it to make models for each unique item and multiple response models.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions