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

354734 Update jackson to 2.16, adapt to behavioral changes #807

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -124,7 +124,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