This is an automated email from the ASF dual-hosted git repository.

fanningpj pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-pekko-http.git


The following commit(s) were added to refs/heads/main by this push:
     new 0d1f29cc8 pekko-http-jackson: upgrade jackson to 2.16 (#368)
0d1f29cc8 is described below

commit 0d1f29cc80a3d5f5bee04565fb4f21498f6d0a81
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Jan 8 10:22:06 2024 +0100

    pekko-http-jackson: upgrade jackson to 2.16 (#368)
    
    * jackson 2.16.0
    
    * javafmt
    
    * Update reference.conf
    
    * jackson 2.16.1
    
    * Update Jackson.java
    
    * add tests
    
    * rename config
---
 .scala-steward.conf                                |  4 +-
 .../http/javadsl/marshallers/jackson/Jackson.java  | 36 +++++++++++++--
 .../http-jackson/src/main/resources/reference.conf | 27 ++++++++++++
 .../javadsl/marshallers/jackson/JacksonTest.java   | 51 +++++++++++++++++++++-
 project/Dependencies.scala                         |  2 +-
 5 files changed, 113 insertions(+), 7 deletions(-)

diff --git a/.scala-steward.conf b/.scala-steward.conf
index d275ff8b4..d0fb9df99 100644
--- a/.scala-steward.conf
+++ b/.scala-steward.conf
@@ -15,8 +15,8 @@ updates.ignore = [
 
 updates.pin = [
   # pin to jackson version used in pekko-core 
-  { groupId = "com.fasterxml.jackson.core", version = "2.14." },
-  { groupId = "com.fasterxml.jackson.dataformat", version = "2.14." },
+  { groupId = "com.fasterxml.jackson.core", version = "2.16." },
+  { groupId = "com.fasterxml.jackson.dataformat", version = "2.16." },
   # https://github.com/akka/akka-http/pull/3995#issuecomment-1009951997
   { groupId = "org.scala-lang.modules", artifactId = "scala-xml", version = 
"1." },
   # https://github.com/akka/akka-http/pull/3996#issuecomment-1009953070
diff --git 
a/http-marshallers-java/http-jackson/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson/Jackson.java
 
b/http-marshallers-java/http-jackson/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson/Jackson.java
index bd2844464..54c5e8f28 100644
--- 
a/http-marshallers-java/http-jackson/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson/Jackson.java
+++ 
b/http-marshallers-java/http-jackson/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson/Jackson.java
@@ -15,23 +15,28 @@ package org.apache.pekko.http.javadsl.marshallers.jackson;
 
 import java.io.IOException;
 
+import com.typesafe.config.Config;
+import com.typesafe.config.ConfigFactory;
 import org.apache.pekko.http.javadsl.model.HttpEntity;
 import org.apache.pekko.http.javadsl.model.MediaTypes;
 import org.apache.pekko.http.javadsl.model.RequestEntity;
 import org.apache.pekko.http.javadsl.marshalling.Marshaller;
 import org.apache.pekko.http.javadsl.unmarshalling.Unmarshaller;
-
 import org.apache.pekko.http.scaladsl.model.ExceptionWithErrorInfo;
 import org.apache.pekko.http.scaladsl.model.ErrorInfo;
-
 import org.apache.pekko.util.ByteString;
+
+import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.StreamReadConstraints;
+import com.fasterxml.jackson.core.StreamWriteConstraints;
 import com.fasterxml.jackson.databind.MapperFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.json.JsonMapper;
 
 public class Jackson {
   private static final ObjectMapper defaultObjectMapper =
-      new ObjectMapper().enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
+      createMapper().enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
 
   /** INTERNAL API */
   public static class JacksonUnmarshallingException extends 
ExceptionWithErrorInfo {
@@ -86,4 +91,29 @@ public class Jackson {
       throw new JacksonUnmarshallingException(expectedType, e);
     }
   }
+
+  private static ObjectMapper createMapper() {
+    return 
createMapper(ConfigFactory.load().getConfig("pekko.http.marshallers.jackson"));
+  }
+
+  static ObjectMapper createMapper(final Config config) {
+    StreamReadConstraints streamReadConstraints =
+        StreamReadConstraints.builder()
+            .maxNestingDepth(config.getInt("read.max-nesting-depth"))
+            .maxNumberLength(config.getInt("read.max-number-length"))
+            .maxStringLength(config.getInt("read.max-string-length"))
+            .maxNameLength(config.getInt("read.max-name-length"))
+            .maxDocumentLength(config.getLong("read.max-document-length"))
+            .build();
+    StreamWriteConstraints streamWriteConstraints =
+        StreamWriteConstraints.builder()
+            .maxNestingDepth(config.getInt("write.max-nesting-depth"))
+            .build();
+    JsonFactory jsonFactory =
+        JsonFactory.builder()
+            .streamReadConstraints(streamReadConstraints)
+            .streamWriteConstraints(streamWriteConstraints)
+            .build();
+    return new JsonMapper(jsonFactory);
+  }
 }
diff --git 
a/http-marshallers-java/http-jackson/src/main/resources/reference.conf 
b/http-marshallers-java/http-jackson/src/main/resources/reference.conf
new file mode 100644
index 000000000..3ffbbba55
--- /dev/null
+++ b/http-marshallers-java/http-jackson/src/main/resources/reference.conf
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: Apache-2.0
+
+##################################
+# Pekko HTTP Jackson Config File #
+##################################
+
+# This is the reference config file that contains all the default settings.
+# Make your edits/overrides in your application.conf.
+
+pekko.http.marshallers.jackson {
+  read {
+    # see 
https://www.javadoc.io/static/com.fasterxml.jackson.core/jackson-core/2.16.1/com/fasterxml/jackson/core/StreamReadConstraints.html
+    # these defaults are the same as the defaults in `StreamReadConstraints`
+    max-nesting-depth = 1000
+    max-number-length = 1000
+    max-string-length = 20000000
+    max-name-length = 50000
+    # max-document-length of -1 means unlimited
+    max-document-length = -1
+  }
+
+  write {
+    # see 
https://www.javadoc.io/static/com.fasterxml.jackson.core/jackson-core/2.16.1/com/fasterxml/jackson/core/StreamWriteConstraints.html
+    # these defaults are the same as the defaults in `StreamWriteConstraints`
+    max-nesting-depth = 1000
+  }
+}
diff --git 
a/http-marshallers-java/http-jackson/src/test/java/org/apache/pekko/http/javadsl/marshallers/jackson/JacksonTest.java
 
b/http-marshallers-java/http-jackson/src/test/java/org/apache/pekko/http/javadsl/marshallers/jackson/JacksonTest.java
index 17532fdaf..7a4ebb122 100644
--- 
a/http-marshallers-java/http-jackson/src/test/java/org/apache/pekko/http/javadsl/marshallers/jackson/JacksonTest.java
+++ 
b/http-marshallers-java/http-jackson/src/test/java/org/apache/pekko/http/javadsl/marshallers/jackson/JacksonTest.java
@@ -15,6 +15,9 @@ package org.apache.pekko.http.javadsl.marshallers.jackson;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.StreamReadConstraints;
+import com.fasterxml.jackson.core.StreamWriteConstraints;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.typesafe.config.Config;
 import com.typesafe.config.ConfigFactory;
 
@@ -32,7 +35,7 @@ import java.util.concurrent.CompletionStage;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 public class JacksonTest extends JUnitRouteTest {
 
@@ -81,4 +84,50 @@ public class JacksonTest extends JUnitRouteTest {
     runRoute(route.seal(), HttpRequest.PUT("/").withEntity(invalidEntity))
         .assertEntity("The request content was malformed:\nCannot unmarshal 
JSON as SomeData");
   }
+
+  @Test
+  public void configStreamReadsConstraints() throws Exception {
+    final int maxNumLen = 987;
+    final int maxNameLen = 54321;
+    final int maxStringLen = 1234567;
+    final long maxDocLen = 123456789L;
+    final int maxNestingDepth = 5;
+    String configText =
+        "read.max-number-length="
+            + maxNumLen
+            + "\n"
+            + "read.max-name-length="
+            + maxNameLen
+            + "\n"
+            + "read.max-string-length="
+            + maxStringLen
+            + "\n"
+            + "read.max-document-length="
+            + maxDocLen
+            + "\n"
+            + "read.max-nesting-depth="
+            + maxNestingDepth;
+    Config config =
+        ConfigFactory.parseString(configText)
+            
.withFallback(ConfigFactory.load().getConfig("pekko.http.marshallers.jackson"));
+    ObjectMapper mapper = Jackson.createMapper(config);
+    StreamReadConstraints constraints = 
mapper.getFactory().streamReadConstraints();
+    assertEquals(maxNumLen, constraints.getMaxNumberLength());
+    assertEquals(maxNameLen, constraints.getMaxNameLength());
+    assertEquals(maxStringLen, constraints.getMaxStringLength());
+    assertEquals(maxDocLen, constraints.getMaxDocumentLength());
+    assertEquals(maxNestingDepth, constraints.getMaxNestingDepth());
+  }
+
+  @Test
+  public void configStreamWritesConstraints() throws Exception {
+    final int maxNestingDepth = 5;
+    String configText = "write.max-nesting-depth=" + maxNestingDepth;
+    Config config =
+        ConfigFactory.parseString(configText)
+            
.withFallback(ConfigFactory.load().getConfig("pekko.http.marshallers.jackson"));
+    ObjectMapper mapper = Jackson.createMapper(config);
+    StreamWriteConstraints constraints = 
mapper.getFactory().streamWriteConstraints();
+    assertEquals(maxNestingDepth, constraints.getMaxNestingDepth());
+  }
 }
diff --git a/project/Dependencies.scala b/project/Dependencies.scala
index 5c5be95d4..fdb26f527 100644
--- a/project/Dependencies.scala
+++ b/project/Dependencies.scala
@@ -18,7 +18,7 @@ import scala.language.implicitConversions
 object Dependencies {
   import DependencyHelpers._
 
-  val jacksonDatabindVersion = "2.14.3"
+  val jacksonDatabindVersion = "2.16.1"
   val jacksonXmlVersion = jacksonDatabindVersion
   val junitVersion = "4.13.2"
   val h2specVersion = "2.6.0"


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to