Home > OS >  Kotlin Jackson "missing type id property 'type' (for POJO property 'data');
Kotlin Jackson "missing type id property 'type' (for POJO property 'data');

Time:04-10

Disclaimer

I know there are many posts with a similar problem, but no solution is working for me. I've been trying since yesterday and the amount of attempts I tried are too many to be listed here. What I'm posting is the latest attempt.

Overview

I have a service, that sends a request to another service and needs to deserialize the response polymorphically.

Classes

This is the format I receive from the rest call. Only two properties.

data class ExecutionPayload(
    val type: ChangeRequestType,
    val data: ExecutionData
)

ExecutionData is the polymorphic bit.

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
    Type(value = UpdateNetworkConfigurationsExecutionData::class, name = "UpdateNetworkConfigurationsExecutionData")
)
sealed class ExecutionData


@JsonIgnoreProperties(ignoreUnknown = true)
data class UpdateNetworkConfigurationsExecutionData(
    @JsonProperty("type") val updatedProfiles: List<UpdateSalesChannelProfileViewModel>,
    @JsonProperty("type") val zoning: List<SoftZoningConfig>
) : ExecutionData()

Details

I'll add some more context, which might be obvious to you, but nevertheless.

ExecutionData will in the future have sublcasses with a completely different data structure than UpdateNetworkConfigurationsExecutionData.

Silly example

data class SomeOtherConfigurationUpdateExecutionData(
    val updateSomethingHere: String,
    val anotherPropertyUpdate: Int
    val anExtraProperty: List<SomeConfig>
) : ExecutionData()

Problem

I keep getting this error no matter how hard I try.

ERROR com.somepackage.utils.Logger - CustomErrorConfig - org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
Could not resolve subtype of [simple type, class com.somepackage.ExecutionData]: missing type id property 'type' (for POJO property 'data');
nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.somepackage.ExecutionData]:
missing type id property 'type' (for POJO property 'data')

I would really appreciate your help.

P.S. I'm new to kotlin, and jackson.

CodePudding user response:

The first thing you need to do is remove the unnecessary @JsonProperty("type"):

@JsonIgnoreProperties(ignoreUnknown = true)
data class UpdateNetworkConfigurationsExecutionData(
    val updatedProfiles: List<UpdateSalesChannelProfileViewModel>,
    val zoning: List<SoftZoningConfig>
) : ExecutionData()

On top of this, keep in mind that the type JSON attribute must match the @Type name attribute. This means that it must match UpdateNetworkConfigurationsExecutionData. Your JSON must be something similar to:

{
  "type": // ChangeRequestType,
  "data": {
      "type": "UpdateNetworkConfigurationsExecutionData",
      "updatedProfiles": [
          // UpdateSalesChannelProfileViewModel objects
      ],
      "zoning": [
          // SoftZoningConfig objects
      ]
  }
}
  • Related