Skip to content

Commit 609ad49

Browse files
committed
Minor reversion wrt #3110: limit changes to SimpleModule registration id handling
1 parent 43ab288 commit 609ad49

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ public class SimpleModule
115115
public SimpleModule() {
116116
// can't chain when making reference to 'this'
117117
// note: generate different name for direct instantiation, sub-classing
118-
_name = (getClass() == SimpleModule.class) ?
119-
"SimpleModule-"+System.identityHashCode(this)
118+
_name = (getClass() == SimpleModule.class)
119+
? "SimpleModule-"+System.identityHashCode(this)
120120
: getClass().getName();
121121
_version = Version.unknownVersion();
122122
// 07-Jun-2021, tatu: [databind#3110] Not passed explicitly so...
@@ -198,14 +198,22 @@ public SimpleModule(String name, Version version,
198198
@Override
199199
public Object getTypeId()
200200
{
201-
// 07-Jun-2021, tatu: [databind#3110] Only return Type Id if name
202-
// was explicitly given
201+
// 07-Jun-2021, tatu: [databind#3110] Return Type Id if name was
202+
// explicitly given
203203
if (_hasExplicitName) {
204204
return _name;
205205
}
206-
// ...otherwise give no type id, even for sub-classes (sub-classes are
207-
// welcome to override this method of course)
208-
return null;
206+
// Otherwise behavior same as with 2.12: no registration id for "throw-away"
207+
// instances (to avoid bogus conflicts if user just instantiates SimpleModule)
208+
209+
// Note: actually... always returning `supet.getTypeId()` should be fine since
210+
// that would return generated id? Let's do that actually.
211+
if (getClass() == SimpleModule.class) {
212+
return _name;
213+
}
214+
// And for what it is worth, this should usually do the same and we could
215+
// in fact always just return `_name`. But leaving as-is for now.
216+
return super.getTypeId();
209217
}
210218

211219
/*

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,20 @@ public void testGetRegisteredModules()
334334
mapper = new ObjectMapper();
335335
assertEquals(0, mapper.getRegisteredModuleIds().size());
336336

337-
// 07-Jun-2021, tatu [databind#3110] Casual SimpleModules not returned
338-
// as registered
337+
// 07-Jun-2021, tatu [databind#3110] Casual SimpleModules ARE returned
338+
// too!
339339
mapper = JsonMapper.builder()
340340
.addModule(new SimpleModule())
341341
.build();
342-
assertEquals(0, mapper.getRegisteredModuleIds().size());
342+
assertEquals(1, mapper.getRegisteredModuleIds().size());
343+
Object id = mapper.getRegisteredModuleIds().iterator().next();
344+
assertTrue(id instanceof String);
345+
if (!id.toString().startsWith("SimpleModule-")) {
346+
fail("SimpleModule registration id should start with 'SimpleModule-', does not: ["
347+
+id+"]");
348+
}
343349

344-
// But named ones are
350+
// And named ones retain their name
345351
mapper = JsonMapper.builder()
346352
.addModule(new SimpleModule("VerySpecialModule"))
347353
.build();

0 commit comments

Comments
 (0)