Skip to content

Commit

Permalink
354734 Update jackson to 2.16, adapt to behavioral changes
Browse files Browse the repository at this point in the history
(1) 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 also:
FasterXML/jackson-databind#4082
FasterXML/jackson-databind@d7e77c3

(2) Jackson adapted handling for untyped deserialization of floating
point numbers. Former implementations (before 2.15) deserialized
floating point numbers within lists as BigDecimal. Since issue 903 and
3751 numbers are deserialized into the smalles possible Java data type
(same behavior as plain numbers not within a JSON list element).
Therefore Scout DoCollectionDeserializer was adapted identically to
DoEntityDeserializer to enforce using BigDecimal for unknown (raw)
number deserialization.

See also:
FasterXML/jackson-core#903
FasterXML/jackson-core@4c957e3
and
FasterXML/jackson-databind#3751
FasterXML/jackson-databind@23ea48c
  • Loading branch information
paolobazzi committed Dec 4, 2023
1 parent 869a0da commit 83dee19
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,24 @@ public void testSerializeDeserialize_EntityWithCollectionRaw() throws Exception
assertEquals("TestItem2", attribute7.get(1).get(ScoutDataObjectModule.DEFAULT_TYPE_ATTRIBUTE_NAME));
}

@Test
public void testSerializeDeserialize_RawEntityWithDouble() throws Exception {
DoEntity entity = BEANS.get(DoEntity.class);
entity.put("attribute", 45.69);
String json = s_dataObjectMapper.writeValueAsString(entity);
DoEntity doMarshalled = s_dataObjectMapper.readValue(json, DoEntity.class);
assertEquals(new BigDecimal("45.69"), doMarshalled.get("attribute"));
}

@Test
public void testSerializeDeserialize_RawEntityWithDoubleList() throws Exception {
DoEntity entity = BEANS.get(DoEntity.class);
entity.put("attribute", List.of(45.69));
String json = s_dataObjectMapper.writeValueAsString(entity);
DoEntity doMarshalled = s_dataObjectMapper.readValue(json, DoEntity.class);
assertEquals(new BigDecimal("45.69"), doMarshalled.getList("attribute").get(0));
}

@Test
public void testSerializeDeserialize_TestMapDo() throws Exception {
TestMapDo mapDo = new TestMapDo();
Expand Down Expand Up @@ -3141,27 +3159,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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.eclipse.scout.rt.jackson.dataobject;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.function.Supplier;

Expand Down Expand Up @@ -74,10 +75,19 @@ protected ResolvedType resolveListElementType(JsonParser p) {
}
else {
// all JSON scalar values are deserialized as bound type (if available) and as fallback as raw object using default jackson typing
return ObjectUtility.nvl(m_collectionType.getBindings().getBoundType(0), TypeFactory.unknownType());
return ObjectUtility.nvl(m_collectionType.getBindings().getBoundType(0), resolveFallbackListElementType(p));
}
}

protected ResolvedType resolveFallbackListElementType(JsonParser p) {
if (p.getCurrentToken() == JsonToken.VALUE_NUMBER_FLOAT) {
// deserialize floating point numbers as BigDecimal
return TypeFactory.defaultInstance().constructType(BigDecimal.class);
}
// JSON scalar values are deserialized as raw object using default jackson typing
return TypeFactory.unknownType();
}

@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
return deserialize(p, ctxt);
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.scout.rt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<jetty.version>10.0.15</jetty.version>
<slf4j.version>2.0.7</slf4j.version>
<logback.version>1.3.8</logback.version>
<jackson.version>2.14.0</jackson.version>
<jackson.version>2.16.0</jackson.version>
<io.netty-version>4.1.94.Final</io.netty-version>
<apache.tika-version>2.6.0</apache.tika-version>
<batik.version>1.17</batik.version>
Expand Down

0 comments on commit 83dee19

Please sign in to comment.