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..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 @@ -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; @@ -26,20 +27,20 @@ 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; 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 volatile ObjectMapper mapper = JsonMapper .builder(JacksonCodec.factory) .addModule(new VertxModule()) .build(); @@ -51,6 +52,16 @@ public static ObjectMapper mapper() { return mapper; } + /** + * 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()); + } + @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..517e7f785ed --- /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.rebuildMapper(builder -> builder.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES).build()); + + final ObjectMapper newMapper = DatabindCodec.mapper(); + newMapper.readValue("{\"age\": null}", User.class); + } +}