4747import java .io .IOException ;
4848import java .util .Collections ;
4949import java .util .function .Consumer ;
50+ import java .util .function .Predicate ;
5051import java .util .function .Supplier ;
5152
5253import org .jspecify .annotations .Nullable ;
6869 * {@link JacksonObjectWriter}.
6970 *
7071 * @author Christoph Strobl
72+ * @author Chris Bono
7173 * @see JacksonObjectReader
7274 * @see JacksonObjectWriter
7375 * @see ObjectMapper
@@ -263,12 +265,11 @@ private static Lazy<String> getConfiguredTypeDeserializationPropertyName(ObjectM
263265 */
264266 public static class GenericJacksonJsonRedisSerializerBuilder <B extends MapperBuilder <? extends ObjectMapper , ? extends MapperBuilder <?, ?>>> {
265267
266- private static final DefaultTyping DEFAULT_TYPING = DefaultTyping .NON_FINAL ;
267-
268268 private final Supplier <B > builderFactory ;
269269
270270 private boolean cacheNullValueSupportEnabled = false ;
271- private @ Nullable DefaultTyping defaultTyping = null ;
271+ private boolean defaultTypingEnabled ;
272+ private @ Nullable Predicate <JavaType > defaultTyping ;
272273 private @ Nullable String typePropertyName ;
273274 private PolymorphicTypeValidator typeValidator = BasicPolymorphicTypeValidator .builder ()
274275 .allowIfBaseType (Object .class ).allowIfSubType ((ctx , clazz ) -> true ).build ();
@@ -325,7 +326,7 @@ public GenericJacksonJsonRedisSerializerBuilder<B> enableSpringCacheNullValueSup
325326 @ Contract ("-> this" )
326327 public GenericJacksonJsonRedisSerializerBuilder <B > enableUnsafeDefaultTyping () {
327328
328- withDefaultTyping () ;
329+ this . defaultTypingEnabled = true ;
329330 return this ;
330331 }
331332
@@ -337,15 +338,15 @@ public GenericJacksonJsonRedisSerializerBuilder<B> enableUnsafeDefaultTyping() {
337338 * <strong>WARNING</strong>: without restrictions of the {@link PolymorphicTypeValidator} deserialization is
338339 * vulnerable to arbitrary code execution when reading from untrusted sources.
339340 *
340- * @param defaultTyping the default typing mode to use .
341+ * @param defaultTyping the predicate that matches whether the type should have type info hints added .
341342 * @return {@code this} builder.
342343 * @since 4.0.3
343344 * @see <a href=
344345 * "https://owasp.org/www-community/vulnerabilities/Deserialization_of_untrusted_data">https://owasp.org/www-community/vulnerabilities/Deserialization_of_untrusted_data</a>
345346 */
346347 @ Contract ("_ -> this" )
347- public GenericJacksonJsonRedisSerializerBuilder <B > defaultTyping (DefaultTyping defaultTyping ) {
348-
348+ public GenericJacksonJsonRedisSerializerBuilder <B > defaultTyping (Predicate < JavaType > defaultTyping ) {
349+ this . defaultTypingEnabled = true ;
349350 this .defaultTyping = defaultTyping ;
350351 return this ;
351352 }
@@ -361,7 +362,7 @@ public GenericJacksonJsonRedisSerializerBuilder<B> defaultTyping(DefaultTyping d
361362 public GenericJacksonJsonRedisSerializerBuilder <B > enableDefaultTyping (PolymorphicTypeValidator typeValidator ) {
362363
363364 typeValidator (typeValidator );
364- withDefaultTyping () ;
365+ this . defaultTypingEnabled = true ;
365366
366367 return this ;
367368 }
@@ -395,15 +396,6 @@ public GenericJacksonJsonRedisSerializerBuilder<B> typePropertyName(String typeP
395396 return this ;
396397 }
397398
398- /**
399- * Enable default typing using {@link DefaultTyping#NON_FINAL} if not already configured.
400- */
401- private void withDefaultTyping () {
402- if (this .defaultTyping == null ) {
403- defaultTyping (DEFAULT_TYPING );
404- }
405- }
406-
407399 /**
408400 * Configures the {@link JacksonObjectWriter}.
409401 *
@@ -472,7 +464,7 @@ public GenericJacksonJsonRedisSerializer build() {
472464 }));
473465 }
474466
475- if (defaultTyping != null ) {
467+ if (defaultTypingEnabled ) {
476468
477469 GenericJacksonJsonRedisSerializer .TypeResolverBuilder resolver = new GenericJacksonJsonRedisSerializer .TypeResolverBuilder (
478470 typeValidator , defaultTyping , JsonTypeInfo .As .PROPERTY , JsonTypeInfo .Id .CLASS , typePropertyName );
@@ -628,12 +620,12 @@ public void setupModule(SetupContext context) {
628620
629621 private static class TypeResolverBuilder extends DefaultTypeResolverBuilder {
630622
631- private final DefaultTyping defaultTyping ;
623+ private final @ Nullable Predicate < JavaType > defaultTyping ;
632624
633- public TypeResolverBuilder (PolymorphicTypeValidator subtypeValidator , DefaultTyping defaultTyping ,
625+ public TypeResolverBuilder (PolymorphicTypeValidator subtypeValidator , @ Nullable Predicate < JavaType > defaultTyping ,
634626 JsonTypeInfo .As includeAs ,
635627 JsonTypeInfo .Id idType , @ Nullable String propertyName ) {
636- super (subtypeValidator , defaultTyping , includeAs , idType , propertyName );
628+ super (subtypeValidator , DefaultTyping . NON_FINAL , includeAs , idType , propertyName );
637629 this .defaultTyping = defaultTyping ;
638630 }
639631
@@ -656,11 +648,7 @@ public boolean useForType(JavaType javaType) {
656648
657649 javaType = resolveArrayOrWrapper (javaType );
658650
659- if (javaType .isEnumType () && defaultTyping != GenericJacksonJsonRedisSerializerBuilder .DEFAULT_TYPING ) {
660- return super .useForType (javaType );
661- }
662-
663- if (javaType .isEnumType () || ClassUtils .isPrimitiveOrWrapper (javaType .getRawClass ())) {
651+ if (ClassUtils .isPrimitiveOrWrapper (javaType .getRawClass ())) {
664652 return false ;
665653 }
666654
@@ -669,8 +657,13 @@ public boolean useForType(JavaType javaType) {
669657 return false ;
670658 }
671659
672- if (defaultTyping != GenericJacksonJsonRedisSerializerBuilder .DEFAULT_TYPING ) {
673- return super .useForType (javaType );
660+ if (defaultTyping != null ) {
661+ return defaultTyping .test (javaType );
662+ }
663+
664+ // Preserve backward compatability if type mapping not set
665+ if (javaType .isEnumType ()) {
666+ return false ;
674667 }
675668
676669 // [databind#88] Should not apply to JSON tree models:
0 commit comments