You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Looking in the code, it's not possible to specify a default class for a type adapter where the discriminator is null because we do this:
val discriminant = jsonObject[discriminantFieldName] as Any
polymorphicInfo.adapter.createInstance().classFor(discriminant)
If the field is not present, this blows up - we could support this in a back-compatible way by doing this instead:
val discriminant = jsonObject[discriminantFieldName]
polymorphicInfo.adapter.createInstance().classForNullable(discriminant)
In the TypeAdapter interface, we can add a default method like this:
interface TypeAdapter<Output> where Output: Any {
fun classFor(type: Any): KClass<out Output>
fun classForNullable(type: Any?): KClass<out Output>{
return classFor(type as Any)
}
}
For people who want the current behavior, no change is required - the as Any in the default method will throw the same exception, and existing implementations of the interface are still valid because the new methods has a default.
For people who want to allow null, they can do so by implementing the new classForNullable(type: Any?) method.
I would be happy to provide a PR for this, if desired.
The text was updated successfully, but these errors were encountered:
Looking in the code, it's not possible to specify a default class for a type adapter where the discriminator is null because we do this:
If the field is not present, this blows up - we could support this in a back-compatible way by doing this instead:
In the
TypeAdapter
interface, we can add a default method like this:For people who want the current behavior, no change is required - the
as Any
in the default method will throw the same exception, and existing implementations of the interface are still valid because the new methods has a default.For people who want to allow null, they can do so by implementing the new
classForNullable(type: Any?)
method.I would be happy to provide a PR for this, if desired.
The text was updated successfully, but these errors were encountered: