diff --git a/src/main/java/com/fasterxml/jackson/databind/Module.java b/src/main/java/com/fasterxml/jackson/databind/Module.java index 5884e6b3b5..d34bfc8ed6 100644 --- a/src/main/java/com/fasterxml/jackson/databind/Module.java +++ b/src/main/java/com/fasterxml/jackson/databind/Module.java @@ -15,6 +15,7 @@ import com.fasterxml.jackson.databind.ser.Serializers; import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.type.TypeModifier; +import java.util.Collections; /** * Simple interface for extensions that can be registered with {@link ObjectMapper} @@ -73,6 +74,17 @@ public Object getTypeId() { * using callback methods passed-in context object exposes. */ public abstract void setupModule(SetupContext context); + + /** + * Returns the list of dependent modules. + * + * It is called to let modules register other modules as dependencies. + * + * @since 2.10 + */ + public Iterable getDependencies() { + return Collections.emptyList(); + } /* /********************************************************** diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java index 9900a2b6e2..045d51edcc 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java @@ -776,6 +776,8 @@ public ObjectMapper registerModule(Module module) throw new IllegalArgumentException("Module without defined version"); } + registerModules(module.getDependencies()); + // And then call registration module.setupModule(new Module.SetupContext() { @@ -941,6 +943,7 @@ public void setNamingStrategy(PropertyNamingStrategy naming) { setPropertyNamingStrategy(naming); } }); + return this; } diff --git a/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java b/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java index a2e6d19f4e..35ad33a045 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java @@ -1,5 +1,6 @@ package com.fasterxml.jackson.databind; +import com.fasterxml.jackson.databind.module.SimpleModule; import java.io.*; import java.util.*; @@ -410,4 +411,41 @@ public void testDataInputViaMapper() throws Exception .readTree(input); assertNotNull(n); } + + public void testRegisterDependentModules() { + ObjectMapper objectMapper = new ObjectMapper(); + + final SimpleModule secondModule = new SimpleModule() { + @Override + public Object getTypeId() { + return "second"; + } + }; + + final SimpleModule thirdModule = new SimpleModule() { + @Override + public Object getTypeId() { + return "third"; + } + }; + + final SimpleModule firstModule = new SimpleModule() { + @Override + public Iterable getDependencies() { + return Arrays.asList(secondModule, thirdModule); + } + + @Override + public Object getTypeId() { + return "first"; + } + }; + + objectMapper.registerModule(firstModule); + + assertEquals( + new HashSet<>(Arrays.asList("first", "second", "third")), + objectMapper.getRegisteredModuleIds() + ); + } }