Skip to content
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

JsonDeserialize(contentAs=...) broken with raw collections #2553

Closed
cpopp opened this issue Nov 26, 2019 · 5 comments
Closed

JsonDeserialize(contentAs=...) broken with raw collections #2553

cpopp opened this issue Nov 26, 2019 · 5 comments
Milestone

Comments

@cpopp
Copy link

cpopp commented Nov 26, 2019

JsonDeserialize(contentAs=...) doesn't seem to be honored if used with raw collections.

In the following example I would expect that the items field would be deserialized as the MyItem class but instead comes out as a java.util.LinkedHashMap. Adjusting the following example from List to List<Object> causes things to work as expected. (I tried it in 2.9.10 and 2.10.1)

public static class MyCollection {
	@JsonDeserialize(contentAs = MyItem.class)
	public List items;
}

public static class MyItem {
	public String name;
}

public static void main(String[] args) throws Exception {
	ObjectMapper objectMapper = new ObjectMapper();
	
	MyCollection myCollection = 
			objectMapper.readValue("{\"items\": [{\"name\":\"item1\"}]}", MyCollection.class);
	
	System.out.println(myCollection.items);
	System.out.println(myCollection.items.get(0).getClass());
}

[{name=item1}]
class java.util.LinkedHashMap

I initially encountered this while doing some dynamic class creation via a refineDeserializationType method in a custom AnnotationIntrospector which caused the type from the subclass to be lost.

@cowtowncoder
Copy link
Member

cowtowncoder commented Nov 26, 2019

Interesting... thank you for reporting this, I think it should work as expected so need to see what causes the issue.
Problem does not occur with List<?> either. I wonder if this is due to some optimization in TypeFactory...

@cowtowncoder
Copy link
Member

Ok, yes, content type is lost within TypeFactory.constructSpecializedType() somehow; difference being that raw List apparently has no type bindings (does not come from generic type).
Need to figure out how to fix: probably affects raw Map, Collection and Set too.

@cowtowncoder cowtowncoder modified the milestones: 2.10.0, 2.10.2 Nov 26, 2019
@cowtowncoder
Copy link
Member

Found an easy fix: changing order of checks to first handle collections, then skip non-generic types seems to work, at least for Map/Collection use cases.

@eduizquierdo
Copy link

@cowtowncoder, I'm hitting same issue but don't understand your solution, maybe because I'm new to jackson. Can you be more explicit or provide a code sample?

@cowtowncoder
Copy link
Member

@eduizquierdo There is a fix in Jackson 2.10.2 that should handle the original case. 2.10.2 was just relesed and is available for upgrade.
So no workarounds needed: just use the annotation as suggested, but make sure to use 2.10.2 not an older version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants