Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Shanzeee opened this issue Dec 3, 2024 · 5 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@Shanzeee
Copy link

Shanzeee commented Dec 3, 2024

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:

data class Profile(
    val name: String,
    val list: ValidatedList<Record>,
)

class ValidatedList<Record>(vararg list: Record) {
    var items: List<Record> = 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:

{
  "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<example.Record>` 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:

{
 "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

@Shanzeee Shanzeee added the to-evaluate Issue that has been received but not yet evaluated label Dec 3, 2024
@pjfanning
Copy link
Member

pjfanning commented Dec 3, 2024

2.18.2 is out and has some fixes that could help

@Shanzeee
Copy link
Author

Shanzeee commented Dec 3, 2024

I’ve just tested with Jackson 2.18.2, and the same issue persists.

@pjfanning
Copy link
Member

If this is Kotlin, then you should report this to the jackson-module-kotlin project.

@cowtowncoder
Copy link
Member

Moving to Kotlin module repo.

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Dec 4, 2024
@cowtowncoder
Copy link
Member

I am fairly certain that the issue is with un-annotated 1-arg constructor of ValidatedList -- formerly apparently being heuristically chosen to be Mode.DELEGATING (map value from JSON into type of argument, List here) vs 2.18.0 choosing Mode.PROPERTIES (single-element JSON Object with specified named property).

I am not 100% sure why heuristics details changed, aside from saying that the whole property introspection (including Creator method introspection, that is, constructor/factory method introspection) was completely rewritten to address problems with Records and mismatching of annotations.

But given inherent difficulty in correctly guessing intent of 1-argument constructors, the usual way is to mark Constructor with

@JsonCreator(mode = JsonCreator.Mode.DELEGATING)

(or use @JsonValue)

which will force use of specific mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

3 participants