Skip to content

Commit c5fd40f

Browse files
committed
Bit more work for #3110
1 parent 609ad49 commit c5fd40f

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.LinkedHashSet;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.concurrent.atomic.AtomicInteger;
89

910
import com.fasterxml.jackson.core.Version;
1011
import com.fasterxml.jackson.databind.*;
@@ -42,6 +43,11 @@ public class SimpleModule
4243
{
4344
private static final long serialVersionUID = 1L; // 2.5.0
4445

46+
// 16-Jun-2021, tatu: For [databind#3110], generate actual unique ids
47+
// for SimpleModule instances (System.identityHashCode(...) is close
48+
// but not quite it...
49+
private static final AtomicInteger MODULE_ID_SEQ = new AtomicInteger(1);
50+
4551
protected final String _name;
4652
protected final Version _version;
4753

@@ -114,9 +120,11 @@ public class SimpleModule
114120
*/
115121
public SimpleModule() {
116122
// can't chain when making reference to 'this'
117-
// note: generate different name for direct instantiation, sub-classing
123+
// note: generate different name for direct instantiation, sub-classing;
124+
// this to avoid collision in former case while still addressing
125+
// [databind#3110]
118126
_name = (getClass() == SimpleModule.class)
119-
? "SimpleModule-"+System.identityHashCode(this)
127+
? "SimpleModule-"+MODULE_ID_SEQ.getAndIncrement()
120128
: getClass().getName();
121129
_version = Version.unknownVersion();
122130
// 07-Jun-2021, tatu: [databind#3110] Not passed explicitly so...

src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,40 @@ public void testGetRegisteredModules()
355355
mapper.getRegisteredModuleIds());
356356
}
357357

358+
// More [databind#3110] testing
359+
public void testMultipleSimpleModules()
360+
{
361+
final SimpleModule mod1 = new SimpleModule();
362+
final SimpleModule mod2 = new SimpleModule();
363+
ObjectMapper mapper = JsonMapper.builder()
364+
.addModule(mod1)
365+
.addModule(mod2)
366+
.build();
367+
assertEquals(2, mapper.getRegisteredModuleIds().size());
368+
369+
// Still avoid actual duplicates
370+
mapper = JsonMapper.builder()
371+
.addModule(mod1)
372+
.addModule(mod1)
373+
.build();
374+
assertEquals(1, mapper.getRegisteredModuleIds().size());
375+
376+
// Same for (anonymous) sub-classes
377+
final SimpleModule subMod1 = new SimpleModule() { };
378+
final SimpleModule subMod2 = new SimpleModule() { };
379+
mapper = JsonMapper.builder()
380+
.addModule(subMod1)
381+
.addModule(subMod2)
382+
.build();
383+
assertEquals(2, mapper.getRegisteredModuleIds().size());
384+
385+
mapper = JsonMapper.builder()
386+
.addModule(subMod1)
387+
.addModule(subMod1)
388+
.build();
389+
assertEquals(1, mapper.getRegisteredModuleIds().size());
390+
}
391+
358392
/*
359393
/**********************************************************
360394
/* Unit tests; other

0 commit comments

Comments
 (0)