openapi-generator: [BUG][JAVA] Generated model contains double JsonTypeName annotation

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

The attached spec seems to be causing some problems with version 5.0.0 of the openapi-generator-maven-plugin. It creates this model class, which has two JsonTypeName annotations, causing a compilation error:

@JsonPropertyOrder({
  First.JSON_PROPERTY_PROP
})
@JsonTypeName("first")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2021-01-28T07:47:45.336926+01:00[Europe/Oslo]")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "discriminator", visible = true)
@JsonTypeName("first")

public class First extends Parent {
...
}

generated-sources/openapi/src/main/java/test/model/First.java:[40,1] com.fasterxml.jackson.annotation.JsonTypeName is not a repeatable annotation type

openapi-generator version

openapi-generator-maven-plugin, version 5.0.0

OpenAPI declaration file content or url

https://gist.github.com/andersflemmen/8e4eddc90b7293fd55f4d75c637b3502

Generation Details

Maven configuration

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <inputSpec>${project.basedir}/openapi.json</inputSpec>
                <generatorName>java</generatorName>
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                    <feignVersion>10.x</feignVersion>
                    <java8>true</java8>
                </configOptions>
                <modelPackage>test.model</modelPackage>
                <apiPackage>test.api</apiPackage>
                <invokerPackage>test.invoker</invokerPackage>
                <generateApiTests>false</generateApiTests>
                <generateModelTests>false</generateModelTests>
                <generateSupportingFiles>true</generateSupportingFiles>
                <library>feign</library>
                <addCompileSourceRoot>true</addCompileSourceRoot>
            </configuration>
        </execution>
    </executions>
</plugin>

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 19
  • Comments: 16 (8 by maintainers)

Most upvoted comments

We work around it at build time using the replacer Maven plugin like so:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.3</version>
    <executions>
        <!-- to address https://github.com/OpenAPITools/openapi-generator/issues/8559 -->
        <!-- removes the 2nd @JsonTypeName in the below snippet
        @JsonTypeName("Classname_allOf")
        ...some more annotations...
        @JsonTypeName("Classname_allOf")

        public class ClassnameAllOf {
        -->
        <execution>
            <id>remove-2nd-json-type-name-for-allof-types</id>
            <phase>process-resources</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <basedir>${basedir}/target/generated-sources/openapi</basedir>
                <filesToInclude>**/*AllOf.java</filesToInclude>
                <regex>true</regex>
                <token>@JsonTypeName\(".*_allOf"\)\n\n</token>
                <value />
            </configuration>
        </execution>
    </executions>
</plugin>

Also happens in 5.4.0

This is caused by the fact that @JsonTypeName is present twice:

I suspect this bug has been present for about 10 months. @wing328 with #6995 you added it to pojo.mustache - about a month after it was added to typeInfoAnnotation.mustache with #6551.

Can confirm on 5.1.0 with modelNamePrefix as well.

Happens with 5.1.0 as well and happens only when I am using the modelNameSuffix directive.