[BUG][Java] Discriminator enum with mapping
Description
When a model with a discriminator field and discriminator.mapping is defined, the generated class annotations cause an unexpected output in serialization.
openapi-generator version
6.2.0
Steps to reproduce
Given the following description:
Kitten:
type: object
properties:
origin:
enum:
- DOMESTICATED
- FERAL
type: string
...
required:
- origin
discriminator:
propertyName: origin
mapping:
DOMESTICATED: '#/components/schemas/Domesticated'
FERAL: '#/components/schemas/Feral'
the generated class contains the following annotations:
@JsonIgnoreProperties(
value = "origin", // ignore manually set origin, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the origin to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "origin", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Domesticated.class, name = "DOMESTICATED"),
@JsonSubTypes.Type(value = Domesticated.class, name = "Domesticated"),
@JsonSubTypes.Type(value = Feral.class, name = "FERAL"),
@JsonSubTypes.Type(value = Feral.class, name = "Feral")
})
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-10-13T10:27:58.342752+02:00[Europe/Brussels]")
public class Kitten {
...
The problem with this is when it is serialized, e.g. using Jackson:
String kitten = new ObjectMapper().writeValueAsString(new Kitten().origin(OriginEnum.DOMESTICATED));
System.out.println(kitten);
the result is:
{"origin":"Kitten","id":null,"name":null,"price":null,"color":null,"pictureIds":null}
when the expected value for the origin field is "DOMESTICATED". I also made some test where the result was "Domesticated" (which is also not the desired output)
With version 5.4.0, the same definition outputs the code that generates the desired output:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "origin", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Domesticated.class, name = "DOMESTICATED"),
@JsonSubTypes.Type(value = Feral.class, name = "FERAL"),
})
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-10-13T10:32:14.126224+02:00[Europe/Brussels]")
public class Kitten {
...
Disclaimer: I am aware that the mapping is unnecessary here, it is just an example. Without the discriminator.mapping defined, the output is correct.
Let me know if further clarification is needed and thank you in advance for comments.