From c63f5fe883a823b1c3e54c2e09cbe6837a9b2721 Mon Sep 17 00:00:00 2001 From: Sascha Date: Thu, 11 Jun 2026 10:13:41 +0200 Subject: [PATCH] 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 dbb22da536a..fb12d700e74 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 @@ -15,6 +15,7 @@ import tools.jackson.core.JsonToken; 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; @@ -28,13 +29,14 @@ import java.io.InputStream; 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(); @@ -46,6 +48,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); + } +}