Class PolymorphicTypeValidator

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    PolymorphicTypeValidator.Base

    public abstract class PolymorphicTypeValidator
    extends java.lang.Object
    implements java.io.Serializable
    Interface for classes that handle validation of class-name - based subtypes used with Polymorphic Deserialization: both via "default typing" and explicit @JsonTypeInfo when using Java Class name as Type Identifier. The main purpose, initially, is to allow pluggable allow lists to avoid security problems that occur with unlimited class names (See this article for full explanation).

    Calls to methods are done as follows:

    1. When a deserializer is needed for a polymorphic property (including root values) -- either for explicitly annotated polymorphic type, or "default typing" -- validateBaseType(com.fasterxml.jackson.databind.cfg.MapperConfig<?>, com.fasterxml.jackson.databind.JavaType) is called to see if validity can be determined for all possible types: if PolymorphicTypeValidator.Validity.ALLOWED is returned no futher checks are made for any subtypes; of PolymorphicTypeValidator.Validity.DENIED is returned, an exception will be thrown to indicate invalid polymorphic property
    2. If neither deny nor allowed was returned for property with specific base type, first time specific Type Id (Class Name) is encountered, method validateSubClassName(com.fasterxml.jackson.databind.cfg.MapperConfig<?>, com.fasterxml.jackson.databind.JavaType, java.lang.String) is called with resolved class name: it may indicate allowed/denied, resulting in either allowed use or denial with exception
    3. If no denial/allowance indicated, class name is resolved to actual Class, and validateSubType(MapperConfig, JavaType, JavaType) is called: if PolymorphicTypeValidator.Validity.ALLOWED is returned, usage is accepted; otherwise (denied or indeterminate) usage is not allowed and exception is thrown

    Notes on implementations: implementations must be thread-safe and shareable (usually meaning they are stateless). Determinations for validity are usually effectively cached on per-property basis (by virtue of subtype deserializers being cached by polymorphic deserializers) so caching at validator level is usually not needed. If caching is used, however, it must be done in thread-safe manner as validators are shared within ObjectMapper as well as possible across mappers (in case of default/standard validator).

    Also note that it is strongly recommended that all implementations are based on provided abstract base class, PolymorphicTypeValidator.Base which contains helper methods and default implementations for returning PolymorphicTypeValidator.Validity.INDETERMINATE for validation methods (to allow only overriding relevant methods implementation cares about)

    Since:
    2.10
    See Also:
    Serialized Form