|
4 | 4 | import com.fasterxml.jackson.databind.cfg.MapperConfig;
|
5 | 5 |
|
6 | 6 | /**
|
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 |
8 | 8 | * with Polymorphic Deserialization: both via "default typing" and explicit
|
9 | 9 | * {@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 |
11 | 11 | * security problems that occur with unlimited class names
|
12 | 12 | * (See <a href="https://medium.com/@cowtowncoder/on-jackson-cves-dont-panic-here-is-what-you-need-to-know-54cd0d6e8062">
|
13 | 13 | * this article</a> for full explanation).
|
14 | 14 | *<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> |
16 | 35 | *<p>
|
17 | 36 | * Notes on implementations: implementations must be thread-safe and shareable (usually meaning they
|
18 | 37 | * are stateless). Determinations for validity are usually effectively cached on per-property
|
19 | 38 | * basis (by virtue of subtype deserializers being cached by polymorphic deserializers) so
|
20 | 39 | * caching at validator level is usually not needed. If caching is used, however, it must be done
|
21 | 40 | * in thread-safe manner as validators are shared within {@link ObjectMapper} as well as possible
|
22 | 41 | * 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) |
23 | 47 | *
|
24 | 48 | * @since 2.10
|
25 | 49 | */
|
@@ -116,4 +140,33 @@ public abstract Validity validateSubClassName(MapperConfig<?> config, JavaType b
|
116 | 140 | */
|
117 | 141 | public abstract Validity validateSubType(MapperConfig<?> config, JavaType baseType,
|
118 | 142 | 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 | + } |
119 | 172 | }
|
0 commit comments