Skip to content

Commit feb4ce1

Browse files
authored
Apply JsonTypeInfo.Value usages from Jackson 3.0 (#3953)
1 parent 77789ab commit feb4ce1

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,8 +1993,9 @@ public ObjectMapper activateDefaultTyping(PolymorphicTypeValidator ptv,
19931993

19941994
TypeResolverBuilder<?> typer = _constructDefaultTypeResolverBuilder(applicability, ptv);
19951995
// we'll always use full class name, when using defaulting
1996-
typer = typer.init(JsonTypeInfo.Id.CLASS, null);
1997-
typer = typer.inclusion(includeAs);
1996+
JsonTypeInfo.Value typeInfo = JsonTypeInfo.Value.construct(JsonTypeInfo.Id.CLASS, includeAs,
1997+
null, null, false, null);
1998+
typer = typer.init(typeInfo, null);
19981999
return setDefaultTyping(typer);
19992000
}
20002001

@@ -2023,9 +2024,9 @@ public ObjectMapper activateDefaultTypingAsProperty(PolymorphicTypeValidator ptv
20232024
TypeResolverBuilder<?> typer = _constructDefaultTypeResolverBuilder(applicability,
20242025
ptv);
20252026
// we'll always use full class name, when using defaulting
2026-
typer = typer.init(JsonTypeInfo.Id.CLASS, null);
2027-
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
2028-
typer = typer.typeProperty(propertyName);
2027+
JsonTypeInfo.Value typeInfo = JsonTypeInfo.Value.construct(JsonTypeInfo.Id.CLASS, JsonTypeInfo.As.PROPERTY,
2028+
propertyName, null, false, null);
2029+
typer = typer.init(typeInfo, null);
20292030
return setDefaultTyping(typer);
20302031
}
20312032

src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,27 +1508,29 @@ protected PropertyName _findConstructorName(Annotated a)
15081508
protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config,
15091509
Annotated ann, JavaType baseType)
15101510
{
1511+
// since 2.16 : backporting {@link JsonTypeInfo.Value} from 3.0
1512+
JsonTypeInfo.Value typeInfo = findPolymorphicTypeInfo(config, ann);
1513+
15111514
// First: maybe we have explicit type resolver?
15121515
TypeResolverBuilder<?> b;
1513-
JsonTypeInfo info = _findAnnotation(ann, JsonTypeInfo.class);
15141516
JsonTypeResolver resAnn = _findAnnotation(ann, JsonTypeResolver.class);
15151517

15161518
if (resAnn != null) {
1517-
if (info == null) {
1519+
if (typeInfo == null) {
15181520
return null;
15191521
}
15201522
// let's not try to force access override (would need to pass
15211523
// settings through if we did, since that's not doable on some platforms)
15221524
b = config.typeResolverBuilderInstance(ann, resAnn.value());
15231525
} else { // if not, use standard one, if indicated by annotations
1524-
if (info == null) {
1526+
if (typeInfo == null) {
15251527
return null;
15261528
}
15271529
// bit special; must return 'marker' to block use of default typing:
1528-
if (info.use() == JsonTypeInfo.Id.NONE) {
1530+
if (typeInfo.getIdType() == JsonTypeInfo.Id.NONE) {
15291531
return _constructNoTypeResolverBuilder();
15301532
}
1531-
b = _constructStdTypeResolverBuilder();
1533+
b = _constructStdTypeResolverBuilder(config, typeInfo, baseType);
15321534
}
15331535
// Does it define a custom type id resolver?
15341536
JsonTypeIdResolver idResInfo = _findAnnotation(ann, JsonTypeIdResolver.class);
@@ -1537,26 +1539,24 @@ protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config,
15371539
if (idRes != null) {
15381540
idRes.init(baseType);
15391541
}
1540-
b = b.init(info.use(), idRes);
15411542
// 13-Aug-2011, tatu: One complication; external id only works for properties;
15421543
// so if declared for a Class, we will need to map it to "PROPERTY"
15431544
// instead of "EXTERNAL_PROPERTY"
1544-
JsonTypeInfo.As inclusion = info.include();
1545+
JsonTypeInfo.As inclusion = typeInfo.getInclusionType();
15451546
if (inclusion == JsonTypeInfo.As.EXTERNAL_PROPERTY && (ann instanceof AnnotatedClass)) {
1546-
inclusion = JsonTypeInfo.As.PROPERTY;
1547+
typeInfo = typeInfo.withInclusionType(JsonTypeInfo.As.PROPERTY);
15471548
}
1548-
b = b.inclusion(inclusion);
1549-
b = b.typeProperty(info.property());
1550-
Class<?> defaultImpl = info.defaultImpl();
1549+
Class<?> defaultImpl = typeInfo.getDefaultImpl();
15511550

15521551
// 08-Dec-2014, tatu: To deprecate `JsonTypeInfo.None` we need to use other placeholder(s);
15531552
// and since `java.util.Void` has other purpose (to indicate "deser as null"), we'll instead
15541553
// use `JsonTypeInfo.class` itself. But any annotation type will actually do, as they have no
15551554
// valid use (cannot instantiate as default)
1556-
if (defaultImpl != JsonTypeInfo.None.class && !defaultImpl.isAnnotation()) {
1557-
b = b.defaultImpl(defaultImpl);
1555+
if (defaultImpl != null && defaultImpl != JsonTypeInfo.None.class && !defaultImpl.isAnnotation()) {
1556+
typeInfo = typeInfo.withDefaultImpl(defaultImpl);
15581557
}
1559-
b = b.typeIdVisibility(info.visible());
1558+
1559+
b = b.init(typeInfo, idRes);
15601560
return b;
15611561
}
15621562

@@ -1568,6 +1568,17 @@ protected StdTypeResolverBuilder _constructStdTypeResolverBuilder() {
15681568
return new StdTypeResolverBuilder();
15691569
}
15701570

1571+
/**
1572+
* Helper method for constructing standard {@link TypeResolverBuilder}
1573+
* implementation.
1574+
*
1575+
* @since 2.16 (backported from Jackson 3.0)
1576+
*/
1577+
protected TypeResolverBuilder<?> _constructStdTypeResolverBuilder(MapperConfig<?> config,
1578+
JsonTypeInfo.Value typeInfo, JavaType baseType) {
1579+
return new StdTypeResolverBuilder(typeInfo);
1580+
}
1581+
15711582
/**
15721583
* Helper method for dealing with "no type info" marker; can't be null
15731584
* (as it'd be replaced by default typing)

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.databind.jsontype.impl;
22

3+
import com.fasterxml.jackson.databind.introspect.Annotated;
34
import java.util.Collection;
45

56
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@@ -35,6 +36,11 @@ public class StdTypeResolverBuilder
3536
*/
3637
protected boolean _typeIdVisible = false;
3738

39+
/**
40+
* @since 2.16 (backported from Jackson 3.0)
41+
*/
42+
protected Boolean _requireTypeIdForSubtypes;
43+
3844
/**
3945
* Default class to use in case type information is not available
4046
* or is broken.
@@ -78,6 +84,7 @@ protected StdTypeResolverBuilder(StdTypeResolverBuilder base,
7884
_customIdResolver = base._customIdResolver;
7985

8086
_defaultImpl = defaultImpl;
87+
_requireTypeIdForSubtypes = base._requireTypeIdForSubtypes;
8188
}
8289

8390
/**
@@ -92,6 +99,8 @@ public StdTypeResolverBuilder(JsonTypeInfo.Value settings) {
9299
_includeAs = settings.getInclusionType();
93100
_typeProperty = _propName(settings.getPropertyName(), _idType);
94101
_defaultImpl = settings.getDefaultImpl();
102+
_typeIdVisible = settings.getIdVisible();
103+
_requireTypeIdForSubtypes = settings.getRequireTypeIdForSubtypes();
95104
}
96105
}
97106

@@ -106,7 +115,9 @@ protected static String _propName(String propName, JsonTypeInfo.Id idType) {
106115
}
107116

108117
public static StdTypeResolverBuilder noTypeInfoBuilder() {
109-
return new StdTypeResolverBuilder().init(JsonTypeInfo.Id.NONE, null);
118+
JsonTypeInfo.Value typeInfo = JsonTypeInfo.Value.construct(JsonTypeInfo.Id.NONE, null,
119+
null, null, false, null);
120+
return new StdTypeResolverBuilder().init(typeInfo, null);
110121
}
111122

112123
@Override
@@ -123,9 +134,6 @@ public StdTypeResolverBuilder init(JsonTypeInfo.Id idType, TypeIdResolver idRes)
123134
return this;
124135
}
125136

126-
/**
127-
* @since 2.16 (backported from Jackson 3.0)
128-
*/
129137
@Override
130138
public StdTypeResolverBuilder init(JsonTypeInfo.Value settings,
131139
TypeIdResolver idRes)
@@ -146,6 +154,7 @@ public StdTypeResolverBuilder init(JsonTypeInfo.Value settings,
146154
}
147155
_typeIdVisible = settings.getIdVisible();
148156
_defaultImpl = settings.getDefaultImpl();
157+
_requireTypeIdForSubtypes = settings.getRequireTypeIdForSubtypes();
149158
}
150159
return this;
151160
}
@@ -488,11 +497,11 @@ protected boolean _strictTypeIdHandling(DeserializationConfig config, JavaType b
488497
*
489498
* @return true if the class has type resolver annotations, false otherwise
490499
*
491-
* @since 2.15
500+
* @since 2.15, using {@code ai.findPolymorphicTypeInfo(config, ac)} since 2.16.
492501
*/
493502
protected boolean _hasTypeResolver(DeserializationConfig config, JavaType baseType) {
494503
AnnotatedClass ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config, baseType.getRawClass());
495504
AnnotationIntrospector ai = config.getAnnotationIntrospector();
496-
return ai.findTypeResolver(config, ac, baseType) != null;
505+
return ai.findPolymorphicTypeInfo(config, ac) != null;
497506
}
498507
}

0 commit comments

Comments
 (0)