Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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<? extends Module> getDependencies() {
return Collections.emptyList();
}

/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -941,6 +943,7 @@ public void setNamingStrategy(PropertyNamingStrategy naming) {
setPropertyNamingStrategy(naming);
}
});

return this;
}

Expand Down
38 changes: 38 additions & 0 deletions src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.databind;

import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.*;
import java.util.*;

Expand Down Expand Up @@ -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<? extends Module> 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()
);
}
}