OpenAPITools / OpenAPITools/openapi-generator
[BUG] Generator Spring creates duplicate "inner" models for array reference types
Nobody has claimed this yet.
- 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 (example)?
- 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
In specific circumstances, starting with openapi-generator-maven version 7.8.0, on specific systems, generates undesired extra models for referenced array types. Instead of using the defined referenced model. I've tried to create an MVP that should generate the same results for everyone.
openapi-generator version
- 7.8.0
- 7.9.0
- 7.10.0
- 7.11.0
- 7.12.0
- 7.13.0
OpenAPI declaration file content or url
api.yaml
openapi: 3.1.1
info:
title: Duplicate model example
version: 0.0.0-SNAPSHOT
servers:
- url: /api
paths:
/test:
put:
responses:
'200':
description: example
content:
'application/json;charset=UTF-8':
schema:
$ref: '#/components/schemas/ExampleType'
components:
schemas:
ExampleType:
type: object
required:
- arrayType
properties:
arrayType:
type: array
items:
$ref: '#/components/schemas/SubType'
SubType:
type: object
properties:
namedField:
type: string
Generation Details
> docker run --rm --volume "${PWD}:/java-build" --workdir "/java-build" maven:3.9-eclipse-temurin-21-alpine mvn clean openapi-generator:generate enforcer:enforce
(...)
[INFO] writing file /java-build/target/generated-sources/openapi/src/gen/java/com/example/openapi/inner/api/model/RestExampleType.java
[INFO] writing file /java-build/target/generated-sources/openapi/src/gen/java/com/example/openapi/inner/api/model/RestExampleTypeArrayTypeInner.java
[INFO] writing file /java-build/target/generated-sources/openapi/src/gen/java/com/example/openapi/inner/api/model/RestSubType.java
(...)
[ERROR] Some files should not exist:
[ERROR] /java-build/target/generated-sources/openapi/src/gen/java/com/example/openapi/inner/api/model/RestExampleTypeArrayTypeInner.java
(...)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.openapi.inner</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<properties>
<java.version>21</java.version>
<main.package>com.example.openapi.inner</main.package>
<openapi-generator.version>7.13.0</openapi-generator.version>
<jakarta-validation.version>3.1.0</jakarta-validation.version>
<swagger.version>2.2.25</swagger.version>
<openapi-jackson.version>0.2.6</openapi-jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>${jakarta-validation.version}</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>${openapi-jackson.version}</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator.version}</version>
<configuration>
<inputSpec>${project.baseUri}/api.yaml</inputSpec>
<generatorName>spring</generatorName>
<output>target/generated-sources/openapi</output>
<groupId>${main.package}</groupId>
<packageName>${main.package}</packageName>
<apiPackage>${main.package}.api</apiPackage>
<modelPackage>${main.package}.api.model</modelPackage>
<artifactId>backend</artifactId>
<modelNamePrefix>Rest</modelNamePrefix>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<skipDefaultInterface>true</skipDefaultInterface>
<sourceFolder>src/gen/java</sourceFolder>
<useOptional>true</useOptional>
<openApiNullable>false</openApiNullable>
<interfaceOnly>true</interfaceOnly>
<unhandledException>true</unhandledException>
<useAbstractionForFiles>true</useAbstractionForFiles>
<useSpringBoot3>true</useSpringBoot3>
<useTags>true</useTags>
</configOptions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<rules>
<requireFilesDontExist>
<files>
<file>${project.build.directory}/generated-sources/openapi/src/gen/java/com/example/openapi/inner/api/model/RestExampleTypeArrayTypeInner.java</file>
</files>
</requireFilesDontExist>
</rules>
<fail>true</fail>
</configuration>
</plugin>
</plugins>
</build>
</project>
Steps to reproduce
add above information to a pom.xml and api.yaml in a folder and run provided command line to (hopefully) get the resulting console output and 3 generated models. The one suffixed with "inner" should not exist.
Running with docker should always result in the duplicate model being generated, running normally under windows with mvn clean openapi-generator:generate@openapi-generate should generate only 2 models instead of 3.
As of testing on 28-04-2025, this also structurally fails on windows on version 7.8 and up. (I'm not sure what changed in my local environment to cause this)
Related issues/PRs
not aware of related issues
Suggest a fix
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
Reproduce the issue with the provided api.yaml and pom.xml by running the Docker Maven command, then compare the generated models with the Windows result. Trace the Spring generator's handling of the array reference in ExampleType; done means the referenced SubType is used and RestExampleTypeArrayTypeInner.java is not generated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100