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

MismatchedInputException: Cannot construct instance when deserialising class with inline field #187

Closed
markth0mas opened this issue Nov 4, 2018 · 8 comments

Comments

@markth0mas
Copy link

markth0mas commented Nov 4, 2018

I've been playing around with Kotlin 1.3 features, and decided to give inline classes a try. With Kotlin 1.3M2 it was possible to serialise and deserialise classes with fields that are inline classes. I just tried them again with Kotlin 1.3 final (1.3.0), and noticed that deserialisation no longer works (serialisation is okay). The Kotlin 1.3 RC release notes mention a change in the binary representation of inline classes, so I guess that could be the root cause.

I've put together a minimal test class to illustrate:

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import org.junit.Assert.assertEquals
import org.junit.jupiter.api.Test

class InlineClassJsonSerialisation {
    @Test
    fun roundTripJson() {
        val objectMapper = ObjectMapper().registerKotlinModule()

        val person = Person(Name("Mark"))
        val json = objectMapper.writeValueAsString(person)

        println("json = $json")

        val parsedPerson = objectMapper.readValue<Person>(json)
        assertEquals(person, parsedPerson)
    }
}

inline class Name(val value: String)

data class Person(val name: Name)

--
If you run this, the test will fail on when calling 'objectMapper.readValue':

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of 'Person' (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"name":"Mark"}"; line: 1, column: 2]

@Leopard2A5
Copy link

I have the same issue.

@Blackdread
Copy link

any news on this issue?

org.springframework.core.codec.CodecException: 
Type definition error: [simple type, class org.xxxx.api.xxxx.Name]; 
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 
Cannot construct instance of `org.xxxx.api.xxxx.Name` 
(no Creators, like default construct, exist): 
cannot deserialize from Object value (no delegate- or property-based Creator)

Inline classes are still not "released" but why is there issues to read from json?
Those fields are compiled are normal java field (inline class Name(val value: String) => String)

@StephenOTT
Copy link

has anyone found a workaround to this?

@junkdog
Copy link

junkdog commented May 31, 2020

has anyone found a workaround to this?

a no-arg constructor seems to do the trick, but it's not really a satisfactory workaround in our case

@cami-la
Copy link

cami-la commented Nov 15, 2021

has anyone found a workaround to this?

a no-arg constructor seems to do the trick, but it's not really a satisfactory workaround in our case

Thank you! It's worked.

@doriancodes
Copy link

I found this (although might not work in Kotlin) https://stackoverflow.com/questions/55529028/jackson-deserialization-error-mismatchedinputexception . Basically adding the annotation @JsonCreator to the inline class Name might work

@dinomite
Copy link
Member

Interesting, I wonder if that annotation causes the Kotlin compiler to generate different bytecode such that the constructor is available for introspection at runtime. I think looking into that would be a good step towards finding a solution for supporting inline classes.

@k163377
Copy link
Contributor

k163377 commented Mar 17, 2023

This issue is closed as the issue regarding deserialization support for value class related content will be summarized in #650.

@k163377 k163377 closed this as completed Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants