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

Incosistent behaviour with a single field #771

Open
3 tasks done
alturkovic opened this issue Feb 17, 2024 · 0 comments
Open
3 tasks done

Incosistent behaviour with a single field #771

alturkovic opened this issue Feb 17, 2024 · 0 comments
Labels

Comments

@alturkovic
Copy link

alturkovic commented Feb 17, 2024

Search before asking

  • I searched in the issues and found nothing similar.
  • I searched in the issues of databind and other modules used and found nothing similar.
  • I have confirmed that the problem only occurs when using Kotlin.

Describe the bug

The example below will not deserialize a single-field data class with a default value:

fun main() {
    val mapper = jacksonObjectMapper()
    val testing = Testing()
    val json = mapper.writeValueAsString(testing)
    println(json) // {} works correctly
    println(mapper.readValue<Testing>(json)) // fails
}

data class Testing(
    private val random: Random = Random
)

But if I add another field to class, it works as expected:

fun main() {
    val mapper = jacksonObjectMapper()
    val testing = Testing()
    val json = mapper.writeValueAsString(testing)
    println(json) // {"name":"John Doe"} works correctly
    println(mapper.readValue<Testing>(json)) // Testing(name=John Doe, random=kotlin.random.Random$Default@2e77b8cf)
}

data class Testing(
    val name: String = "John Doe",
    private val random: Random = Random
)

I have also noticed that it works with a single primitive field:

fun main() {
    val mapper = jacksonObjectMapper()
    val testing = Testing()
    val json = mapper.writeValueAsString(testing)
    println(json) // {} works correctly
    println(mapper.readValue<Testing>(json)) // Testing(name=John Doe)
}

data class Testing(
    private val name: String = "John Doe"
)

Another example is with @JacksonInject:

fun main() {
    val mapper = ObjectMapper()
        .registerModule(kotlinModule {
            enable(KotlinFeature.NullIsSameAsDefault)
        })
        .setInjectableValues(InjectableValues.Std().addValue(String::class.java, "Example"))
    val testing = mapper.readValue<Testing>("{}") // fails
    println(testing.id)
}

data class Testing(
    @JacksonInject val id: String
)

But adding another field do the data class fixes the problem:

fun main() {
    val mapper = ObjectMapper()
        .registerModule(kotlinModule {
            enable(KotlinFeature.NullIsSameAsDefault)
        })
        .setInjectableValues(InjectableValues.Std().addValue(String::class.java, "Example"))
    val testing = mapper.readValue<Testing>("{}") // works
    println(testing.id) // Example
}

data class Testing(
    val name: String = "John Doe", 
    @JacksonInject val id: String
)

To Reproduce

No response

Expected behavior

No response

Versions

Kotlin: 1.9.20
Jackson-module-kotlin: 2.16.0
Jackson-databind: 2.16.0

Additional context

No response

@alturkovic alturkovic added the bug label Feb 17, 2024
@alturkovic alturkovic changed the title Incosistent behaviour with a single "resolved" field Incosistent behaviour with a single field Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant