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.

`TypeFactory` cannot convert `Collection` sub-type without type parameters to canonical form and back

See original GitHub issue

Describe the bug When constructing a type using constructType and getting its canonical String representation, a type parameter is added to classes that implement a Collection type, even if the class itself does not have any type parameters. Trying to read the result back using constructFromCanonical fails with java.lang.IllegalArgumentException: Cannot create TypeBindings for class kotlin.collections.EmptyList with 1 type parameter: class expects 0.

Version information Tested with 2.11.4 and 2.12.2.

To Reproduce

See the following test written in Kotlin:

class JacksonTypeFactoryTest {
    private val objectMapper = ObjectMapper()

    @Test
    fun `EmptyList type can be converted to canonical string and back`() {
        val canonical = objectMapper.typeFactory.constructType(emptyList<String>().javaClass).toCanonical()
        println(canonical)
        val type = objectMapper.typeFactory.constructFromCanonical(canonical)
        println(type)
    }

    @Test
    fun `StringList type can be converted to canonical string and back`() {
        val canonical = objectMapper.typeFactory.constructType(StringList::class.java).toCanonical()
        println(canonical)
        val type = objectMapper.typeFactory.constructFromCanonical(canonical)
        println(type)
    }

    @Test
    fun `Non-collection type can be converted to canonical string and back`() {
        val canonical = objectMapper.typeFactory.constructType(ConcreteType::class.java).toCanonical()
        println(canonical)
        val type = objectMapper.typeFactory.constructFromCanonical(canonical)
        println(type)
    }

    class StringList : ArrayList<String>()

    open class ParamType<T>

    class ConcreteType : ParamType<Int>()
}

The first two tests fail, while the third one succeeds. So it seems to be a problem only with types that extend Collection.

Expected behavior TypeFactory should always produce canonical type names that it can parse again.

Additional context Although this doesn’t seem to be a Kotlin-specific problem, it gains relevance by the fact that Kotlin uses the EmptyList class to optimize empty lists.

This is similar to #1415 but different in that I’m not using constructCollectionType here.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Apr 9, 2021

Thanks! It seems likely that addressing problems shown by 2 Java-only cases might handle Kotlin case too.

0reactions
cowtowncodercommented, Apr 14, 2021

@lbilger no problem! And yes, it is probably good to have a look – there may be legit need there, and since the methods are public I can see why they may seem like good options. But if at all possible I would suggest trying to avoid use.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SUSE-SU-2022:1678-1: important: Security update for jackson ...
Type ' should accept array of names + Jackson version alignment with ... 'Collection' sub-type without type parameters to canonical form and ...
Read more >
SUSE alert SUSE-SU-2022:1678-1 (jackson-databind ...
... doesn't work with empty strings + 'TypeFactory' cannot convert 'Collection' sub-type without type parameters to canonical form and back ...
Read more >
AnnotatedTypeFactory (checker-framework 3.28.0 API)
Provides utility method to infer type arguments. protected AnnotatedTypeFormatter · typeFormatter. This formatter is used for converting AnnotatedTypeMirrors to ...
Read more >
Converting a collection of subtype to collection of supertype
Even if this were the right thing to do, you're using a generic type parameter ( <A> ), so the A in this...
Read more >
AnnotatedTypeFactory - javadoc.io
The methods of this class take an element or AST node, and return the annotated type as an AnnotatedTypeMirror . The methods are:....
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