We ran into the following issue when we tried to deserialize a JSON string coming from our Spring rest controller.
FATAL EXCEPTION: main
Process: com.example.driver, PID: 28305
java.lang.IllegalStateException: Failed to load DataModel: com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class com.example.model.StorageDevice] value failed for JSON property storageDeviceId due to missing (therefore NULL) value for creator parameter storageDeviceId which is a non-nullable type
at [Source: (okhttp3.ResponseBody$BomAwareReader); line: 1, column: 1764] (through reference chain: com.example.model.DataModel["storageDevices"]->java.util.ArrayList[0]->com.example.model.cargospace.StorageDevice["storageDeviceId"])
at com.example.model.RestTourRepository$updateDataModel$1.onFailure(RestTourRepository.kt:38)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$2.run(ExecutorCallAdapterFactory.java:79)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
The object we try to construct looks like this:
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator::class,
property = "storageDeviceId"
)
data class StorageDevice (
val storageDeviceId: String,
val configuration: AbstractStorageDeviceConfiguration,
val storageDeviceLocation: StorageDeviceLocation
)
And the JSON string we receive is the following:
{
"storageDeviceId": "71ba49f3-d8c8-425f-b7e4-29b8c49f417d",
"id": 166,
"configuration": {
"storageDeviceConfigurationId": "c2c926b3-de70-491a-9f8d-514830d56a4b",
…
},
"storageDeviceLocation": "1f64613b-6e96-4825-a489-faa00608516e",
"new": false
}, …
We solved the issue by changing the val storageDeviceId: String to val storageDeviceId: String?. The value was then properly set after deserialization.
The question arises why we had to set it to nullable in the first place?
Versions:
jackson-module-kotlin:2.9.0
jackson-annotations:2.9.0
We ran into the following issue when we tried to deserialize a JSON string coming from our Spring rest controller.
The object we try to construct looks like this:
@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator::class, property = "storageDeviceId" ) data class StorageDevice ( val storageDeviceId: String, val configuration: AbstractStorageDeviceConfiguration, val storageDeviceLocation: StorageDeviceLocation )And the JSON string we receive is the following:
We solved the issue by changing the
val storageDeviceId: Stringtoval storageDeviceId: String?. The value was then properly set after deserialization.The question arises why we had to set it to nullable in the first place?
Versions:
jackson-module-kotlin:2.9.0
jackson-annotations:2.9.0