From 609ad49e4cf3d05c308c569b14f3f63d0a190a19 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 16 Jun 2021 15:14:12 -0700 Subject: [PATCH] Minor reversion wrt #3110: limit changes to SimpleModule registration id handling --- .../jackson/databind/module/SimpleModule.java | 22 +++++++++++++------ .../databind/module/SimpleModuleTest.java | 14 ++++++++---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java b/src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java index 2bed1cb799..d870a25bda 100644 --- a/src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java +++ b/src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java @@ -115,8 +115,8 @@ public class SimpleModule public SimpleModule() { // can't chain when making reference to 'this' // note: generate different name for direct instantiation, sub-classing - _name = (getClass() == SimpleModule.class) ? - "SimpleModule-"+System.identityHashCode(this) + _name = (getClass() == SimpleModule.class) + ? "SimpleModule-"+System.identityHashCode(this) : getClass().getName(); _version = Version.unknownVersion(); // 07-Jun-2021, tatu: [databind#3110] Not passed explicitly so... @@ -198,14 +198,22 @@ public SimpleModule(String name, Version version, @Override public Object getTypeId() { - // 07-Jun-2021, tatu: [databind#3110] Only return Type Id if name - // was explicitly given + // 07-Jun-2021, tatu: [databind#3110] Return Type Id if name was + // explicitly given if (_hasExplicitName) { return _name; } - // ...otherwise give no type id, even for sub-classes (sub-classes are - // welcome to override this method of course) - return null; + // Otherwise behavior same as with 2.12: no registration id for "throw-away" + // instances (to avoid bogus conflicts if user just instantiates SimpleModule) + + // Note: actually... always returning `supet.getTypeId()` should be fine since + // that would return generated id? Let's do that actually. + if (getClass() == SimpleModule.class) { + return _name; + } + // And for what it is worth, this should usually do the same and we could + // in fact always just return `_name`. But leaving as-is for now. + return super.getTypeId(); } /* diff --git a/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java b/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java index 2b9bc84a65..951fa37c37 100644 --- a/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java @@ -334,14 +334,20 @@ public void testGetRegisteredModules() mapper = new ObjectMapper(); assertEquals(0, mapper.getRegisteredModuleIds().size()); - // 07-Jun-2021, tatu [databind#3110] Casual SimpleModules not returned - // as registered + // 07-Jun-2021, tatu [databind#3110] Casual SimpleModules ARE returned + // too! mapper = JsonMapper.builder() .addModule(new SimpleModule()) .build(); - assertEquals(0, mapper.getRegisteredModuleIds().size()); + assertEquals(1, mapper.getRegisteredModuleIds().size()); + Object id = mapper.getRegisteredModuleIds().iterator().next(); + assertTrue(id instanceof String); + if (!id.toString().startsWith("SimpleModule-")) { + fail("SimpleModule registration id should start with 'SimpleModule-', does not: [" + +id+"]"); + } - // But named ones are + // And named ones retain their name mapper = JsonMapper.builder() .addModule(new SimpleModule("VerySpecialModule")) .build();