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

Cannot Construct Subtype of Sealed Class #844

Open
1 of 4 tasks
skvariljan opened this issue Nov 5, 2024 · 1 comment
Open
1 of 4 tasks

Cannot Construct Subtype of Sealed Class #844

skvariljan opened this issue Nov 5, 2024 · 1 comment
Labels

Comments

@skvariljan
Copy link

skvariljan commented Nov 5, 2024

Search before asking

  • I searched in the issues and found nothing similar.
  • I have confirmed that the same problem is not reproduced if I exclude the KotlinModule.
  • I searched in the issues of databind and other modules used and found nothing similar.
  • I have confirmed that the problem does not reproduce in Java and only occurs when using Kotlin and KotlinModule.

Describe the bug

After upgrading from v2.17.2 to v2.18.1, we cannot parse sealed hierarchies annotated with @JsonTypeInfo anymore. The code in the example works in v2.17.2, but doesn't work in later versions and produces the following exception:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of ChildClass (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)

To Reproduce

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "_type")
sealed class BaseClass

data class ChildClass(val text: String) : BaseClass()

val json =
    """
        {
            "_type": "ChildClass",
            "text": "Test"
        }
    """

val jacksonObjectMapper = ObjectMapper().registerKotlinModule()
val message = jacksonObjectMapper.readValue<BaseClass>(json)

println(message is ChildClass)

Expected behavior

Prints true.

Versions

Kotlin:
Jackson-module-kotlin: 2.18.1
Jackson-databind: 2.18.1

Additional context

No response

@skvariljan skvariljan added the bug label Nov 5, 2024
@cowtowncoder
Copy link
Member

One suggestion: this might be due to change in detection of "Creator" style (Delegating vs Property), and not polymorphic handling or sealed classes.
One way to fix this is so is by adding

@JsonCreator(mode = JsonCreator.Mode.DELEGATING)

to constructor (I forget how that is done with Kotlin -- @k163377 can help).

The change, if my hunch is correct, is because of how ambiguity of 1-property types is resolved -- this is heuristic and fundamentally ambiguous. But formerly (2.17) it seems that the case was deemed as Delegating (not recognizing text as property), but with property introspection rewrite in 2.18, as Property-based.

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

2 participants