FasterXML / FasterXML/jackson-module-kotlin

Reified types are not inlined into type references of generic classes

Aperta
#243 9 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Kotlin
Stelle
1.2k
Fork
187
Merge medio
9h 48m
PR unite (30g)
16

Descrizione

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
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.