Skip to content

SimpleModule not registered due to getTypeId() returning an empty string #5063

@seadbrane

Description

@seadbrane

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

I think the changes made for #3110 have resulted in some unintended behavior.

Specifically, the new SimpleModule(Version) constructor calls this(version.getArtifactId(), version); which sets _hasExplicitName = true even though no explicit name was passed in. And since getArtifactId() returns "" instead of null, this results in getTypeId() returning the same "" for distinct modules.

I think either _hasExplicitName name should not be set to true when it is not explicitly passed in, or _name should be set to null if getArtifactId() returns empty string. The former is probably easier since there validation in ObjectMapper to ensure getModuleName() is not null, although it doesn't really seem correct to implicitly set name to an empty string - especially when it can also result in getTypeId() being an empty string.

Here is a simple test case:

  public class Module1 extends SimpleModule {
        public Module1() {
            super(Version.unknownVersion());
        }
    };
    
    public class Module2 extends SimpleModule {
        public Module2() {
            super(Version.unknownVersion());
        }
    };
    
    @Test
    public void test() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new Module1());
        mapper.registerModule(new Module2());
        Set<Object> modules = mapper.getRegisteredModuleIds();
        Assert.assertEquals(2, modules.size());
    }

Version Information

No response

Reproduction

<-- Any of the following

  1. Brief code sample/snippet: include here in preformatted/code section
  2. Longer example stored somewhere else (diff repo, snippet), add a link
  3. Textual explanation: include here
    -->
// Your code here

Expected behavior

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.19Issues planned at 2.19 or later

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions