Skip to content

Commit 7d84a02

Browse files
committed
Fix #5481: add modules should work for Builder beyond build() method
1 parent 041fce8 commit 7d84a02

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
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)