Skip to content

Fixes #5063: generate distinct names (type ids) if Version has no artifact id #5076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Project: jackson-databind
(contributed by @pjfanning)
#5052: Minor bug in `FirstCharBasedValidator.forFirstNameRule()`: returns `null`
in non-default case
#5063: `SimpleModule` not registered due to `getTypeId()` returning an empty string
(reported by @seadbrane)
#5069: Add copy-constructor for `MappingIterator`
(contributed by @wrongwrong)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,19 @@ public SimpleModule() {
// note: generate different name for direct instantiation, sub-classing;
// this to avoid collision in former case while still addressing
// [databind#3110]
_name = (getClass() == SimpleModule.class)
? "SimpleModule-"+MODULE_ID_SEQ.getAndIncrement()
: getClass().getName();
_name = _generateName(getClass());
_version = Version.unknownVersion();
// 07-Jun-2021, tatu: [databind#3110] Not passed explicitly so...
_hasExplicitName = false;
}

// @since 2.19
private static String _generateName(Class<?> cls) {
return (cls == SimpleModule.class)
? "SimpleModule-"+MODULE_ID_SEQ.getAndIncrement()
: cls.getName();
}

/**
* Convenience constructor that will default version to
* {@link Version#unknownVersion()}.
Expand All @@ -148,10 +153,21 @@ public SimpleModule(String name) {

/**
* Convenience constructor that will use specified Version,
* including name from {@link Version#getArtifactId()}.
* including name from {@link Version#getArtifactId()}
* (if available -- if not, will generate).
*/
public SimpleModule(Version version) {
this(version.getArtifactId(), version);
// 07-Apr-2025, tatu: [databind#5063]: check that we actually have a name;
// if not, generate it
String maybeName = version.getArtifactId();
// Only considered explicit if name is non-null and not empty (i.e. not generated)
_hasExplicitName = (maybeName != null) && !maybeName.isEmpty();
if (_hasExplicitName) {
_name = maybeName;
} else {
_name = _generateName(getClass());
}
_version = Version.unknownVersion();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ public void serialize(Test3787Bean value, JsonGenerator gen, SerializerProvider
}
}

// For [databind#5063]
static class Module5063A extends SimpleModule {
public Module5063A() {
super(Version.unknownVersion());
}
}

static class Module5063B extends SimpleModule {
public Module5063B() {
super(Version.unknownVersion());
}
}

/*
/**********************************************************
/* Unit tests; first, verifying need for custom handlers
Expand Down Expand Up @@ -624,4 +637,15 @@ public void testAddModuleWithDeserializerTwiceThenOnlyLatestIsKept_reverseOrder(

assertEquals("I am A", result.value);
}

// For [databind#5063]
@Test
public void testDuplicateModules5063() {
ObjectMapper mapper = JsonMapper.builder()
.addModule(new Module5063A())
.addModule(new Module5063B())
.build();
Set<Object> modules = mapper.getRegisteredModuleIds();
assertEquals(2, modules.size());
}
}