From 2507ed51d2b4bac414eedb2f2d795c67069ed71a Mon Sep 17 00:00:00 2001 From: Sascha Date: Thu, 11 Jun 2026 10:13:41 +0200 Subject: [PATCH 1/2] implement issue #6168 --- .../core/json/jackson/v3/DatabindCodec.java | 8 ++++- .../jackson/v3/MapperConfigurationTest.java | 35 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 vertx-core/src/test/java/io/vertx/tests/json/jackson/v3/MapperConfigurationTest.java diff --git a/vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java b/vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java index 9578fa2f5c6..43a809f1c60 100644 --- a/vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java +++ b/vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java @@ -17,6 +17,7 @@ import tools.jackson.core.StreamWriteFeature; import tools.jackson.core.type.TypeReference; import tools.jackson.databind.ObjectMapper; +import tools.jackson.databind.cfg.MapperBuilder; import tools.jackson.databind.json.JsonMapper; import io.netty.buffer.ByteBufInputStream; import io.vertx.core.buffer.Buffer; @@ -33,13 +34,14 @@ import java.io.Writer; import java.util.List; import java.util.Map; +import java.util.function.Function; /** * @author Julien Viet */ public class DatabindCodec extends JacksonCodec { - private static final ObjectMapper mapper = JsonMapper + private static ObjectMapper mapper = JsonMapper .builder(JacksonCodec.factory) .addModule(new VertxModule()) .build(); @@ -51,6 +53,10 @@ public static ObjectMapper mapper() { return mapper; } + public static void updateMapper(Function, ObjectMapper> f) { + mapper = f.apply(mapper().rebuild()); + } + @Override public T fromValue(Object json, Class clazz) { T value = DatabindCodec.mapper.convertValue(json, clazz); diff --git a/vertx-core/src/test/java/io/vertx/tests/json/jackson/v3/MapperConfigurationTest.java b/vertx-core/src/test/java/io/vertx/tests/json/jackson/v3/MapperConfigurationTest.java new file mode 100644 index 00000000000..681c0b172b3 --- /dev/null +++ b/vertx-core/src/test/java/io/vertx/tests/json/jackson/v3/MapperConfigurationTest.java @@ -0,0 +1,35 @@ +package io.vertx.tests.json.jackson.v3; + +import io.vertx.core.json.jackson.v3.DatabindCodec; +import org.junit.Test; +import tools.jackson.core.JacksonException; +import tools.jackson.databind.DeserializationFeature; +import tools.jackson.databind.ObjectMapper; + +import static org.junit.Assert.assertThrows; + +public class MapperConfigurationTest { + + public static class User { + private int age; + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + } + + @Test + public void testConfigurationChanges() { + final ObjectMapper oldMapper = DatabindCodec.mapper(); + assertThrows(JacksonException.class, () -> oldMapper.readValue("{\"age\": null}", User.class)); + + DatabindCodec.updateMapper(builder -> builder.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES).build()); + + final ObjectMapper newMapper = DatabindCodec.mapper(); + newMapper.readValue("{\"age\": null}", User.class); + } +} From 2f7365da7c38e6130d0278f1bebe3453c02d6be6 Mon Sep 17 00:00:00 2001 From: Sascha Date: Wed, 8 Jul 2026 15:33:20 +0200 Subject: [PATCH 2/2] update code --- .../io/vertx/core/json/jackson/v3/DatabindCodec.java | 11 ++++++++--- .../json/jackson/v3/MapperConfigurationTest.java | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java b/vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java index 43a809f1c60..4e492b33c71 100644 --- a/vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java +++ b/vertx-core/src/main/java21/io/vertx/core/json/jackson/v3/DatabindCodec.java @@ -27,7 +27,6 @@ import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; @@ -41,7 +40,7 @@ */ public class DatabindCodec extends JacksonCodec { - private static ObjectMapper mapper = JsonMapper + private static volatile ObjectMapper mapper = JsonMapper .builder(JacksonCodec.factory) .addModule(new VertxModule()) .build(); @@ -53,7 +52,13 @@ public static ObjectMapper mapper() { return mapper; } - public static void updateMapper(Function, ObjectMapper> f) { + /** + * this creates a @{@link MapperBuilder} from the already configured @{@link ObjectMapper} + * this builder is passed to the function which returns a new mapper + * + * @param f update mapper function + */ + public static void rebuildMapper(Function, ObjectMapper> f) { mapper = f.apply(mapper().rebuild()); } diff --git a/vertx-core/src/test/java/io/vertx/tests/json/jackson/v3/MapperConfigurationTest.java b/vertx-core/src/test/java/io/vertx/tests/json/jackson/v3/MapperConfigurationTest.java index 681c0b172b3..517e7f785ed 100644 --- a/vertx-core/src/test/java/io/vertx/tests/json/jackson/v3/MapperConfigurationTest.java +++ b/vertx-core/src/test/java/io/vertx/tests/json/jackson/v3/MapperConfigurationTest.java @@ -27,7 +27,7 @@ public void testConfigurationChanges() { final ObjectMapper oldMapper = DatabindCodec.mapper(); assertThrows(JacksonException.class, () -> oldMapper.readValue("{\"age\": null}", User.class)); - DatabindCodec.updateMapper(builder -> builder.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES).build()); + DatabindCodec.rebuildMapper(builder -> builder.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES).build()); final ObjectMapper newMapper = DatabindCodec.mapper(); newMapper.readValue("{\"age\": null}", User.class);