FasterXML / FasterXML/jackson-module-kotlin

Reified types are not inlined into type references of generic classes

Offen
#243 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Kotlin
Sterne
1.2k
Forks
187
Ø Merge
9 Std. 48 Min.
Gemergte PRs (30 T.)
16

Beschreibung

When you create a type reference for a generic class with a reified type parameter, this type parameter is still generic, not the inlined type. That prevents deserialization of properties of the reified type.
It might look like very rare case unless you prefer composition over inheritance.
Looks like a new typeOf Kotlin function works correctly in this case. Can we use it somehow?

Kotlin version: 1.3.41
Jackson version: 2.9.9

```Kotlin
val objectMapper = jacksonObjectMapper()

fun main() {
val json = """{ "wrapped": { "content": 42 } }"""
println("objectMapper.readValue>(json): ${objectMapper.readValue>(json)}")

println("jacksonTypeRef>(): ${jacksonTypeRef>().type}")

val readWrapped = readWrapped(json)
println("readWrapped(json): $readWrapped")
println("readWrapped(json).wrapped::class: ${readWrapped.wrapped::class}")
}

@UseExperimental(ExperimentalStdlibApi::class)
private inline fun readWrapped(json: String): Wrap {
println("jacksonTypeRef().type: ${jacksonTypeRef().type}")
println("jacksonTypeRef>().type: ${jacksonTypeRef>().type}")
println("typeOf>(): ${typeOf>()}")
return objectMapper.readValue(json)
}

data class Wrap(val wrapped: V)

data class Box(val content: Int)
```

Output:
```
objectMapper.readValue>(json): Wrap(wrapped=Box(content=42))
jacksonTypeRef>(): Wrap
jacksonTypeRef().type: class Box
jacksonTypeRef>().type: Wrap
typeOf>(): Wrap
readWrapped(json): Wrap(wrapped={content=42})
readWrapped(json).wrapped::class: class java.util.LinkedHashMap
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.