fix: deserialize generic collection types with correct element types#16272
Open
EvanYao826 wants to merge 2 commits into
Open
fix: deserialize generic collection types with correct element types#16272EvanYao826 wants to merge 2 commits into
EvanYao826 wants to merge 2 commits into
Conversation
Hessian2 deserializes all small integers as Integer, causing ClassCastException when the expected type is Byte, Short, etc. This happens because Dubbo passes only the erased Class (e.g., List.class) to the serialization framework, discarding the generic Type (e.g., List<Byte>). Fix: After deserialization, convert collection/map elements to match the declared generic type arguments. Handles: - Collection types (List, Set) with element type conversion - Map types with key/value type conversion - Numeric type narrowing (Byte, Short, Float, Long, Double, Integer) - Both boxed and primitive number types Fixes apache#16197
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16272 +/- ##
=============================================
- Coverage 60.85% 36.95% -23.91%
+ Complexity 11779 11761 -18
=============================================
Files 1953 1952 -1
Lines 89186 89234 +48
Branches 13454 13395 -59
=============================================
- Hits 54277 32974 -21303
- Misses 29353 51663 +22310
+ Partials 5556 4597 -959
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Fixes #16197
Problem
Hessian2 deserializes all small integers as
Integer, causingClassCastExceptionwhen the expected type isByte,Short, etc.Root cause: Dubbo passes only the erased
Class(e.g.,List.class) to the serialization framework during request deserialization, discarding the genericType(e.g.,List<Byte>). Hessian2 has no way to know the expected element type.Fix
In
Hessian2ObjectInput.readObjectOfType(), after deserializing with the erased class, check if the declared type is aParameterizedType. If so, convert collection/map elements to match the declared generic type arguments.Changes
Hessian2ObjectInput.java: AddedconvertGenericType()method that handles:List,Set) with element type conversionByte,Short,Float,Long,Double,Integer)ReflectionMethodDescriptor.java: AddedgetGenericParameterTypes()method to expose generic type informationDecodeableRpcInvocation.java: Pass genericTypeinstead of erasedClassto deserializationHessian2SerializationTest.java: Added comprehensive tests for:List<Byte>,List<Short>,List<Float>,List<Long>,List<Double>Map<String, Byte>,Map<String, Short>