fix: enable enum type metadata serialization with NON_FINAL_AND_ENUMS#3364
Open
lukman48 wants to merge 1 commit into
Open
fix: enable enum type metadata serialization with NON_FINAL_AND_ENUMS#3364lukman48 wants to merge 1 commit into
lukman48 wants to merge 1 commit into
Conversation
Fixes issue spring-projects#3306: RedisCache with GenericJacksonJsonRedisSerializer breaks when using enums. Problem ------- When using GenericJacksonJsonRedisSerializer, enum values are always serialized without type metadata, even when DefaultTyping.NON_FINAL_AND_ENUMS is explicitly configured. This causes ClassCastException during deserialization because deserializers receive String values instead of Enum types. Root Cause ---------- The TypeResolverBuilder.useForType() method unconditionally excludes enum types regardless of the DefaultTyping setting. It did not account for the case where users explicitly request enum type metadata via NON_FINAL_AND_ENUMS. Solution -------- 1. Store the DefaultTyping parameter in TypeResolverBuilder 2. Update useForType() to check the DefaultTyping setting: - Return true for enums when DefaultTyping == NON_FINAL_AND_ENUMS - Return false for enums with other DefaultTyping options (backward compatible) 3. Preserve Kotlin support and other existing logic Backward Compatibility --------------------- ✓ Enums still excluded by default (NON_FINAL, OBJECT_AND_NON_FINAL, etc.) ✓ Only included when explicitly requested via NON_FINAL_AND_ENUMS ✓ No API changes ✓ All existing tests pass Test Coverage ------------- Added three test cases: - shouldSerializeEnumWithTypeMetadataWhenUsingNonFinalAndEnumsTyping - shouldDeserializeEnumWithoutTypeMetadataUsingNonFinalTyping - shouldHandleEnumWithUnsafeDefaultTyping_StillConvertsToStringDueToLegacyBehavior All 30 existing tests in GenericJacksonJsonRedisSerializerUnitTests pass. Signed-off-by: lukman48 <lukman_uki@yahoo.co.id>
bbccb66 to
0965640
Compare
Author
✅ DCO Signature UpdatedThe commit has been amended with the required DCO (Developer Certificate of Origin) signature. Changes Made:
Commit Details:PR Status:
Ready for maintainer review! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes issue #3306: RedisCache with
GenericJacksonJsonRedisSerializerbreaks when using enums.Problem
When using
GenericJacksonJsonRedisSerializer, enum values are always serialized without type metadata, even whenDefaultTyping.NON_FINAL_AND_ENUMSis explicitly configured. This causesClassCastExceptionduring deserialization.Example
When configured with:
Expected: Enum cached with type metadata, deserializes correctly to
StatusenumActual: Throws
ClassCastException: class java.lang.String cannot be cast to class StatusRoot Cause
TypeResolverBuilder.useForType()unconditionally excludes enum types regardless of theDefaultTypingsetting:This doesn't account for
DefaultTyping.NON_FINAL_AND_ENUMSwhich explicitly requests enum type metadata.Solution
DefaultTypinginTypeResolverBuilder- Add a field to preserve the typing configurationuseForType()logic - Check theDefaultTypingsetting before excluding enums:truefor enums whenDefaultTyping == NON_FINAL_AND_ENUMSfalsefor other settings (maintains backward compatibility)Implementation Details
Changes Made
File:
GenericJacksonJsonRedisSerializer.javadefaultTypingfield toTypeResolverBuilderuseForType()to respect the configuration:Test Coverage
Added three test cases in
GenericJacksonJsonRedisSerializerUnitTests:shouldSerializeEnumWithTypeMetadataWhenUsingNonFinalAndEnumsTypingshouldDeserializeEnumWithoutTypeMetadataUsingNonFinalTypingNON_FINALshouldHandleEnumWithUnsafeDefaultTyping_StillConvertsToStringDueToLegacyBehaviorTest Results: All 30 existing tests pass ✅
Backward Compatibility
✅ No breaking changes:
NON_FINAL_AND_ENUMSValidation
Related
RedisCachewithGenericJacksonJsonRedisSerializerbreaks when using enums #3306DefaultTyping.NON_FINAL_AND_ENUMSintent