-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeFactory
cannot convert Collection
sub-type without type parameters to canonical form and back
#3108
Comments
Assuming this is not Kotlin-specific, it'd be great to be able to reproduce with plain Java, to add test here (tests that use Kotlin would need to go under Kotlin module). |
Thanks for the fast answer! Following is the test converted to Java. The first one would only work if you have the Kotlin libs on the class path, but you can just remove it. public class JacksonTypeFactoryTest {
private final ObjectMapper objectMapper = new ObjectMapper();
@Test
public void empty_list() {
String canonical = objectMapper.getTypeFactory().constructType(CollectionsKt.<String>emptyList().getClass()).toCanonical();
System.out.println(canonical);
JavaType type = objectMapper.getTypeFactory().constructFromCanonical(canonical);
System.out.println(type);
}
@Test
public void string_list() {
String canonical = objectMapper.getTypeFactory().constructType(StringList.class).toCanonical();
System.out.println(canonical);
JavaType type = objectMapper.getTypeFactory().constructFromCanonical(canonical);
System.out.println(type);
}
@Test
public void non_collection() {
String canonical = objectMapper.getTypeFactory().constructType(ConcreteType.class).toCanonical();
System.out.println(canonical);
JavaType type = objectMapper.getTypeFactory().constructFromCanonical(canonical);
System.out.println(type);
}
static class StringList extends ArrayList<String> {}
static class ParamType<T> {}
static class ConcreteType extends ParamType<Integer> {}
} |
Thanks! It seems likely that addressing problems shown by 2 Java-only cases might handle Kotlin case too. |
I can reproduce this. Yeah, this will be very difficult to resolve... I'll see what I can do, but I would recommend that no code outside of Jackson core functionality tries to use canonical name for |
TypeFactory
cannot convert Collection
sub-type without type parameters to canonical form and back
Fixed this in 2.12 for 2.12.3, wrt. "simple" cases presented: works by checking that the number of type parameters The problem fundamentally is that I'll have to think about what to do wrt canonical name in general for 3.0: it is really only ever needed for one quite specific case -- that of |
Thanks for fixing this so quickly! I found this bug through a standard software I'm using that's internally using Jackson to serialize/deserialize data. I'll have a look at how exactly they are using the canonical name and point them to your comment about not using it outside Jackson itself. |
@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. |
Describe the bug
When constructing a type using
constructType
and getting its canonicalString
representation, a type parameter is added to classes that implement aCollection
type, even if the class itself does not have any type parameters. Trying to read the result back usingconstructFromCanonical
fails withjava.lang.IllegalArgumentException: Cannot create TypeBindings for class kotlin.collections.EmptyList with 1 type parameter: class expects 0
.Version information
Tested with
2.11.4
and2.12.2
.To Reproduce
See the following test written in Kotlin:
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.The text was updated successfully, but these errors were encountered: