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

JsonTypeInfo.Id.DEDUCTION blocks signatures for non-instantiable classes #4708

Open
1 task done
Xfel opened this issue Sep 19, 2024 · 2 comments
Open
1 task done

JsonTypeInfo.Id.DEDUCTION blocks signatures for non-instantiable classes #4708

Xfel opened this issue Sep 19, 2024 · 2 comments
Labels
2.19 Issues planned at 2.19 or later polymorphic-deduction Issues related to "Id.DEDUCTION" mode of `@JsonTypeInfo` pr-welcome Issue for which progress most likely if someone submits a Pull Request

Comments

@Xfel
Copy link

Xfel commented Sep 19, 2024

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

I have a class hierarchy with multiple levels of abstract classes that I want to deserialize in DEDUCTION mode.

If I explicitly add only the leaf classes as subtypes it works fine.

However I'm using kotlin and sealed classes, and here the intermediate levels are also registered. These intermediate level classes do not have a JsonCreator and are sealed/abstract, so they cannot be instantiated. I would expect the AsDeductionTypeDeserializer to simply ignore these types.

However, it does not do so and instead complains that these abstract types have the same signature.

Version Information

2.17.2

Reproduction

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
sealed class Ingredient {
    sealed class Item: Ingredient() {
        abstract class Id(
            @JsonProperty("item")
            val id: String,
            val count: Int = 1,
        ): Item()
        data class Tag(
            @JsonProperty("tag")
            val tag: String,
            val count: Int = 1,
        ): Item()
    }
    sealed class Fluid: Ingredient() {
        data class Id(
            @JsonProperty("fluid")
            val id: String,
            val count: Int = 1000,
        ): Fluid()
        data class Tag(
            @JsonProperty("fluidTag")
            val tag: String,
            val count: Int = 1000,
        ): Fluid()
    }
}

fun main() {
    val mapper = jacksonObjectMapper()
    println(mapper.readValue<Ingredient>("""
        {
            "item": "test"
        }
    """.trimIndent()))
}

Expected behavior

No response

Additional context

jackson-module-kotlin only scans for direct subclasses of sealed classes, recursion in the hierarchy is handled by the core logic. Therefor this seems like an issue for this project.

@Xfel Xfel added the to-evaluate Issue that has been received but not yet evaluated label Sep 19, 2024
@cowtowncoder cowtowncoder added the polymorphic-deduction Issues related to "Id.DEDUCTION" mode of `@JsonTypeInfo` label Sep 19, 2024
@cowtowncoder
Copy link
Member

@drekbour WDYT? Although in general it is difficult to determined viability of deserializing into given type (Jackson really knows by trying to figure it out), being abstract would be reliable "no can do" signal indeed.

@drekbour
Copy link
Contributor

Sounds sensible to me

  • cheap and a once-only cost during type registration
  • doesn't invalidate/limit any other ideas floating around about hinting stuff to the deduction code

@cowtowncoder cowtowncoder added 2.19 Issues planned at 2.19 or later and removed to-evaluate Issue that has been received but not yet evaluated labels Sep 22, 2024
@cowtowncoder cowtowncoder added the pr-welcome Issue for which progress most likely if someone submits a Pull Request label Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.19 Issues planned at 2.19 or later polymorphic-deduction Issues related to "Id.DEDUCTION" mode of `@JsonTypeInfo` pr-welcome Issue for which progress most likely if someone submits a Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants