Skip to content

Commit 76fb342

Browse files
marcospassoscowtowncoder
authored andcommitted
Add support for module bundles (#2432)
Add support for module bundles (#2432)
1 parent b94a732 commit 76fb342

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/main/java/com/fasterxml/jackson/databind/Module.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.fasterxml.jackson.databind.ser.Serializers;
1616
import com.fasterxml.jackson.databind.type.TypeFactory;
1717
import com.fasterxml.jackson.databind.type.TypeModifier;
18+
import java.util.Collections;
1819

1920
/**
2021
* Simple interface for extensions that can be registered with {@link ObjectMapper}
@@ -73,6 +74,17 @@ public Object getTypeId() {
7374
* using callback methods passed-in context object exposes.
7475
*/
7576
public abstract void setupModule(SetupContext context);
77+
78+
/**
79+
* Returns the list of dependent modules.
80+
*
81+
* It is called to let modules register other modules as dependencies.
82+
*
83+
* @since 2.10
84+
*/
85+
public Iterable<? extends Module> getDependencies() {
86+
return Collections.emptyList();
87+
}
7688

7789
/*
7890
/**********************************************************

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,8 @@ public ObjectMapper registerModule(Module module)
796796
throw new IllegalArgumentException("Module without defined version");
797797
}
798798

799+
registerModules(module.getDependencies());
800+
799801
// And then call registration
800802
module.setupModule(new Module.SetupContext()
801803
{
@@ -961,6 +963,7 @@ public void setNamingStrategy(PropertyNamingStrategy naming) {
961963
setPropertyNamingStrategy(naming);
962964
}
963965
});
966+
964967
return this;
965968
}
966969

src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.databind;
22

3+
import com.fasterxml.jackson.databind.module.SimpleModule;
34
import java.io.*;
45
import java.util.*;
56

@@ -410,4 +411,41 @@ public void testDataInputViaMapper() throws Exception
410411
.readTree(input);
411412
assertNotNull(n);
412413
}
414+
415+
public void testRegisterDependentModules() {
416+
ObjectMapper objectMapper = new ObjectMapper();
417+
418+
final SimpleModule secondModule = new SimpleModule() {
419+
@Override
420+
public Object getTypeId() {
421+
return "second";
422+
}
423+
};
424+
425+
final SimpleModule thirdModule = new SimpleModule() {
426+
@Override
427+
public Object getTypeId() {
428+
return "third";
429+
}
430+
};
431+
432+
final SimpleModule firstModule = new SimpleModule() {
433+
@Override
434+
public Iterable<? extends Module> getDependencies() {
435+
return Arrays.asList(secondModule, thirdModule);
436+
}
437+
438+
@Override
439+
public Object getTypeId() {
440+
return "first";
441+
}
442+
};
443+
444+
objectMapper.registerModule(firstModule);
445+
446+
assertEquals(
447+
new HashSet<>(Arrays.asList("first", "second", "third")),
448+
objectMapper.getRegisteredModuleIds()
449+
);
450+
}
413451
}

0 commit comments

Comments
 (0)