Skip to content

Deserialization issue, @JsonCreator ignored #5040

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

Closed
1 task done
fabienfleureau opened this issue Mar 21, 2025 · 3 comments
Closed
1 task done

Deserialization issue, @JsonCreator ignored #5040

fabienfleureau opened this issue Mar 21, 2025 · 3 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@fabienfleureau
Copy link

fabienfleureau commented Mar 21, 2025

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Hello,
While updating to spring boot 3.4.x I faced an issue related to the upgrade of jackson dependencies from 2.17.3 to 2.18.2
Deserialization is not working anymore for a class with 2 constructor, one with @JsonCreator annotation.

It fails with this exception:

Instantiation of [simple type, class User] value failed for JSON property fullName due to missing (therefore NULL) value for creator parameter fullName which is a non-nullable type
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 6, column: 13] (through reference chain: User["fullName"])
com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class User] value failed for JSON property fullName due to missing (therefore NULL) value for creator parameter fullName which is a non-nullable type
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 6, column: 13] (through reference chain: User["fullName"])
	at com.fasterxml.jackson.module.kotlin.KotlinValueInstantiator.createFromObjectWith(KotlinValueInstantiator.kt:97)
	at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:214)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:541)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1497)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4931)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3868)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3851)
	at UserDeserializationTest.test user deserialization(UserDeserializationTest.kt:25)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

It seems it take the wrong constructor, I also try with @JsonIgnore and @JsonCreator(mode=DISABLED) on the first constructor but it didn't help.
Any idea on how to solve this issue?

Version Information

2.18.2,2.18.3

Reproduction

Project with failing test can be found on this repo https://github.com/fabienfleureau/jackson-deserialization-issue/

I have defined this class:

class User(
    val age: Int,
    fullName: String,
) {
    var firstName: String = fullName.split(" ").first()
    var lastName: String = fullName.split(" ").last()

    fun fullName() = "$firstName $lastName"

    @JsonCreator
    constructor(): this(
        age = 0,
        fullName = "John Doe",
    )
}

and the json to deserialize looks like this:

            {
                "age": 25,
                "firstName": "Jane",
                "lastName": "Doe"
            }

Expected behavior

I expected to have a user deserialized having age set to 25, firstName set to Jane and lastName set to Doe

Additional context

No response

@fabienfleureau fabienfleureau added the to-evaluate Issue that has been received but not yet evaluated label Mar 21, 2025
@pjfanning
Copy link
Member

Is this Kotlin? If so bring it to https://github.com/FasterXML/jackson-module-kotlin

It does seem like a really odd example. The json does not relate to the constructors. Can't you add another constructor that takes "age", "firstName", "lastName"?

@fabienfleureau
Copy link
Author

fabienfleureau commented Mar 21, 2025

Yes it is kotlin. I will create an issue on the other project. -> FasterXML/jackson-module-kotlin#932

Let's say I can't modify the structure of the class for "legacy" reasons.

@cowtowncoder
Copy link
Member

I could have transferred but looks like it's been re-created: closing this one.

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