openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] Generator Spring creates duplicate "inner" models for array reference types

Open Gelunox opened this issue 1 year ago • 4 comments

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)?
  • [ ] 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

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

Gelunox avatar Dec 09 '24 10:12 Gelunox

thanks for reporting the issue

do you mind doing a git bisect to identify the commit causing the change?

wing328 avatar Dec 16 '24 12:12 wing328

I am afraid I'm currently not in a position to do a git bisect. I have however been able to confirm that this is still not fixed in 7.13. I will keep using 7.7 until this is fixed.

I have updated the OP to automatically fail the build if the undesired class is found.

Gelunox avatar Apr 28 '25 07:04 Gelunox

@wing328 I stumbled upon a similar issue, where additional inner classes are generated and referenced even though enumsAsRef is used. I think this the same bug @Gelunox has reported.

I have done a git bisect with the sample of @Gelunox and identified the first bad commit: 5612852fb6398d78e561147cb5aea91523e2ab89

The problem arises when OAS 3.1 is used together with the Maven variable ${project.baseUri}. baseUri is required for generation on Windows with OAS 3.1 – with basedir the build fails on Windows (see https://github.com/OpenAPITools/openapi-generator/issues/14648)

sebkoller avatar Jun 03 '25 10:06 sebkoller

The problem arises when OAS 3.1 is used together with the Maven variable ${project.baseUri}. baseUri is required for generation on Windows with OAS 3.1 – with basedir the build fails on Windows (see #14648)

I can confirm that it seems like this issue is "solved" by not using ${project.baseUri}. Although this seems like a rather bizarre solution.

Using ${project.basedir} also seems to work properly. I can't test right now if ${project.parent.basedir} also works, but I may try to verify tomorrow if my actual full-size projects also work with this... "fix".

Thanks @sebkoller for your efforts 👍.

Update the next day: in multi-module projects I still cannot use ${project.basedir} or ${project.parent.basedir} because of the "Illegal character in opaque part at index 2" problem. Not using any maven property as part of the inputSpec config does work, but that is not an acceptable solution; I do not want to be chained to have my terminal in a specific directory for parts of the project to be able to build.

Gelunox avatar Jun 03 '25 13:06 Gelunox