Skip to content

Commit 5045d8b

Browse files
committed
Javadoc improvements, adding base class for PTV (wrt #2195)
1 parent 9da1f1b commit 5045d8b

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @since 2.10
2626
*/
2727
public class BasicPolymorphicTypeValidator
28-
extends PolymorphicTypeValidator
28+
extends PolymorphicTypeValidator.Base
2929
implements java.io.Serializable
3030
{
3131
private static final long serialVersionUID = 1L;

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

+56-3
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,46 @@
44
import com.fasterxml.jackson.databind.cfg.MapperConfig;
55

66
/**
7-
* Interface for classes that handle validation of class name-based subtypes used
7+
* Interface for classes that handle validation of class-name - based subtypes used
88
* with Polymorphic Deserialization: both via "default typing" and explicit
99
* {@code @JsonTypeInfo} when using Java Class name as Type Identifier.
10-
* The main purpose, initially, is to allow pluggable allow/deny lists to avoid
10+
* The main purpose, initially, is to allow pluggable allow lists to avoid
1111
* security problems that occur with unlimited class names
1212
* (See <a href="https://medium.com/@cowtowncoder/on-jackson-cves-dont-panic-here-is-what-you-need-to-know-54cd0d6e8062">
1313
* this article</a> for full explanation).
1414
*<p>
15-
* Call fal
15+
* Calls to methods are done as follows:
16+
* <ol>
17+
* <li>When a deserializer is needed for a polymorphic property (including root values) -- either
18+
* for explicitly annotated polymorphic type, or "default typing" -- {@link #validateBaseType}
19+
* is called to see if validity can be determined for all possible types: if
20+
* {@link Validity#ALLOWED} is returned no futher checks are made for any subtypes; of
21+
* {@link Validity#DENIED} is returned, an exception will be thrown to indicate invalid polymorphic
22+
* property
23+
* </li>
24+
* <li>If neither deny nor allowed was returned for property with specific base type, first time
25+
* specific Type Id (Class Name) is encountered, method {@link #validateSubClassName} is called
26+
* with resolved class name: it may indicate allowed/denied, resulting in either allowed use or
27+
* denial with exception
28+
* </li>
29+
* <li>If no denial/allowance indicated, class name is resolved to actual {@link Class}, and
30+
* {@link #validateSubType(MapperConfig, JavaType, JavaType)} is called: if
31+
* {@link Validity#ALLOWED} is returned, usage is accepted; otherwise (denied or indeterminate)
32+
* usage is not allowed and exception is thrown
33+
* </li>
34+
* </ol>
1635
*<p>
1736
* Notes on implementations: implementations must be thread-safe and shareable (usually meaning they
1837
* are stateless). Determinations for validity are usually effectively cached on per-property
1938
* basis (by virtue of subtype deserializers being cached by polymorphic deserializers) so
2039
* caching at validator level is usually not needed. If caching is used, however, it must be done
2140
* in thread-safe manner as validators are shared within {@link ObjectMapper} as well as possible
2241
* across mappers (in case of default/standard validator).
42+
*<p>
43+
* Also note that it is strongly recommended that all implementations are based on provided
44+
* abstract base class, {@link PolymorphicTypeValidator.Base} which contains helper methods
45+
* and default implementations for returning {@link Validity#INDETERMINATE} for validation
46+
* methods (to allow only overriding relevant methods implementation cares about)
2347
*
2448
* @since 2.10
2549
*/
@@ -116,4 +140,33 @@ public abstract Validity validateSubClassName(MapperConfig<?> config, JavaType b
116140
*/
117141
public abstract Validity validateSubType(MapperConfig<?> config, JavaType baseType,
118142
JavaType subType) throws JsonMappingException;
143+
144+
/**
145+
* Shared base class with partial implementation (with all validation calls returning
146+
* {@link Validity#INDETERMINATE}) and convenience methods for indicating failure reasons.
147+
* Use of this base class is strongly recommended over directly implement
148+
*/
149+
public abstract static class Base
150+
extends PolymorphicTypeValidator
151+
implements java.io.Serializable
152+
{
153+
private static final long serialVersionUID = 1L;
154+
155+
@Override
156+
public Validity validateBaseType(MapperConfig<?> config, JavaType baseType) {
157+
return Validity.INDETERMINATE;
158+
}
159+
160+
@Override
161+
public Validity validateSubClassName(MapperConfig<?> config, JavaType baseType, String subClassName)
162+
throws JsonMappingException {
163+
return Validity.INDETERMINATE;
164+
}
165+
166+
@Override
167+
public Validity validateSubType(MapperConfig<?> config, JavaType baseType, JavaType subType)
168+
throws JsonMappingException {
169+
return Validity.INDETERMINATE;
170+
}
171+
}
119172
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @since 2.10
1414
*/
1515
public final class LaissezFaireSubTypeValidator
16-
extends PolymorphicTypeValidator
16+
extends PolymorphicTypeValidator.Base
1717
{
1818
private static final long serialVersionUID = 1L;
1919

@@ -35,4 +35,4 @@ public Validity validateSubType(MapperConfig<?> ctxt, JavaType baseType,
3535
JavaType subType) {
3636
return Validity.ALLOWED;
3737
}
38-
}
38+
}

src/test/java/com/fasterxml/jackson/databind/testutil/NoCheckSubTypeValidator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* use of any subtypes.
1010
*/
1111
public final class NoCheckSubTypeValidator
12-
extends PolymorphicTypeValidator
12+
extends PolymorphicTypeValidator.Base
1313
{
1414
private static final long serialVersionUID = 1L;
1515

0 commit comments

Comments
 (0)