You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
openapi-generator generate -g spring -i api.yaml -o generated -DdelegatePattern=true,hideGenerationTimestamp=true
Steps to reproduce
Invoke the command line about with the provided api.yaml
Expected Output:
Animal.java
packageorg.openapitools.model;
importjava.util.Objects;
importcom.fasterxml.jackson.annotation.JsonProperty;
importcom.fasterxml.jackson.annotation.JsonCreator;
importcom.fasterxml.jackson.annotation.JsonSubTypes;
importcom.fasterxml.jackson.annotation.JsonTypeInfo;
importio.swagger.annotations.ApiModel;
importio.swagger.annotations.ApiModelProperty;
importjavax.validation.Valid;
importjavax.validation.constraints.*;
/** * Animal */@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "className", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class, name = "BigDog"),
@JsonSubTypes.Type(value = Cat.class, name = "Cat"),
})
publicclassAnimal {
@JsonProperty("className")
privateStringclassName;
@JsonProperty("color")
privateStringcolor = "red";
publicAnimalclassName(StringclassName) {
this.className = className;
returnthis;
}
/** * Get className * @return className */@ApiModelProperty(required = true, value = "")
@NotNullpublicStringgetClassName() {
returnclassName;
}
publicvoidsetClassName(StringclassName) {
this.className = className;
}
publicAnimalcolor(Stringcolor) {
this.color = color;
returnthis;
}
/** * Get color * @return color */@ApiModelProperty(value = "")
publicStringgetColor() {
returncolor;
}
publicvoidsetColor(Stringcolor) {
this.color = color;
}
@Overridepublicbooleanequals(java.lang.Objecto) {
if (this == o) {
returntrue;
}
if (o == null || getClass() != o.getClass()) {
returnfalse;
}
Animalanimal = (Animal) o;
returnObjects.equals(this.className, animal.className) &&
Objects.equals(this.color, animal.color);
}
@OverridepublicinthashCode() {
returnObjects.hash(className, color);
}
@OverridepublicStringtoString() {
StringBuildersb = newStringBuilder();
sb.append("class Animal {\n");
sb.append(" className: ").append(toIndentedString(className)).append("\n");
sb.append(" color: ").append(toIndentedString(color)).append("\n");
sb.append("}");
returnsb.toString();
}
/** * Convert the given object to string with each line indented by 4 spaces * (except the first line). */privateStringtoIndentedString(java.lang.Objecto) {
if (o == null) {
return"null";
}
returno.toString().replace("\n", "\n ");
}
}
Dog.java
publicabstractclassDogextendsAnimal {
}
BigDog.java
publicclassBigDogextendsDog {
}
Cat.java
publicclassCatextendsAnimal {
}
Actual Output
Animal.java
packageorg.openapitools.model;
importjava.util.Objects;
importcom.fasterxml.jackson.annotation.JsonProperty;
importcom.fasterxml.jackson.annotation.JsonCreator;
importcom.fasterxml.jackson.annotation.JsonSubTypes;
importcom.fasterxml.jackson.annotation.JsonTypeInfo;
importio.swagger.annotations.ApiModel;
importio.swagger.annotations.ApiModelProperty;
importjavax.validation.Valid;
importjavax.validation.constraints.*;
/** * Animal */@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "className", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class, name = "Dog"),
@JsonSubTypes.Type(value = Cat.class, name = "Cat"),
})
publicclassAnimal {
@JsonProperty("className")
privateStringclassName;
@JsonProperty("color")
privateStringcolor = "red";
publicAnimalclassName(StringclassName) {
this.className = className;
returnthis;
}
/** * Get className * @return className */@ApiModelProperty(required = true, value = "")
@NotNullpublicStringgetClassName() {
returnclassName;
}
publicvoidsetClassName(StringclassName) {
this.className = className;
}
publicAnimalcolor(Stringcolor) {
this.color = color;
returnthis;
}
/** * Get color * @return color */@ApiModelProperty(value = "")
publicStringgetColor() {
returncolor;
}
publicvoidsetColor(Stringcolor) {
this.color = color;
}
@Overridepublicbooleanequals(java.lang.Objecto) {
if (this == o) {
returntrue;
}
if (o == null || getClass() != o.getClass()) {
returnfalse;
}
Animalanimal = (Animal) o;
returnObjects.equals(this.className, animal.className) &&
Objects.equals(this.color, animal.color);
}
@OverridepublicinthashCode() {
returnObjects.hash(className, color);
}
@OverridepublicStringtoString() {
StringBuildersb = newStringBuilder();
sb.append("class Animal {\n");
sb.append(" className: ").append(toIndentedString(className)).append("\n");
sb.append(" color: ").append(toIndentedString(color)).append("\n");
sb.append("}");
returnsb.toString();
}
/** * Convert the given object to string with each line indented by 4 spaces * (except the first line). */privateStringtoIndentedString(java.lang.Objecto) {
if (o == null) {
return"null";
}
returno.toString().replace("\n", "\n ");
}
}
Dog.java
publicclassDogextendsAnimal {
}
BigDog.java
publicclassBigDogextendsDog {
}
Cat.java
publicclassCatextendsAnimal {
}
Suggest a fix
Update DefaultCodegen.createDiscriminator
to add a model for each Schema that is descended from schemaName.
Allow the schema consumer to provide configuration to the code generator to obtain the desired @JsonSubTypes behavior either by designating the desired values explicitly or by indicating the models in the inheritance hierarchy that should be abstract and ignored (similar to the way that JPA uses @MappedSuperclass and @Entity).
Update the Maven plugin to support the new config options.
mappedSupermodels
openapi-generator generate -g spring -i api.yaml -o generated -DdelegatePattern=true,hideGenerationTimestamp=true -DmappedSupermodel=Dog
subtypeModels
openapi-generator generate -g spring -i api.yaml -o generated -DdelegatePattern=true,hideGenerationTimestamp=true -DsubtypeModels=Cat,BigDog
The text was updated successfully, but these errors were encountered:
Description
The openapi-generator spring generator generates incorrect @JsonSubTypes for multilevel inheritance
openapi-generator version
3.3.4
OpenAPI declaration file content or url
api.yaml
Command line used for generation
Steps to reproduce
Invoke the command line about with the provided api.yaml
Expected Output:
Animal.java
Dog.java
BigDog.java
Cat.java
Actual Output
Animal.java
Dog.java
BigDog.java
Cat.java
Suggest a fix
DefaultCodegen.createDiscriminator
to add a model for each
Schema
that is descended fromschemaName
.@JsonSubTypes
behavior either by designating the desired values explicitly or by indicating the models in the inheritance hierarchy that should be abstract and ignored (similar to the way that JPA uses@MappedSuperclass
and@Entity)
.mappedSupermodels
subtypeModels
The text was updated successfully, but these errors were encountered: