Fix NullPointerException resolving type hints for JSON with duplicate properties#3397
Open
ctomc wants to merge 1 commit into
Open
Fix NullPointerException resolving type hints for JSON with duplicate properties#3397ctomc wants to merge 1 commit into
ctomc wants to merge 1 commit into
Conversation
… properties. GenericJacksonJsonRedisSerializer.TypeResolver.readTree(...) built the JSON node tree using a DeserializationContext from ObjectMapper._deserializationContext() that was never bound to the parser, so its stream-read capabilities stayed uninitialized. Deserializing any value whose JSON contains a duplicate property then failed with a NullPointerException in BaseNodeDeserializer._handleDuplicateProperty, which consults StreamReadCapability.DUPLICATE_PROPERTIES. Duplicate property names are valid JSON (RFC 8259) and occur in practice, for example in values written by the Jackson 2 serializer. Bind the parser to the context (as ObjectMapper._readTreeAndClose does via assignAndReturnParser) so the type-hint tree scan handles them. Closes spring-projects#3396 Signed-off-by: Tomaz Cerar <tomaz@cerar.net>
96ed288 to
3f96d6e
Compare
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.
Problem
GenericJacksonJsonRedisSerializer(Jackson 3, default typing enabled) throws aNullPointerExceptionwhen deserializing any value whose JSON contains a duplicate property name.TypeResolver.readTree(byte[])builds the JSON node tree with aDeserializationContextobtained fromObjectMapper._deserializationContext(), which is never bound to the parser. Its stream-read capabilities therefore staynull, and the first timeBaseNodeDeserializer._handleDuplicatePropertyconsultsStreamReadCapability.DUPLICATE_PROPERTIESit fails:Duplicate property names are valid JSON (RFC 8259) and occur in practice — for example in cache entries written by the Jackson 2 serializer, or by a polymorphic type whose
@JsonTypeInfo(include = As.PROPERTY)type-id name coincides with a bean property of the same name (Jackson 2 emitted both as duplicate keys). Such entries can no longer be read back under the Jackson 3 serializer — not even to be evicted.Reported in #3396, with a minimal reproduction there.
Fix
Bind the parser to the context before deserializing, so its stream-read capabilities are initialized. This mirrors how
ObjectMapper._readTreeAndClose(...)binds the parser viaassignAndReturnParser(...). The tree scan then applies Jackson's normal duplicate-property handling (last value wins) instead of dereferencing anull.Test
GenericJacksonJsonRedisSerializerUnitTests.deserializeShouldHandleDuplicatePropertyWithDefaultTypingdeserializes a value containing a duplicate property. It reproduces theNullPointerExceptionabove without the fix and passes with it.Closes #3396