Skip to content

Commit 98068c7

Browse files
dinomitecowtowncoder
authored andcommitted
Accessor for module typeIds registered in an ObjectMapper (#2035)
Add accessor for registered modules in `ObjectMapper`
1 parent 69416ee commit 98068c7

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,19 @@ public ObjectMapper registerModules(Iterable<? extends Module> modules)
952952
}
953953
return this;
954954
}
955-
955+
956+
/**
957+
* The set of {@link Module} typeIds that are registered in this
958+
* ObjectMapper. By default the typeId for a module is it's full
959+
* class name (see {@link Module#getTypeId()}).
960+
*
961+
* @since 2.9.6
962+
*/
963+
public Set<Object> getRegisteredModuleIds()
964+
{
965+
return Collections.unmodifiableSet(_registeredModuleTypes);
966+
}
967+
956968
/**
957969
* Method for locating available methods, using JDK {@link ServiceLoader}
958970
* facility, along with module-provided SPI.

src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ public MySimpleModule(String name, Version version) {
146146
}
147147
}
148148

149+
/**
150+
* Test module that is different from MySimpleModule. Used to test registration
151+
* of multiple modules.
152+
*/
153+
protected static class AnotherSimpleModule extends SimpleModule
154+
{
155+
public AnotherSimpleModule(String name, Version version) {
156+
super(name, version);
157+
}
158+
}
159+
149160
protected static class ContextVerifierModule extends com.fasterxml.jackson.databind.Module
150161
{
151162
@Override
@@ -302,6 +313,21 @@ public void testMultipleModules() throws Exception
302313
assertSame(SimpleEnum.A, result);
303314
}
304315

316+
public void testGetRegisteredModules()
317+
{
318+
MySimpleModule mod1 = new MySimpleModule("test1", Version.unknownVersion());
319+
AnotherSimpleModule mod2 = new AnotherSimpleModule("test2", Version.unknownVersion());
320+
321+
ObjectMapper mapper = new ObjectMapper();
322+
mapper.registerModule(mod1);
323+
mapper.registerModule(mod2);
324+
325+
Set<Object> registeredModuleIds = mapper.getRegisteredModuleIds();
326+
assertEquals(2, registeredModuleIds.size());
327+
assertTrue(registeredModuleIds.contains(mod1.getTypeId()));
328+
assertTrue(registeredModuleIds.contains(mod2.getTypeId()));
329+
}
330+
305331
/*
306332
/**********************************************************
307333
/* Unit tests; other

0 commit comments

Comments
 (0)