swagger-api / swagger-api/swagger-codegen
[java, spring]: codegen.v3:swagger-codegen-maven-plugin does not create @JsonTypeInfo and @JsonSubTypes annotations if polymorphism is used
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
We are using polymorphism like the famous Pet/Cat/Dog example. In effect, the problem occurs even if you use the example code exactly as provided (see below in "Swagger generation file"
In order to use these classes correctly as body-input in Rest-class, the base class (Pet) must be annotated with @Json-related annotations like this
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Cat.class, name = "Cat"),
@JsonSubTypes.Type(value = Dog.class, name = "Dog")})
The io.swagger.codegen.v3:swagger-codegen-maven-plugin simply doesn't create this annotations.
The interesting thing (which I figured out after several hours of research and Try-and-Error): if I use the (older) io.swagger:swagger-codegen-maven-plugin, it works.
Swagger-codegen version
We tested several versions of io.swagger.codegen.v3:swagger-codegen-maven-plugin, latest was 3.0.19. But also 3.0.3 doesn't do it better.
The latest tested version of io.swagger:swagger-codegen-maven-plugin was 2.4.17. As I said, this one works.
Swagger declaration file content or url
"Pet": {
"type": "object",
"discriminator": "petType",
"properties": {
"name": {
"type": "string"
},
"petType": {
"type": "string"
}
},
"required": [
"name",
"petType"
]
},
"Cat": {
"description": "A representation of a cat",
"allOf": [
{
"$ref": "#/definitions/Pet"
},
{
"type": "object",
"properties": {
"huntingSkill": {
"type": "string",
"description": "The measured skill for hunting",
"default": "lazy",
"enum": [
"clueless",
"lazy",
"adventurous",
"aggressive"
]
}
},
"required": [
"huntingSkill"
]
}
]
},
"Dog": {
"description": "A representation of a dog",
"allOf": [
{
"$ref": "#/definitions/Pet"
},
{
"type": "object",
"properties": {
"packSize": {
"type": "integer",
"format": "int32",
"description": "the size of the pack the dog is from",
"default": 0,
"minimum": 0
}
},
"required": [
"packSize"
]
}
]
},
Command line used for generation
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.19</version>
<executions>
<execution>
<id>api</id>
<phase>process-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${file.location}
</inputSpec>
<language>java</language>
<configOptions>
<library>jersey2</library>
<interfaceOnly>true</interfaceOnly>
<useTags>true</useTags>
<dateLibrary>java8</dateLibrary>
</configOptions>
<apiPackage>${default.package}</apiPackage>
<modelPackage>${default.package}.model</modelPackage>
</configuration>
</execution>
</executions>
</plugin>
Note that for <language> I also tested "spring". For <library>, I tested several values given with '<configHelp>true</configHelp>'
Steps to reproduce
call mvn generate and see the result Pet.java class
Related issues/PRs
Suggest a fix/enhancement
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
Start by running mvn generate with the provided Swagger declaration and plugin configuration, then inspect the generated Pet.java class. Trace the Java generator's model and template handling for discriminator-based polymorphism. Done means generated base models include the expected Jackson polymorphism annotations and the result is covered by a reproducible generation check.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100