openapi-generator
openapi-generator copied to clipboard
Spring 3 Generator, generated parent class don't contain @JsonTypeName on maven plugin 7.2.0
Bug Report Checklist
- [X] Have you provided a full/minimal spec to reproduce the issue?
- [ ] Have you validated the input using an OpenAPI validator (example)?
- [X] 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
Worked on maven plugin version 6.2.1. Tried with latest release 7.2.0
The generated parent class "Animal.java" doesn't contain an annotation @JsonTypeName("Animal").
openapi-generator version
OpenAPI declaration file content or url
components:
schemas:
Dog:
allOf:
- $ref: '#/components/schemas/Animal'
- $ref: '#/components/schemas/Dog_allOf'
Cat:
allOf:
- $ref: '#/components/schemas/Animal'
- $ref: '#/components/schemas/Cat_allOf'
Animal:
discriminator:
propertyName: className
properties:
className:
type: string
color:
default: red
type: string
required:
- className
type: object
Dog_allOf:
properties:
breed:
type: string
type: object
Cat_allOf:
properties:
declawed:
type: boolean
type: object
With following configuration of Maven plugin
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.2.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/resources/test-api.yaml</inputSpec>
<modelNamePrefix>ABC</modelNamePrefix>
<modelNameSuffix>DTO</modelNameSuffix>
<apiPackage>com.test.api</apiPackage>
<modelPackage>com.test.model</modelPackage>
<enablePostProcessFile>false</enablePostProcessFile>
<generatorName>spring</generatorName>
<additionalProperties>removeEnumValuePrefix=false</additionalProperties>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
<generateApiDocumentation>false</generateApiDocumentation>
<generateApiTests>false</generateApiTests>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<skipDefaultInterface>true</skipDefaultInterface>
<serializableModel>true</serializableModel>
<snapshotVersion>true</snapshotVersion>
<useTags>true</useTags>
<useSpringBoot3>true</useSpringBoot3>
<generatedConstructorWithRequiredArgs>false</generatedConstructorWithRequiredArgs>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
The generated code of Animal DTO:
package com.test.model;
import java.net.URI;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.openapitools.jackson.nullable.JsonNullable;
import java.io.Serializable;
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.*;
import jakarta.annotation.Generated;
/**
* ABCAnimalDTO
*/
@JsonIgnoreProperties(
value = "className", // ignore manually set className, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the className to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "className", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = ABCBigCatDTO.class, name = "BigCat"),
@JsonSubTypes.Type(value = ABCCatDTO.class, name = "Cat"),
@JsonSubTypes.Type(value = ABCDogDTO.class, name = "Dog")
})
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-01-10T18:28:22.136625800+01:00[Europe/Berlin]")
public class ABCAnimalDTO implements Serializable {
private static final long serialVersionUID = 1L;
private String className;
private String color = "red";
public ABCAnimalDTO className(String className) {
this.className = className;
return this;
}
/**
* Get className
* @return className
*/
@NotNull
@Schema(name = "className", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("className")
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public ABCAnimalDTO color(String color) {
this.color = color;
return this;
}
/**
* Get color
* @return color
*/
@Schema(name = "color", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("color")
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ABCAnimalDTO animal = (ABCAnimalDTO) o;
return Objects.equals(this.className, animal.className) &&
Objects.equals(this.color, animal.color);
}
@Override
public int hashCode() {
return Objects.hash(className, color);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ABCAnimalDTO {\n");
sb.append(" className: ").append(toIndentedString(className)).append("\n");
sb.append(" color: ").append(toIndentedString(color)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix
I don't think JsonTypeName is needed as Jackson has all the required annotations.
@ExtendWith(SpringExtension.class)
@AutoConfigureJson
class TestInheritance {
@Autowired
ObjectMapper objectMapper;
@Test
void dog() throws IOException {
Dog dogA = new Dog()
.breed("Westy")
// .className("dog") // not needed
.color("black");
String jsonA = objectMapper.writeValueAsString(dogA);
System.out.println(jsonA);
Animal dogB = objectMapper.readValue(jsonA, Animal.class);
System.out.println(dogB);
String jsonB = objectMapper.writeValueAsString(dogA);
System.out.println(jsonB);
assertThat(jsonB).isEqualTo(jsonA);
}
}
output is
{"className":"Dog","color":"black","breed":"Westy"}
class Dog {
class Animal {
className: Dog
color: black
}
breed: Westy
}
{"className":"Dog","color":"black","breed":"Westy"}`
so marrshalling and unmarshalling work correctly
@jpfinne You are right, if the class name the same as the json key. It works correctly. I'll modified the example.
The described removal of @JsonTypeName("Animal")
happens since https://github.com/OpenAPITools/openapi-generator/pull/14733.
cc @jorgerod
@martin-mfg I now understand you want @JSonTypeName
on the parent class.
According to JsonTypeName only subTypes need it
The @JsonTypeName annotation allows developers to specify a custom name for the subtype when dealing with polymorphism. Instead of using fully qualified class names, this annotation provides a way to use a more concise or meaningful name for the type in the serialized JSON.
Do you have an issue in the serialization/deserialization?
Anyway. I will add JsonTypeName to parent classes (https://github.com/OpenAPITools/openapi-generator/pull/17781)
@jpfinne the JsonTypeName needs to be added to the parent classes once module prefix or suffix definied. In java, dealing with polymorphism, it is allowed parent classes have parent class, that is the reason why the annotation here is needed. And thanks for fixing it.