[Java] Set deserialized as ArrayList in XLANG mode disrupts collection semantics #2105
Open
2 tasks done
Labels
bug
Something isn't working
Search before asking
Version
0.10.0
Component(s)
Java
Minimal reproduce step
What did you expect to see?
I expected the deserialized object to maintain the same collection type as the original object - a HashSet in this case. Since the field is declared as a Set, it should be deserialized as a Set implementation like HashSet, preserving the collection's semantics (uniqueness of elements, hash-based operations, etc.).
What did you see instead?
The deserialized object contains an ArrayList instead of a HashSet:
The Set is converted to an ArrayList during deserialization, which breaks the expected behavior of the Set interface (no duplicate elements, different performance characteristics for operations like contains()).
Anything Else?
I've traced the issue to the StructSerializer::getGenericType method which contains the following logic:
When the serializer detects a field is a Collection and its class is abstract (which includes interfaces like Set), it defaults to using ArrayList as the implementation rather than preserving the original collection type.
While this approach makes the code work (the elements are preserved), it changes the semantics of the collection. This can lead to unexpected behavior for code that relies on specific collection implementations or interfaces.
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: