question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][JAVA] Generated model contains double JsonTypeName annotation

See original GitHub issue

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>

Issue Analytics

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

github_iconTop GitHub Comments

7reactions
stefano-luckacommented, Feb 3, 2022

Also happens in 5.4.0

7reactions
marcelstoercommented, May 26, 2021

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>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why discriminator property gets serialized twice?
I Just Ran into the same issue. The problem is with the @JsonTypeInfo annotation on the generated model class. Its being generated as...
Read more >
Jackson Annotations - @JsonTypeName - Tutorialspoint
@JsonTypeName is used to set type names to be used for annotated class. Example - @JsonTypeName. import java.io.
Read more >
Using @JsonTypeInfo annotation to handle polymorphic types
CLASS) and persist that as JSON property (include = JsonTypeInfo.As.PROPERTY). The property name should be 'className'. Let's run our main ...
Read more >
Jackson Annotation Examples - Baeldung
Use Jackson to map custom JSON to any java entity graph with full control over the deserialization process. Read more → ...
Read more >
RE: [jackson-user] Binding polymorphic type discriminator as ...
I also have a JsonSubTypes annotation on the same base class with two classes ... However, I am also providing JSON schema validation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found