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

[BUG] Java generator: discriminator enum type not compatible with Spring Boot 3.5.5 / oneOf interfaces

Open SzymonZasada opened this issue 11 months ago • 5 comments

Hi team

I'm experiencing an issue when generating Java client code from the Allegro OpenAPI spec using OpenAPI Generator v7.17.0.

Setup

My Maven plugin configuration:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>7.17.0</version>
    <executions>
        <execution>
            <id>generate-allegro-api</id>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/allegro-swagger.yaml</inputSpec>
                <generatorName>java</generatorName>
                <output>${project.basedir}</output>
                <library>restclient</library>
                <apiPackage>com.allegroclient.api</apiPackage>
                <modelPackage>com.allegroclient.model</modelPackage>
                <configOptions>
                    <sourceFolder>src/main/java</sourceFolder>
                    <useJakartaEe>true</useJakartaEe>
                    <hideGenerationTimestamp>true</hideGenerationTimestamp>
                    <openApiNullable>false</openApiNullable>
                    <legacyDiscriminatorBehavior>false</legacyDiscriminatorBehavior>
                    <useOneOfInterfaces>true</useOneOfInterfaces>
                </configOptions>
                <generateApiDocumentation>false</generateApiDocumentation>
                <generateModelDocumentation>false</generateModelDocumentation>
                <generateApiTests>false</generateApiTests>
                <generateModelTests>false</generateModelTests>
            </configuration>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

In the generated code, some polymorphic models using oneOf with a discriminator (e.g., TextSafetyInformation) generate an enum for the type property. This leads to Spring Boot 3.5.5 runtime

getType() in 'TextSafetyInformation' clashes with 'getType()' in the interface 'ProductSetElementSafetyInformationRequestSafetyInformation'; incompatible return type

I tried:

legacyDiscriminatorBehavior=true/false
useOneOfInterfaces=true
openApiNullable=false

…but the enum type is still generated and incompatible with interfaces.

Environment OpenAPI Generator: 7.17.0 Spring Boot: 3.5.5 Java: 21

Ecample of openAPi file https://developer.allegro.pl/swagger.yaml or: https://developer.allegro.pl/documentation

SzymonZasada avatar Dec 14 '25 18:12 SzymonZasada

Sounds like the same issue as https://github.com/OpenAPITools/openapi-generator/issues/22177.

Mattias-Sehlstedt avatar Dec 14 '25 19:12 Mattias-Sehlstedt

It is true that both issues are very similar, as they both relate to how the Java generator handles polymorphic models using oneOf and a discriminator.

However, there is a fundamental difference in where the failure occurs. The other issue caused the generator to crash during the generation process itself. In my situation, the code generates without any errors. The problems only appear after generation, when attempting to use the resulting classes, leading to runtime or compilation errors due to type incompatibility.

Question:

Is there any estimation on when a fix might be implemented and merged?

SzymonZasada avatar Dec 14 '25 19:12 SzymonZasada

I forgot to mention that I tried version 7.11.0, and also other wchich supports restClient but none of them work in this case.

SzymonZasada avatar Dec 14 '25 20:12 SzymonZasada

I assume that both stem from the current lack of support for the case where the parent has the type as a String but the child as an enum. There can of course still be differences in how such an inheritance can be expressed, hence why there are different symptoms for it.

For the case of useOneOfInterfaces=true I believe no version will ever work, since your parent (interface) will always have a String, while the implementor has an enum. If you turn it off I would at least assume that you get another error if you were to try 7.11.0. I usually solve this in my project by having all children share the same enum, or by removing the enum entirely, and relying on the implicit discriminator mapping.

Is there any estimation on when a fix might be implemented and merged?

The issue will most likely only be resolved if you yourself submit a fix, or elect to attempt to sponsor one. The most common solution for these issue is to adjust the specification so that it conforms to a format that the generator can handle. For example by inserting a custom normalizer.

Mattias-Sehlstedt avatar Dec 14 '25 20:12 Mattias-Sehlstedt

Oh oki understood. Thanks I will try drop fix to resolve this

SzymonZasada avatar Dec 15 '25 06:12 SzymonZasada