OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java][Spring] Models not generated correctly when identical names used from schemas
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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.
javaJavaItemModelJavaListItems200ResponseModel
springSpringItemModelSpringListItems200ResponseModel
But you would expect it to make models for each unique item and multiple response models.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Use openapi.yaml with test1.yaml and test2.yaml and the provided Maven plugin configuration to reproduce the Java and Spring outputs. Compare the generated model sets with the expected distinct referenced models and response models, then trace external-reference and model-name handling and add a regression test for the collision.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend-api-design, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100