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
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Cat.class, name = "Cat"),
@JsonSubTypes.Type(value = Dog.class, name = "Dog")
})
public abstract class Pet {
public String name;
}
public class Dog extends Pet{
}
public class Cat extends Pet{
}
What we get from the codegen (we use swagger-codegen-maven-plugin version 3.0.46) is this:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true )
@JsonSubTypes({
@JsonSubTypes.Type(value = Cat.class, name = "Cat"),
@JsonSubTypes.Type(value = Dog.class, name = "Dog"),
})
public class Pet {
@JsonProperty("name")
private String name = null;
@JsonTypeId
private String type = null;
...
When we serialize:
Cat cat = (Cat) new Cat().name("charly");
objectMapper.writeValueAsString(cat);
result looks like this:
{
"type" : "",
"name" : "charly"
}
For the serialization to work properly, I have to modify the generated Pet.java as follows:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true )
@JsonSubTypes({
@JsonSubTypes.Type(value = Cat.class, name = "Cat"),
@JsonSubTypes.Type(value = Dog.class, name = "Dog"),
})
@JsonIgnoreProperties(
value = "type", // ignore manually set type, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the type to be set during deserialization
)
public class Pet {
@JsonProperty("name")
private String name = null;
private String type = null;
...
So I remove the @JsonTypeId and I add the @JsonIgnoreProperties for the type variable.
Which I think as how the code should be generated. @JsonTypeId marks a field for overriding the type id. So since type is null, we get an empty value in JSON. What should be done is that the discriminator property should be ignored, so that Jackson can populate it with the type name automatically.
Looks like the @JsonTypeId was added as result of this issue: #105 Back then it looks like Jackson was behaving differently and type was still populated. This looks however like it was an incorrect handling of @JsonTypeId on Jackson side.
The text was updated successfully, but these errors were encountered:
We generate the oas3 schema from the java classes which use polymorphism. The schema that we get looks right:
The relevant source Java classes look like this:
What we get from the codegen (we use swagger-codegen-maven-plugin version 3.0.46) is this:
When we serialize:
result looks like this:
For the serialization to work properly, I have to modify the generated
Pet.java
as follows:So I remove the
@JsonTypeId
and I add the@JsonIgnoreProperties
for the type variable.Which I think as how the code should be generated.
@JsonTypeId
marks a field for overriding the type id. So sincetype
isnull
, we get an empty value in JSON. What should be done is that the discriminator property should be ignored, so that Jackson can populate it with the type name automatically.Looks like the
@JsonTypeId
was added as result of this issue: #105 Back then it looks like Jackson was behaving differently and type was still populated. This looks however like it was an incorrect handling of@JsonTypeId
on Jackson side.The text was updated successfully, but these errors were encountered: