{"action":"created","issue":{"url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/676","labels_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/676/labels{/name}","comments_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/676/comments","events_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/676/events","html_url":"https://github.com/FasterXML/jackson-databind/issues/676","id":54053776,"number":676,"title":"Deserialization of class with generic collection inside depends on how is was deserialized first time","user":{"login":"lunaticare","id":947471,"avatar_url":"https://avatars.githubusercontent.com/u/947471?v=3","gravatar_id":"","url":"https://api.github.com/users/lunaticare","html_url":"https://github.com/lunaticare","followers_url":"https://api.github.com/users/lunaticare/followers","following_url":"https://api.github.com/users/lunaticare/following{/other_user}","gists_url":"https://api.github.com/users/lunaticare/gists{/gist_id}","starred_url":"https://api.github.com/users/lunaticare/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lunaticare/subscriptions","organizations_url":"https://api.github.com/users/lunaticare/orgs","repos_url":"https://api.github.com/users/lunaticare/repos","events_url":"https://api.github.com/users/lunaticare/events{/privacy}","received_events_url":"https://api.github.com/users/lunaticare/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/FasterXML/jackson-databind/milestones/32","html_url":"https://github.com/FasterXML/jackson-databind/milestones/2.5.4","labels_url":"https://api.github.com/repos/FasterXML/jackson-databind/milestones/32/labels","id":1084507,"number":32,"title":"2.5.4","description":null,"creator":{"login":"cowtowncoder","id":55065,"avatar_url":"https://avatars.githubusercontent.com/u/55065?v=3","gravatar_id":"","url":"https://api.github.com/users/cowtowncoder","html_url":"https://github.com/cowtowncoder","followers_url":"https://api.github.com/users/cowtowncoder/followers","following_url":"https://api.github.com/users/cowtowncoder/following{/other_user}","gists_url":"https://api.github.com/users/cowtowncoder/gists{/gist_id}","starred_url":"https://api.github.com/users/cowtowncoder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cowtowncoder/subscriptions","organizations_url":"https://api.github.com/users/cowtowncoder/orgs","repos_url":"https://api.github.com/users/cowtowncoder/repos","events_url":"https://api.github.com/users/cowtowncoder/events{/privacy}","received_events_url":"https://api.github.com/users/cowtowncoder/received_events","type":"User","site_admin":false},"open_issues":0,"closed_issues":3,"state":"open","created_at":"2015-04-27T05:42:53Z","updated_at":"2015-05-08T03:12:05Z","due_on":null,"closed_at":null},"comments":8,"created_at":"2015-01-12T13:34:40Z","updated_at":"2015-05-08T12:48:36Z","closed_at":"2015-05-08T03:12:05Z","body":"Hi everyone!\r\n\r\nAfter migration from Jackson 1.9.x to 2.x (the problem can be reproduced in 2.5.0) I've got a problem deserializing generic collections.\r\nHere's the test that documents the behavour.\r\nAm I doing something wrong?\r\n\r\n```java\r\npackage com.fasterxml.jackson.databind.test;\r\n\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\nimport com.fasterxml.jackson.databind.annotation.JsonSerialize;\r\nimport com.google.common.collect.ImmutableList;\r\nimport com.google.common.collect.ImmutableMap;\r\nimport org.json.JSONException;\r\nimport org.json.JSONObject;\r\nimport org.junit.Test;\r\nimport org.skyscreamer.jsonassert.JSONAssert;\r\n\r\nimport java.io.IOException;\r\nimport java.util.Date;\r\nimport java.util.Map;\r\n\r\nimport static org.junit.Assert.assertEquals;\r\nimport static org.junit.Assert.assertNotEquals;\r\n\r\n/**\r\n * Deserialization of class with generic collection inside depends on how is was deserialized first time.\r\n */\r\npublic class JacksonPolymorphicDeSerFailTest {\r\n\tprivate static final int TIMESTAMP = 123456;\r\n\tprivate final MapContainer originMap =\r\n\t\t\tnew MapContainer(ImmutableMap.<String, Object>of(\"DateValue\", new Date(TIMESTAMP)));\r\n\r\n\t/**\r\n\t * If the class was first deserialized as polymorphic field,\r\n\t * deserialization will fail at complex type.\r\n\t */\r\n\t@Test\r\n\tpublic void testFactorsSerializationFail() throws IOException, JSONException {\r\n\t\tObjectMapper mapper = new ObjectMapper();\r\n\r\n\t\t// incorrect\r\n\t\tMapContainer deserMapBad = createDeSerMapContainer(originMap, mapper);\r\n\t\tassertNotEquals(originMap, deserMapBad);\r\n\t\tassertEquals(ImmutableList.of(\"java.util.Date\", TIMESTAMP),\r\n\t\t\t\tdeserMapBad.getMap().get(\"DateValue\"));\r\n\r\n\t\t// incorrect again\r\n\t\tassertNotEquals(originMap, mapper.readValue(mapper.writeValueAsString(originMap),\r\n\t\t\t\tMapContainer.class));\r\n\t}\r\n\r\n\t/**\r\n\t * If the class was first deserialized as is,\r\n\t * deserialization will work correctly.\r\n\t */\r\n\t@Test\r\n\tpublic void testFactorsSerializationMagicGood() throws IOException, JSONException {\r\n\t\tObjectMapper mapper = new ObjectMapper();\r\n\t\t// commenting out the following statement will fail the test\r\n\t\tassertEquals(new MapContainer(ImmutableMap.<String, Object>of(\"1\", 1)),\r\n\t\t\t\tmapper.readValue(\r\n\t\t\t\t\t\tmapper.writeValueAsString(new MapContainer(ImmutableMap.<String, Object>of(\"1\", 1))),\r\n\t\t\t\t\t\tMapContainer.class));\r\n\r\n\t\tMapContainer deserMapGood = createDeSerMapContainer(originMap, mapper);\r\n\t\t// correct\r\n\t\tassertEquals(originMap, deserMapGood);\r\n\t\tassertEquals(new Date(TIMESTAMP), deserMapGood.getMap().get(\"DateValue\"));\r\n\r\n\t\t// correct again\r\n\t\tassertEquals(originMap, mapper.readValue(mapper.writeValueAsString(originMap), MapContainer.class));\r\n\t}\r\n\r\n\tprivate static MapContainer createDeSerMapContainer(MapContainer originMap, ObjectMapper mapper)\r\n\t\t\tthrows JSONException, IOException {\r\n\t\tPolymorphicValueWrapper result = new PolymorphicValueWrapper();\r\n\t\tresult.setValue(originMap);\r\n\t\tString json = mapper.writeValueAsString(result);\r\n\t\tSystem.out.println(\"Original map json: \" + json);\r\n\t\tJSONAssert.assertEquals(\"{\\\"value\\\":{\\\"@class\\\":\"\r\n\t\t\t\t\t\t+ \"\\\"com.fasterxml.jackson.databind.test.JacksonPolymorphicDeSerFailTest$MapContainer\\\",\"\r\n\t\t\t\t\t\t+ \"\\\"map\\\":{\\\"DateValue\\\":[\\\"java.util.Date\\\",123456]}}}\",\r\n\t\t\t\tnew JSONObject(json), true);\r\n\t\tPolymorphicValueWrapper deserializedResult = mapper.readValue(json, PolymorphicValueWrapper.class);\r\n\t\tSystem.out.println(\"Deserialized map json: \" + mapper.writeValueAsString(deserializedResult));\r\n\t\treturn (MapContainer) deserializedResult.getValue();\r\n\t}\r\n\r\n\t@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)\r\n\tpublic static class MapContainer {\r\n\t\t@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,\r\n\t\t\t\tinclude = JsonTypeInfo.As.PROPERTY,\r\n\t\t\t\tproperty = \"@class\")\r\n\t\tprivate Map<String, Object> map;\r\n\r\n\t\tpublic MapContainer() { }\r\n\r\n\t\tpublic MapContainer(Map<String, Object> map) {\r\n\t\t\tthis.map = map;\r\n\t\t}\r\n\r\n\t\tpublic Map<String, Object> getMap() {\r\n\t\t\treturn map;\r\n\t\t}\r\n\r\n\t\tpublic void setMap(Map<String, Object> map) {\r\n\t\t\tthis.map = map;\r\n\t\t}\r\n\r\n\t\t@Override\r\n\t\tpublic boolean equals(Object o) {\r\n\t\t\tif (this == o) {\r\n\t\t\t\treturn true;\r\n\t\t\t}\r\n\t\t\tif (o == null || getClass() != o.getClass()) {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\r\n\t\t\tMapContainer that = (MapContainer) o;\r\n\r\n\t\t\tif (map != null ? !map.equals(that.map) : that.map != null) {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\r\n\t\t\treturn true;\r\n\t\t}\r\n\r\n\t\t@Override\r\n\t\tpublic int hashCode() {\r\n\t\t\treturn map != null ? map.hashCode() : 0;\r\n\t\t}\r\n\r\n\t\t@Override\r\n\t\tpublic String toString() {\r\n\t\t\treturn \"MapContainer{\" +\r\n\t\t\t\t\t\"map=\" + map +\r\n\t\t\t\t\t'}';\r\n\t\t}\r\n\t}\r\n\r\n\t@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)\r\n\tpublic static class PolymorphicValueWrapper {\r\n\t\t@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,\r\n\t\t\t\tinclude = JsonTypeInfo.As.PROPERTY,\r\n\t\t\t\tproperty = \"@class\")\r\n\t\tprivate Object value;\r\n\r\n\t\tpublic Object getValue() {\r\n\t\t\treturn value;\r\n\t\t}\r\n\r\n\t\tpublic void setValue(Object value) {\r\n\t\t\tthis.value = value;\r\n\t\t}\r\n\t}\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/comments/100222598","html_url":"https://github.com/FasterXML/jackson-databind/issues/676#issuecomment-100222598","issue_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/676","id":100222598,"user":{"login":"lunaticare","id":947471,"avatar_url":"https://avatars.githubusercontent.com/u/947471?v=3","gravatar_id":"","url":"https://api.github.com/users/lunaticare","html_url":"https://github.com/lunaticare","followers_url":"https://api.github.com/users/lunaticare/followers","following_url":"https://api.github.com/users/lunaticare/following{/other_user}","gists_url":"https://api.github.com/users/lunaticare/gists{/gist_id}","starred_url":"https://api.github.com/users/lunaticare/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lunaticare/subscriptions","organizations_url":"https://api.github.com/users/lunaticare/orgs","repos_url":"https://api.github.com/users/lunaticare/repos","events_url":"https://api.github.com/users/lunaticare/events{/privacy}","received_events_url":"https://api.github.com/users/lunaticare/received_events","type":"User","site_admin":false},"created_at":"2015-05-08T12:48:36Z","updated_at":"2015-05-08T12:48:36Z","body":"@cowtowncoder thank you very much!"}}