question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failure to resolve generic type parameters on serialization

See original GitHub issue

(note: originally reported against Kotlin module, example in Kotlin but failure in databind, transferred)

I have a Either definition:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes(
    JsonSubTypes.Type(Either.Left::class, name = "left"),
    JsonSubTypes.Type(Either.Right::class, name = "right")
)
sealed class Either<out A, out B> {
    class Left<A>(val value: A): Either<A, Nothing>()
    class Right<B>(val value: B): Either<Nothing, B>()
}

that I use in a simple data class:

data class Foo(
    val bar: Either<String, String>,
    val second: String
)

I create an instance and try to serialize:

return Foo(
    bar = Either.Left("aaa"),
    second = "bbbb"
)

but jackson throws an IndexOutOfBoundsException:

Caused by: java.lang.IndexOutOfBoundsException: Index: 0
        at java.base/java.util.Collections$EmptyList.get(Collections.java:4481)
        at com.fasterxml.jackson.databind.type.TypeFactory._resolveTypePlaceholders(TypeFactory.java:478)
        at com.fasterxml.jackson.databind.type.TypeFactory._bindingsForSubtype(TypeFactory.java:451)
        at com.fasterxml.jackson.databind.type.TypeFactory.constructSpecializedType(TypeFactory.java:423)
        at com.fasterxml.jackson.databind.cfg.MapperConfig.constructSpecializedType(MapperConfig.java:305)
        at com.fasterxml.jackson.databind.DatabindContext.constructSpecializedType(DatabindContext.java:163)
        at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._findAndAddDynamic(BeanPropertyWriter.java:893)
        at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:705)
        at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:722)
        ... 85 common frames omitted

Is it some misconfiguration on my side? I am using jackson 2.10.1.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
frost13itcommented, Feb 26, 2020

@cowtowncoder Kotlin’s Nothing always get compiled into a raw type (actual type stays in Kotlin metadata) as described here. The issue can be reproduced in Java, please look at this gist.

0reactions
cowtowncodercommented, May 28, 2020

If you do not want to include Type Id, then just do not use @JsonTypeInfo; there is no point in working around that if you have actual logic in (de)serializer, and use of 2 separate physical type is more of an implementation detail than type hierarchy to support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java - generic parameter can't be resolved - Stack Overflow
(well, OK, no XML for me) I fail to see any valid reason to use Serializable in a Java project started in the...
Read more >
Restrictions on Generics (The Java™ Tutorials > Learning the ...
Because the Java compiler erases all type parameters in generic code, you cannot verify which parameterized type for a generic type is being...
Read more >
C# Generics
Generic constraints solve the following problem: Generic code can contain code that is incompatible with the specified type argument. Generic constraints ...
Read more >
Register Generic Classes using Generic Type Handler
Register generic classes web step 1. Against Library, click on Show Default Generics. The classes will be listed in Loaded Class list box....
Read more >
Data Types & Serialization
Data Types & Serialization # Apache Flink handles data types and serialization in a unique way, containing its own type descriptors, generic type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found