Reputation: 21
I am trying to deserialize a data class the built from a delegated property.
Here is a quick snippet to reproduce my case :
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION, defaultImpl = BaseResponseDto::class)
interface ResponseDto {
val name: String
val age: Int
}
data class BaseResponseDto(
override val name: String,
override val age: Int,
) : ResponseDto
data class ExtendedResponseDto(
private val base: ResponseDto,
val city: String,
) : ResponseDto by base
val json = """{
"name": "Thomas",
"age": 3,
"city": "Paris"
}"""
val mapper = jacksonObjectMapper()
val dto = mapper.readValue<ExtendedResponseDto>(json)
This code throws this exception.
Exception in thread "main" com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class xxx.ExtendedResponseDto] value failed for JSON property base due to missing (therefore NULL) value for creator parameter base which is a non-nullable type
I've been trying to play with @JsonUnwrapped
and other annotations but could not make it work. Having and sub-class that only have the delegate as constructor parameter seems to be working, but does not meet my needs...
// This can be deserialized
data class ExtendedResponseDto(
private val base: ResponseDto,
) : ResponseDto by base
I hope I am missing something and there is an easy solution. Final objective is not to duplicate the base interface fields
Thank's alot for you help !
Upvotes: 2
Views: 225