Skip to content

Commit 4539a55

Browse files
committed
Fix #3110
1 parent 2a7600a commit 4539a55

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Project: jackson-databind
3131
`JsonToken.VALUE_EMBEDDED_OBJECT`
3232
#3099: Optimize "AnnotatedConstructor.call()" case by passing explicit null
3333
#3101: Add AnnotationIntrospector.XmlExtensions interface for decoupling javax dependencies
34+
#3110: Custom SimpleModule not included in list returned by ObjectMapper.getRegisteredModuleIds()
35+
after registration
36+
(reported by dkindler@github)
3437
#3117: Use more limiting default visibility settings for JDK types (java.*, javax.*)
3538
#3122: Deep merge for `JsonNode` using `ObjectReader.readTree()`
3639
(reported by Eric S)

src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java

+30-10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public class SimpleModule
4545
protected final String _name;
4646
protected final Version _version;
4747

48+
/**
49+
* Flag that indicates whether module was given an explicit name
50+
* or not. Distinction is used to determine whether method
51+
* {@link #getTypeId()} should return name (yes, if explicit) or
52+
* {@code null} (if no explicit name was passed).
53+
*
54+
* @since 2.13
55+
*/
56+
protected final boolean _hasExplicitName;
57+
4858
protected SimpleSerializers _serializers = null;
4959
protected SimpleDeserializers _deserializers = null;
5060

@@ -109,8 +119,10 @@ public SimpleModule() {
109119
"SimpleModule-"+System.identityHashCode(this)
110120
: getClass().getName();
111121
_version = Version.unknownVersion();
122+
// 07-Jun-2021, tatu: [databind#3110] Not passed explicitly so...
123+
_hasExplicitName = false;
112124
}
113-
125+
114126
/**
115127
* Convenience constructor that will default version to
116128
* {@link Version#unknownVersion()}.
@@ -121,13 +133,12 @@ public SimpleModule(String name) {
121133

122134
/**
123135
* Convenience constructor that will use specified Version,
124-
* including name from {@link Version#getArtifactId()}
136+
* including name from {@link Version#getArtifactId()}.
125137
*/
126138
public SimpleModule(Version version) {
127-
_name = version.getArtifactId();
128-
_version = version;
139+
this(version.getArtifactId(), version);
129140
}
130-
141+
131142
/**
132143
* Constructor to use for actual reusable modules.
133144
* ObjectMapper may use name as identifier to notice attempts
@@ -140,6 +151,8 @@ public SimpleModule(Version version) {
140151
public SimpleModule(String name, Version version) {
141152
_name = name;
142153
_version = version;
154+
// 07-Jun-2021, tatu: [databind#3110] Is passed explicitly (may be `null`)
155+
_hasExplicitName = true;
143156
}
144157

145158
/**
@@ -166,6 +179,8 @@ public SimpleModule(String name, Version version,
166179
List<JsonSerializer<?>> serializers)
167180
{
168181
_name = name;
182+
// 07-Jun-2021, tatu: [databind#3110] Is passed explicitly (may be `null`)
183+
_hasExplicitName = true;
169184
_version = version;
170185
if (deserializers != null) {
171186
_deserializers = new SimpleDeserializers(deserializers);
@@ -181,13 +196,18 @@ public SimpleModule(String name, Version version,
181196
* but class name (default impl) for sub-classes.
182197
*/
183198
@Override
184-
public Object getTypeId() {
185-
if (getClass() == SimpleModule.class) {
186-
return null;
199+
public Object getTypeId()
200+
{
201+
// 07-Jun-2021, tatu: [databind#3110] Only return Type Id if name
202+
// was explicitly given
203+
if (_hasExplicitName) {
204+
return _name;
187205
}
188-
return super.getTypeId();
206+
// ...otherwise give no type id, even for sub-classes (sub-classes are
207+
// welcome to override this method of course)
208+
return null;
189209
}
190-
210+
191211
/*
192212
/**********************************************************
193213
/* Simple setters to allow overriding

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

+24-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.fasterxml.jackson.core.*;
99
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
1010
import com.fasterxml.jackson.databind.*;
11-
11+
import com.fasterxml.jackson.databind.json.JsonMapper;
1212
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
1313
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
1414

@@ -285,7 +285,12 @@ public void testSimpleEnumDeserializer() throws Exception
285285
public void testMultipleModules() throws Exception
286286
{
287287
MySimpleModule mod1 = new MySimpleModule("test1", Version.unknownVersion());
288+
assertEquals("test1", mod1.getModuleName());
289+
// 07-Jun-2021, tatu: as per [databind#3110]:
290+
assertEquals("test1", mod1.getTypeId());
288291
SimpleModule mod2 = new SimpleModule("test2", Version.unknownVersion());
292+
assertEquals("test2", mod2.getModuleName());
293+
assertEquals("test2", mod2.getTypeId());
289294
mod1.addSerializer(SimpleEnum.class, new SimpleEnumSerializer());
290295
mod1.addDeserializer(CustomBean.class, new CustomBeanDeserializer());
291296

@@ -315,10 +320,10 @@ public void testGetRegisteredModules()
315320
MySimpleModule mod1 = new MySimpleModule("test1", Version.unknownVersion());
316321
AnotherSimpleModule mod2 = new AnotherSimpleModule("test2", Version.unknownVersion());
317322

318-
ObjectMapper mapper = new ObjectMapper();
319-
320-
mapper.registerModule(mod1);
321-
mapper.registerModule(mod2);
323+
ObjectMapper mapper = JsonMapper.builder()
324+
.addModule(mod1)
325+
.addModule(mod2)
326+
.build();
322327

323328
Set<Object> registeredModuleIds = mapper.getRegisteredModuleIds();
324329
assertEquals(2, registeredModuleIds.size());
@@ -328,6 +333,20 @@ public void testGetRegisteredModules()
328333
// 01-Jul-2019, [databind#2374]: verify empty list is fine
329334
mapper = new ObjectMapper();
330335
assertEquals(0, mapper.getRegisteredModuleIds().size());
336+
337+
// 07-Jun-2021, tatu [databind#3110] Casual SimpleModules not returned
338+
// as registered
339+
mapper = JsonMapper.builder()
340+
.addModule(new SimpleModule())
341+
.build();
342+
assertEquals(0, mapper.getRegisteredModuleIds().size());
343+
344+
// But named ones are
345+
mapper = JsonMapper.builder()
346+
.addModule(new SimpleModule("VerySpecialModule"))
347+
.build();
348+
assertEquals(Collections.singleton("VerySpecialModule"),
349+
mapper.getRegisteredModuleIds());
331350
}
332351

333352
/*

0 commit comments

Comments
 (0)