0

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...

darth jemico
  • 605
  • 2
  • 9
  • 18

1 Answers1

0

IMHO this is the easiest solution for an API that sends multiple unused fields.

@JsonIgnoreProperties(ignoreUnknown = true)
data class Car(
        @JsonProperty("Id")
        val id: Long,

        @JsonProperty("Age")
        val age: Int,

        @JsonProperty("Vendor")
        val vendor: String,

        @JsonProperty("UniqueAudiTechnology")
        val hasUniqueAudiTechnology: Boolean,

        @JsonProperty("SkodaRentalProgramId")
        val skodaRentalProgramId: Int,

        @JsonProperty("Model")
        val model: String,

        @JsonProperty("MaxPassengers")
        val maxPassengers: Int)

It can get really tedious if there are many fields, but that's a bad API design in the first place.

Check this answer for more info in the above way.

rtsketo
  • 1,168
  • 11
  • 18
  • that's not the solution i need. the point is that i need to organize some hierarchy of interface and data classes or maybe of just data classes, that could be defined by className... – darth jemico Oct 22 '20 at 20:30