Good day, Mr. Freeman! I'm trying to make a non trivial polymorphic dto for Json / Object conversion for an external api. The dto contains the property, that can be of two types, but it depends on it's internal value... Let's say that i have a such json:
[{
"Id": 1,
"Age": 2,
"Car": {
"MaxPassengers": 20,
"Model": "Audi",
"UniqueAudyTechnology": true
},
"Vendor": "VOLKSWAGEN AUTO GROUP (VAG)"
},
{
"Id": 2,
"Age": 1,
"Car": {
"MaxPassengers": 5,
"Model": "Skoda",
"SkodaRentalProgramId": 100
},
"Vendor": "VOLKSWAGEN AUTO GROUP (VAG)"
}]
So, in "Car" field i can have any car class, but to define it i need to use Car.Model property. I've made a common interface and data classes:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "Model",
visible = true
)
@JsonSubTypes(value = [
JsonSubTypes.Type(value = Audi::class, name = "Audi"),
JsonSubTypes.Type(value = Skoda::class, name = "Skoda")
])
@ApiModel(description = "Used car")
interface UsedCar {
@get:JsonProperty("Id")
val id: Long
@get:JsonProperty("Age")
val age: Int
@get:JsonProperty("Vendor")
val vendor: String
}
and the data classes:
data class Audi(
@JsonProperty("UniqueAudyTechnology")
val hasUniqueAudyTechnology: boolean,
@JsonProperty("Model")
val model: String,
@JsonProperty("MaxPassengers")
val maxPassengers: Int
)
data class Skoda(
@JsonProperty("SkodaRentalProgramId")
val skodaRentalProgramId: Int,
@JsonProperty("Model")
val model: String,
@JsonProperty("MaxPassengers")
val maxPassengers: Int
)
In fact, i want to make jackson resolve subtype by subtypes property Model. I keep trying all the day, but i can't understand what did i miss...? P.S. the code may not work because i've removed implementation of UsedCar by Audi and Skoda... sorry... no idea how to handle it...