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.

Exception when trying to @JsonSerialized a nested polymorphic field annotated with @JsonTypeInfo

See original GitHub issue

I have this field that is annotated as @StoredAsJson and it’s type is interface FilterValue. In FilterValue, I have @JsonTypeInfo and @JsonSubTypes configured properly, also having like classes StringValue and NumberValue implementing that interface.

The serialization works fine, but it’s failing to deserialize the values back from the database. When I try to fetch the object from the database with my DAO I get the following error:

com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (VALUE_STRING), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class io.github.rscarvalho.rosettatest.data.FilterValue
 at [Source: {"id":0,"name":"filter1","filterValue":"{\"@type\":\"StringValue\",\"value\":\"my value\"}"}; line: 1, column: 25] (through reference chain: io.github.rscarvalho.rosettatest.data.Filter["filterValue"])

I’ve set up a repository with a small test to reproduce the issue. You can see the original error message into the Travis build that failed the same way as my local tests.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
rscarvalhocommented, May 3, 2016

@ldriscoll when you configure use = JsonTypeInfo.Id.NAME on @JsonTypeInfo it does only records the simple class name.

From what I understand Jackson should be picking up which class to use based on the configured @JsonSubTypes

0reactions
jhabercommented, Mar 1, 2018

Confirmed that this is fixed by 3.11.8, although some of the tests seemed to have logical errors. With this patch the tests pass:

diff --git a/pom.xml b/pom.xml
index dafc98a..d3eeb43 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,7 +9,7 @@
   <version>1.0-SNAPSHOT</version>
 
   <properties>
-    <rosetta.version>3.10.7</rosetta.version>
+    <rosetta.version>3.11.8</rosetta.version>
     <jdbi.version>2.73</jdbi.version>
     <jackson.version>2.5.4</jackson.version>
     <h2.version>1.4.191</h2.version>
diff --git a/src/test/java/io/github/rscarvalho/rosettatest/dao/FilterDaoTest.java b/src/test/java/io/github/rscarvalho/rosettatest/dao/FilterDaoTest.java
index 32ae0fe..28de220 100644
--- a/src/test/java/io/github/rscarvalho/rosettatest/dao/FilterDaoTest.java
+++ b/src/test/java/io/github/rscarvalho/rosettatest/dao/FilterDaoTest.java
@@ -18,7 +18,6 @@ import com.hubspot.rosetta.jdbi.RosettaObjectMapperOverride;
 import io.github.rscarvalho.rosettatest.JsonUtils;
 import io.github.rscarvalho.rosettatest.data.Filter;
 import io.github.rscarvalho.rosettatest.data.FilterValue;
-import io.github.rscarvalho.rosettatest.data.NumberValue;
 import io.github.rscarvalho.rosettatest.data.StringValue;
 
 public class FilterDaoTest {
@@ -99,7 +98,8 @@ public class FilterDaoTest {
     assertThat(actualFilter).isNotNull();
     assertThat(actualFilter.getId()).isEqualTo(id);
     assertThat(actualFilter.getName()).isEqualTo("filter1");
-    assertThat(actualFilter.getFilterValue()).isInstanceOf(NumberValue.class);
+    assertThat(actualFilter.getFilterValue()).isInstanceOf(StringValue.class);
+    assertThat(actualFilter.getFilterValue().getValue()).isEqualTo("my value");
   }
 
   @Test
@@ -119,7 +119,8 @@ public class FilterDaoTest {
     assertThat(actualFilter).isNotNull();
     assertThat(actualFilter.getId()).isEqualTo(id);
     assertThat(actualFilter.getName()).isEqualTo("filter1");
-    assertThat(actualFilter.getFilterValue()).isInstanceOf(NumberValue.class);
+    assertThat(actualFilter.getFilterValue()).isInstanceOf(StringValue.class);
+    assertThat(actualFilter.getFilterValue().getValue()).isEqualTo("my value");
   }
 
   @Test
@@ -140,6 +141,7 @@ public class FilterDaoTest {
     assertThat(actualFilter).isNotNull();
     assertThat(actualFilter.getId()).isEqualTo(id);
     assertThat(actualFilter.getName()).isEqualTo("filter1");
-    assertThat(actualFilter.getFilterValue()).isInstanceOf(NumberValue.class);
+    assertThat(actualFilter.getFilterValue()).isInstanceOf(StringValue.class);
+    assertThat(actualFilter.getFilterValue().getValue()).isEqualTo("my value");
   }
 }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson deserializing nested polymorphic type - Stack Overflow
Your definitions are messed up -- you are trying to use two type identifiers, type name AND class. This does not make any...
Read more >
Using @JsonTypeInfo annotation to handle polymorphic types
In cases where polymorphic types are persisted to JSON, there's no way for Jackson ... Let's fix about exception by using this annotation:...
Read more >
Serialization with Jackson - Documentation - Akka
When nested fields or collections are of polymorphic type the ... the type must be listed with @JsonTypeInfo and @JsonSubTypes annotations.
Read more >
JSON serialization - Immutables
You can use @JsonProperty to customize JSON field names. ... in the corresponding type (directly annotated and all nested immutable values) or package....
Read more >
Serialization for Nested Polymorphic Types with FasterXML ...
I have polymorphic types defined as below: import com.fasterxml.jackson.annotation._ @JsonTypeInfo(use = JsonTypeInfo.Id.NA…
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