Skip to content

Commit 1e253a6

Browse files
committed
Merge branch '3.0' into 3.x
2 parents f1586ff + b8bacc2 commit 1e253a6

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/main/java/tools/jackson/databind/cfg/MapperBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,8 @@ public B withAllCoercionConfigs(Consumer<CoercionConfigs> handler) {
10371037
*/
10381038
public B removeAllModules() {
10391039
_modules = null;
1040+
// [databind#5481]: invalidate cached state when modules are modified
1041+
_savedState = null;
10401042
return _this();
10411043
}
10421044

@@ -1068,6 +1070,8 @@ public B addModule(JacksonModule module)
10681070
_modules.putIfAbsent(dep.getRegistrationId(), dep);
10691071
}
10701072
_modules.put(moduleId, module);
1073+
// [databind#5481]: invalidate cached state when modules are modified
1074+
_savedState = null;
10711075
return _this();
10721076
}
10731077

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package tools.jackson.databind.module;
2+
3+
import java.util.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import tools.jackson.core.Version;
8+
import tools.jackson.databind.*;
9+
import tools.jackson.databind.json.JsonMapper;
10+
import tools.jackson.databind.testutil.DatabindTestUtil;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
/**
15+
* Test for [databind#5481]: JsonMapper.Builder module registration bug
16+
* when reusing builder instance between multiple builds with different modules.
17+
*/
18+
@SuppressWarnings("serial")
19+
public class BuilderModuleReuse5481Test extends DatabindTestUtil
20+
{
21+
// Test modules with distinct names for easy identification
22+
static class ModuleA extends SimpleModule {
23+
public ModuleA() {
24+
super("ModuleA", Version.unknownVersion());
25+
}
26+
}
27+
28+
static class ModuleB extends SimpleModule {
29+
public ModuleB() {
30+
super("ModuleB", Version.unknownVersion());
31+
}
32+
}
33+
34+
static class ModuleC extends SimpleModule {
35+
public ModuleC() {
36+
super("ModuleC", Version.unknownVersion());
37+
}
38+
}
39+
40+
/**
41+
* Test case demonstrating issue #5481: when reusing a JsonMapper.Builder,
42+
* calling modules() multiple times between builds should reflect in each
43+
* new mapper instance, but currently only the first set of modules is
44+
* registered in all instances.
45+
*/
46+
@Test
47+
public void testBuilderReuseWithDifferentModules() {
48+
ModuleA moduleA = new ModuleA();
49+
ModuleB moduleB = new ModuleB();
50+
ModuleC moduleC = new ModuleC();
51+
52+
// Create a builder and register first set of modules
53+
JsonMapper.Builder builder = JsonMapper.builder()
54+
.addModule(moduleA)
55+
.addModule(moduleB);
56+
57+
// Build first mapper
58+
ObjectMapper mapper1 = builder.build();
59+
60+
// Verify first mapper has modules A, B
61+
Collection<JacksonModule> modules1 = mapper1.registeredModules();
62+
assertEquals(List.of("ModuleA", "ModuleB"), getModuleNames(modules1));
63+
64+
// Now reuse the builder and register different modules
65+
builder.addModule(moduleC);
66+
67+
// Build second mapper
68+
ObjectMapper mapper2 = builder.build();
69+
70+
// BUG: Second mapper should have modules A, B, C
71+
// but according to issue #5481, it incorrectly only has A, B
72+
Collection<JacksonModule> modules2 = mapper2.registeredModules();
73+
assertEquals(List.of("ModuleA", "ModuleB", "ModuleC"), getModuleNames(modules2));
74+
}
75+
76+
private List<String> getModuleNames(Collection<JacksonModule> modules) {
77+
return modules.stream().map(JacksonModule::getModuleName).toList();
78+
}
79+
}

0 commit comments

Comments
 (0)