Problem with TypeAdapterRuntimeTypeWrapper and logic in MapTypeAdapterFactory
- Dominant language
- Java
- Stars
- 24.2k
- Forks
- 4.5k
- Avg merge
- 6d 4h
- Merged PRs (30d)
- 12
Description
Here's my Kotlin model:
```kt
sealed class X {
object A : X()
object B : X()
object C : X()
}
data class Y(
val a: A,
val map: Map)
val y = Y(X.A, mapOf("a", X.A))
```
I would like to serialize `y`. I have set a type adapter for X, but also for A.
The problem arises in `MapTypeAdapterFactory`. It will wrap the adapters in `TypeAdapterRuntimeTypeWrapper`:

`TypeAdapterRuntimeTypeWrapper` has a dubious logic to look for a "better" adapter based on runtime type:

This completely breaks my serialization, because now `X.A` inside a map will be serialized using type adapter for `A` instead of for `X`.
The logic inside `TypeAdapterRuntimeTypeWrapper` is very questionable. If we already have an adapter based on map's value type, why do we look at runtime type at all? Sure, runtime type will be more specific, and its adapter more specific, but using the said runtime adapter does not make sense in the context of generic value type, and will be impossible to deserialize. I don't see any case where this logic would make sense.
Is it possible to make this logic optional? Basically, I do **not** want to wrap map's value type in `TypeAdapterRuntimeTypeWrapper`.
Contributor guide
Assessment
This issue has not been assessed yet.