Skip to content

Commit

Permalink
Minor reversion wrt #3110: limit changes to SimpleModule registration…
Browse files Browse the repository at this point in the history
… id handling
  • Loading branch information
cowtowncoder committed Jun 16, 2021
1 parent 43ab288 commit 609ad49
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public class SimpleModule
public SimpleModule() {
// can't chain when making reference to 'this'
// note: generate different name for direct instantiation, sub-classing
_name = (getClass() == SimpleModule.class) ?
"SimpleModule-"+System.identityHashCode(this)
_name = (getClass() == SimpleModule.class)
? "SimpleModule-"+System.identityHashCode(this)
: getClass().getName();
_version = Version.unknownVersion();
// 07-Jun-2021, tatu: [databind#3110] Not passed explicitly so...
Expand Down Expand Up @@ -198,14 +198,22 @@ public SimpleModule(String name, Version version,
@Override
public Object getTypeId()
{
// 07-Jun-2021, tatu: [databind#3110] Only return Type Id if name
// was explicitly given
// 07-Jun-2021, tatu: [databind#3110] Return Type Id if name was
// explicitly given
if (_hasExplicitName) {
return _name;
}
// ...otherwise give no type id, even for sub-classes (sub-classes are
// welcome to override this method of course)
return null;
// Otherwise behavior same as with 2.12: no registration id for "throw-away"
// instances (to avoid bogus conflicts if user just instantiates SimpleModule)

// Note: actually... always returning `supet.getTypeId()` should be fine since
// that would return generated id? Let's do that actually.
if (getClass() == SimpleModule.class) {
return _name;
}
// And for what it is worth, this should usually do the same and we could
// in fact always just return `_name`. But leaving as-is for now.
return super.getTypeId();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,20 @@ public void testGetRegisteredModules()
mapper = new ObjectMapper();
assertEquals(0, mapper.getRegisteredModuleIds().size());

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

// But named ones are
// And named ones retain their name
mapper = JsonMapper.builder()
.addModule(new SimpleModule("VerySpecialModule"))
.build();
Expand Down

0 comments on commit 609ad49

Please sign in to comment.