FasterXML / FasterXML/jackson-module-kotlin

Deserialization of Custom Wrapper Class Fails in Jackson 2.18.1 Without Nested Object Structure

Open
#865 5 comments 0 reactions 0 assignees View on GitHub
to-evaluate
Dominant language
Kotlin
Stars
1.2k
Forks
187
Avg merge
9h 48m
Merged PRs (30d)
16

Description

### Describe your Issue

When upgrading from Jackson `2.17.2` to `2.18.1`, I encountered an issue with deserializing a custom wrapper class (ValidatedList) directly from an array in JSON. Previously, in `2.17.2`, the deserialization worked as expected.

Here is the code representation of my classes:

``` Kotlin
data class Profile(
val name: String,
val list: ValidatedList,
)

class ValidatedList(vararg list: Record) {
var items: List = listOf()

init {
this.items = list.asList()
}
}

data class Record(
val nationality: String,
val firstName: String,
)

```

In Jackson `2.17.2`, the following JSON deserialized correctly:

``` json

{
"name": "JohnDoe",
"list": [
{
"nationality": "US",
"firstName": "John"
},
{
"nationality": "GB",
"firstName": "Jane"
}
]
}

```

But in `2.18.1` i have a problem with deserialization:
```Error: Cannot deserialize value of type `example.ValidatedList` from Array value (token `JsonToken.START_ARRAY`)
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 3, column: 11] (through reference chain: example.Profile["list"])```

To make the deserialization work in `2.18.1`, the JSON has to be structured with an additional `items` key like this:

``` json
{
"name": "JohnDoe",
"list": {
"items": [
{
"nationality": "US",
"firstName": "John"
},
{
"nationality": "GB",
"firstName": "Jane"
}
]
}
}

```

Question
Is there a way to configure Jackson `2.18.1` to accept the original JSON format (without the nested `items` key)? I would like to maintain compatibility with the original JSON format while using the newer version of Jackson.

Any advice or configuration suggestions to address this would be greatly appreciated

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.