From f918d04da4cbc4fcce7037742fe12896002e9dd1 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 11 Apr 2023 18:27:05 -0700 Subject: [PATCH] Minor clean up post #3853 --- release-notes/CREDITS-2.x | 6 ++++++ release-notes/VERSION-2.x | 3 +++ .../com/fasterxml/jackson/databind/MapperFeature.java | 11 ++++++++--- .../jsontype/impl/AsPropertyTypeDeserializer.java | 5 ++--- .../jsontype/impl/StdTypeResolverBuilder.java | 5 +++-- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index e11bc40258..e99f26633d 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -1613,3 +1613,9 @@ Patrick Strawderman (kilink@github) Matt Nelson (mattnelson@github) * Requested #3814: Enhance `StdNodeBasedDeserializer` to support `readerForUpdating` (2.15.0) + +Steve Storey (stevestorey@github) + * Contributed #3853: Add `MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES` to enable/disable + strict subtype Type Id handling + (2.15.0) + diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 783bf2b557..27a5eef2c0 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -17,6 +17,9 @@ Not yet released #3836: `Optional` is not recognized as boolean field (reported by @thnaeff) (fix contributed by Joo-Hyuk K) +#3853: Add `MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES` to enable/disable + strict subtype Type Id handling + (contributed by Steve S)) 2.15.0-rc2 (28-Mar-2023) diff --git a/src/main/java/com/fasterxml/jackson/databind/MapperFeature.java b/src/main/java/com/fasterxml/jackson/databind/MapperFeature.java index d419712e34..2ecf7ee450 100644 --- a/src/main/java/com/fasterxml/jackson/databind/MapperFeature.java +++ b/src/main/java/com/fasterxml/jackson/databind/MapperFeature.java @@ -329,9 +329,14 @@ public enum MapperFeature implements ConfigFeature INFER_BUILDER_TYPE_BINDINGS(true), /** - * Feature that determines what happens when deserializing to a registered sub-type, but no - * type information has been provided. If enabled, then an {@link InvalidTypeIdException} - * will be thrown, if disabled then the deserialization will proceed without the type information. + * Feature that determines what happens when deserializing to a registered sub-type + * (polymorphic deserialization), but no type information has been provided. + * If enabled, then an {@code InvalidTypeIdException} will be thrown; + * if disabled then the deserialization may proceed without the type information + * if sub-type is legit target (non-abstract). + *

+ * Feature is enabled by default for backwards-compatibility (same behavior + * as in Jackson 2.14 and earlier). * * @since 2.15 */ diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsPropertyTypeDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsPropertyTypeDeserializer.java index 894102b317..3e1ae93eeb 100644 --- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsPropertyTypeDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsPropertyTypeDeserializer.java @@ -62,9 +62,8 @@ public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes, String typePropertyName, boolean typeIdVisible, JavaType defaultImpl, As inclusion) { - super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl); - _inclusion = inclusion; - _strictTypeIdHandling = false; + this(bt, idRes, typePropertyName, typeIdVisible, defaultImpl, inclusion, + true); } public AsPropertyTypeDeserializer(AsPropertyTypeDeserializer src, BeanProperty property) { diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java index 6ef4d18874..a98b92c0ed 100644 --- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java +++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java @@ -421,13 +421,14 @@ protected boolean _strictTypeIdHandling(DeserializationConfig config, JavaType b if (config.isEnabled(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES)) { return true; } - // Otherwise we will be strict if there's a type resolver + // Otherwise we will be strict if there's a type resolver: presumably + // target type is a (likely abstract) base type and cannot be used as target return _hasTypeResolver(config, baseType); } /** * Checks whether the given class has annotations indicating some type resolver - * is applied, for example {@link com.fasterxml.jackson.annotation.JsonSubTypes}. + * is applied, for example {@link com.fasterxml.jackson.annotation.JsonTypeInfo}. * Only initializes {@link #_hasTypeResolver} once if its value is null. * * @param config the deserialization configuration to use