Skip to content

Commit

Permalink
354734 Update jackson to 2.16, adapt Java Optional handling to change
Browse files Browse the repository at this point in the history
Jackson adapted handling for Optional serialization providing a custom
exception message if Optional is being serialized. Scout does not
support serialization of Java Optional, therefore adapt test case.

See
FasterXML/jackson-databind#4082
FasterXML/jackson-databind@d7e77c3
  • Loading branch information
paolobazzi committed Nov 29, 2023
1 parent a547eb9 commit f1b5621
Showing 1 changed file with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3141,27 +3141,28 @@ public void testSerializeDeserializeThrowable() throws Exception {
assertArrayEquals(exception.getStackTrace(), marshalled.getException().getStackTrace());
}

/**
* {@link Optional} is currently not serializable/deserializable using Scout Jackson implementation.
*/
@Test
public void testSerializeDeserializeOptionalDo() throws Exception {
@SuppressWarnings("unchecked")
TestOptionalDo optional = BEANS.get(TestOptionalDo.class)
.withOptString(Optional.ofNullable(null))
.withOptStringList(Optional.empty(), Optional.ofNullable("foo"));
String json = s_dataObjectMapper.writeValueAsString(optional);

// currently serializable using Scout Jackson implementation, but without values, e.g. useless!
assertJsonEquals("TestOptionalDo.json", json);

// currently not deserializable using Scout Jackson implementation
.withOptString(Optional.empty())
.withOptStringList(Optional.empty(), Optional.of("foo"));

// Expect:
// com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 optional type `java.util.Optional<java.lang.String>`
// not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jdk8" to enable handling
JsonMappingException writeException = assertThrows(JsonMappingException.class, () -> s_dataObjectMapper.writeValueAsString(optional));
assertTrue("expected InvalidDefinitionException, got " + writeException, writeException instanceof InvalidDefinitionException);

// Expect:
// com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 optional type `java.util.Optional<java.lang.String>`
// not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jdk8" to enable handling
String json = readResourceAsString("TestOptionalDo.json");
JsonMappingException exception = assertThrows(JsonMappingException.class, () -> s_dataObjectMapper.readValue(json, TestOptionalDo.class));

// TODO [23.1] pbz remove when JDK 11 is no longer supported
if ("11".equals(System.getProperty("java.specification.version"))) {
assertTrue("expected cause UnrecognizedPropertyException, got " + exception.getCause(), exception.getCause() instanceof UnrecognizedPropertyException);
}
else {
assertTrue("expected cause InvalidDefinitionException, got " + exception.getCause(), exception.getCause() instanceof InvalidDefinitionException);
}
assertTrue("expected InvalidDefinitionException, got " + exception.getCause(), exception.getCause() instanceof InvalidDefinitionException);
}

@Test
Expand Down

0 comments on commit f1b5621

Please sign in to comment.