Skip to content

Commit

Permalink
Fix #3110
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 7, 2021
1 parent 2a7600a commit 4539a55
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Project: jackson-databind
`JsonToken.VALUE_EMBEDDED_OBJECT`
#3099: Optimize "AnnotatedConstructor.call()" case by passing explicit null
#3101: Add AnnotationIntrospector.XmlExtensions interface for decoupling javax dependencies
#3110: Custom SimpleModule not included in list returned by ObjectMapper.getRegisteredModuleIds()
after registration
(reported by dkindler@github)
#3117: Use more limiting default visibility settings for JDK types (java.*, javax.*)
#3122: Deep merge for `JsonNode` using `ObjectReader.readTree()`
(reported by Eric S)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public class SimpleModule
protected final String _name;
protected final Version _version;

/**
* Flag that indicates whether module was given an explicit name
* or not. Distinction is used to determine whether method
* {@link #getTypeId()} should return name (yes, if explicit) or
* {@code null} (if no explicit name was passed).
*
* @since 2.13
*/
protected final boolean _hasExplicitName;

protected SimpleSerializers _serializers = null;
protected SimpleDeserializers _deserializers = null;

Expand Down Expand Up @@ -109,8 +119,10 @@ public SimpleModule() {
"SimpleModule-"+System.identityHashCode(this)
: getClass().getName();
_version = Version.unknownVersion();
// 07-Jun-2021, tatu: [databind#3110] Not passed explicitly so...
_hasExplicitName = false;
}

/**
* Convenience constructor that will default version to
* {@link Version#unknownVersion()}.
Expand All @@ -121,13 +133,12 @@ public SimpleModule(String name) {

/**
* Convenience constructor that will use specified Version,
* including name from {@link Version#getArtifactId()}
* including name from {@link Version#getArtifactId()}.
*/
public SimpleModule(Version version) {
_name = version.getArtifactId();
_version = version;
this(version.getArtifactId(), version);
}

/**
* Constructor to use for actual reusable modules.
* ObjectMapper may use name as identifier to notice attempts
Expand All @@ -140,6 +151,8 @@ public SimpleModule(Version version) {
public SimpleModule(String name, Version version) {
_name = name;
_version = version;
// 07-Jun-2021, tatu: [databind#3110] Is passed explicitly (may be `null`)
_hasExplicitName = true;
}

/**
Expand All @@ -166,6 +179,8 @@ public SimpleModule(String name, Version version,
List<JsonSerializer<?>> serializers)
{
_name = name;
// 07-Jun-2021, tatu: [databind#3110] Is passed explicitly (may be `null`)
_hasExplicitName = true;
_version = version;
if (deserializers != null) {
_deserializers = new SimpleDeserializers(deserializers);
Expand All @@ -181,13 +196,18 @@ public SimpleModule(String name, Version version,
* but class name (default impl) for sub-classes.
*/
@Override
public Object getTypeId() {
if (getClass() == SimpleModule.class) {
return null;
public Object getTypeId()
{
// 07-Jun-2021, tatu: [databind#3110] Only return Type Id if name
// was explicitly given
if (_hasExplicitName) {
return _name;
}
return super.getTypeId();
// ...otherwise give no type id, even for sub-classes (sub-classes are
// welcome to override this method of course)
return null;
}

/*
/**********************************************************
/* Simple setters to allow overriding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.*;

import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

Expand Down Expand Up @@ -285,7 +285,12 @@ public void testSimpleEnumDeserializer() throws Exception
public void testMultipleModules() throws Exception
{
MySimpleModule mod1 = new MySimpleModule("test1", Version.unknownVersion());
assertEquals("test1", mod1.getModuleName());
// 07-Jun-2021, tatu: as per [databind#3110]:
assertEquals("test1", mod1.getTypeId());
SimpleModule mod2 = new SimpleModule("test2", Version.unknownVersion());
assertEquals("test2", mod2.getModuleName());
assertEquals("test2", mod2.getTypeId());
mod1.addSerializer(SimpleEnum.class, new SimpleEnumSerializer());
mod1.addDeserializer(CustomBean.class, new CustomBeanDeserializer());

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

ObjectMapper mapper = new ObjectMapper();

mapper.registerModule(mod1);
mapper.registerModule(mod2);
ObjectMapper mapper = JsonMapper.builder()
.addModule(mod1)
.addModule(mod2)
.build();

Set<Object> registeredModuleIds = mapper.getRegisteredModuleIds();
assertEquals(2, registeredModuleIds.size());
Expand All @@ -328,6 +333,20 @@ public void testGetRegisteredModules()
// 01-Jul-2019, [databind#2374]: verify empty list is fine
mapper = new ObjectMapper();
assertEquals(0, mapper.getRegisteredModuleIds().size());

// 07-Jun-2021, tatu [databind#3110] Casual SimpleModules not returned
// as registered
mapper = JsonMapper.builder()
.addModule(new SimpleModule())
.build();
assertEquals(0, mapper.getRegisteredModuleIds().size());

// But named ones are
mapper = JsonMapper.builder()
.addModule(new SimpleModule("VerySpecialModule"))
.build();
assertEquals(Collections.singleton("VerySpecialModule"),
mapper.getRegisteredModuleIds());
}

/*
Expand Down

0 comments on commit 4539a55

Please sign in to comment.