Skip to content

Commit 92517aa

Browse files
committed
Refactoring before trying to fix #3235
1 parent 812d57a commit 92517aa

File tree

3 files changed

+34
-45
lines changed

3 files changed

+34
-45
lines changed

src/main/java/com/fasterxml/jackson/databind/jsontype/TypeResolverBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public interface TypeResolverBuilder<T extends TypeResolverBuilder<T>>
5151
* available during type resolution
5252
*/
5353
public Class<?> getDefaultImpl();
54-
54+
5555
/*
5656
/**********************************************************
5757
/* Actual builder methods

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -168,47 +168,39 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config,
168168
throw new IllegalStateException("Do not know how to construct standard type serializer for inclusion type: "+_includeAs);
169169
}
170170

171-
protected JavaType defineDefaultImpl(DeserializationConfig config, JavaType baseType) {
172-
JavaType defaultImpl;
173-
if (_defaultImpl == null) {
174-
if (config.isEnabled(MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL) && !baseType.isAbstract()) {
175-
defaultImpl = baseType;
176-
} else {
177-
defaultImpl = null;
178-
}
179-
} else {
171+
protected JavaType defineDefaultImpl(DeserializationConfig config, JavaType baseType)
172+
{
173+
if (_defaultImpl != null) {
180174
// 20-Mar-2016, tatu: It is important to do specialization go through
181175
// TypeFactory to ensure proper resolution; with 2.7 and before, direct
182176
// call to JavaType was used, but that cannot work reliably with 2.7
183177
// 20-Mar-2016, tatu: Can finally add a check for type compatibility BUT
184178
// if so, need to add explicit checks for marker types. Not ideal, but
185179
// seems like a reasonable compromise.
186-
if ((_defaultImpl == Void.class)
187-
|| (_defaultImpl == NoClass.class)) {
188-
defaultImpl = config.getTypeFactory().constructType(_defaultImpl);
189-
} else {
190-
if (baseType.hasRawClass(_defaultImpl)) { // common enough to check
191-
defaultImpl = baseType;
192-
} else if (baseType.isTypeOrSuperTypeOf(_defaultImpl)) {
193-
// most common case with proper base type...
194-
defaultImpl = config.getTypeFactory()
195-
.constructSpecializedType(baseType, _defaultImpl);
196-
} else {
197-
// 05-Apr-2018, tatu: As [databind#1565] and [databind#1861] need to allow
198-
// some cases of seemingly incompatible `defaultImpl`. Easiest to just clear
199-
// the setting.
200-
201-
/*
202-
throw new IllegalArgumentException(
203-
String.format("Invalid \"defaultImpl\" (%s): not a subtype of basetype (%s)",
204-
ClassUtil.nameOf(_defaultImpl), ClassUtil.nameOf(baseType.getRawClass()))
205-
);
206-
*/
207-
defaultImpl = null;
208-
}
180+
if ((_defaultImpl == Void.class) || (_defaultImpl == NoClass.class)) {
181+
// 18-Sep-2021, tatu: This has specific meaning: these two markers will
182+
// be used to conjure `null` value out of invalid type ids
183+
return config.getTypeFactory().constructType(_defaultImpl);
184+
}
185+
if (baseType.hasRawClass(_defaultImpl)) { // tiny optimization
186+
return baseType;
209187
}
188+
if (baseType.isTypeOrSuperTypeOf(_defaultImpl)) {
189+
// most common case with proper base type...
190+
return config.getTypeFactory()
191+
.constructSpecializedType(baseType, _defaultImpl);
192+
}
193+
if (baseType.hasRawClass(_defaultImpl)) {
194+
return baseType;
195+
}
196+
}
197+
// use base type as default should always be used as the last choice.
198+
if (config.isEnabled(MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL)
199+
&& !baseType.isAbstract()) {
200+
// still can not resolve by default impl, fall back to use base type as default impl
201+
return baseType;
210202
}
211-
return defaultImpl;
203+
return null;
212204
}
213205

214206
/*

src/test/java/com/fasterxml/jackson/databind/jsontype/TestBaseTypeAsDefault.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
99

10-
import java.io.IOException;
11-
1210
public class TestBaseTypeAsDefault extends BaseMapTest
1311
{
1412
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class")
1513
static class Parent {
1614
}
1715

18-
1916
static class Child extends Parent {
2017
}
2118

@@ -44,18 +41,18 @@ static class ChildOfChild extends ChildOfAbstract {
4441
protected ObjectMapper MAPPER_WITHOUT_BASE = jsonMapperBuilder()
4542
.disable(MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL)
4643
.build();
47-
48-
public void testPositiveForParent() throws IOException {
44+
45+
public void testPositiveForParent() throws Exception {
4946
Object o = MAPPER_WITH_BASE.readerFor(Parent.class).readValue("{}");
5047
assertEquals(o.getClass(), Parent.class);
5148
}
5249

53-
public void testPositiveForChild() throws IOException {
50+
public void testPositiveForChild() throws Exception {
5451
Object o = MAPPER_WITH_BASE.readerFor(Child.class).readValue("{}");
5552
assertEquals(o.getClass(), Child.class);
5653
}
5754

58-
public void testNegativeForParent() throws IOException {
55+
public void testNegativeForParent() throws Exception {
5956
try {
6057
/*Object o =*/ MAPPER_WITHOUT_BASE.readerFor(Parent.class).readValue("{}");
6158
fail("Should not pass");
@@ -64,7 +61,7 @@ public void testNegativeForParent() throws IOException {
6461
}
6562
}
6663

67-
public void testNegativeForChild() throws IOException {
64+
public void testNegativeForChild() throws Exception {
6865
try {
6966
/*Object o =*/ MAPPER_WITHOUT_BASE.readerFor(Child.class).readValue("{}");
7067
fail("Should not pass");
@@ -73,19 +70,19 @@ public void testNegativeForChild() throws IOException {
7370
}
7471
}
7572

76-
public void testConversionForAbstractWithDefault() throws IOException {
73+
public void testConversionForAbstractWithDefault() throws Exception {
7774
// should pass shouldn't it?
7875
Object o = MAPPER_WITH_BASE.readerFor(AbstractParentWithDefault.class).readValue("{}");
7976
assertEquals(o.getClass(), ChildOfChild.class);
8077
}
8178

82-
public void testPositiveWithTypeSpecification() throws IOException {
79+
public void testPositiveWithTypeSpecification() throws Exception {
8380
Object o = MAPPER_WITH_BASE.readerFor(Parent.class)
8481
.readValue("{\"@class\":\""+Child.class.getName()+"\"}");
8582
assertEquals(o.getClass(), Child.class);
8683
}
8784

88-
public void testPositiveWithManualDefault() throws IOException {
85+
public void testPositiveWithManualDefault() throws Exception {
8986
Object o = MAPPER_WITH_BASE.readerFor(ChildOfAbstract.class).readValue("{}");
9087

9188
assertEquals(o.getClass(), ChildOfChild.class);

0 commit comments

Comments
 (0)