Repository navigation
Conversation
SingleToMultiValueMapAdapter and MultiToSingleValueMapAdapter expose adapted entry values (singleton lists and first values, respectively) through entrySet() and equals(), but delegated hashCode() to the target map whose entry values differ. As a consequence, an adapter could be equal to a regular map while having a different hash code. This commit computes hashCode() from the adapted entries, as defined by the Map.hashCode() contract. Closes spring-projectsgh-37469 Signed-off-by: lejuho <zedd1324@gmail.com>
This branch has not been deployed
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.
Overview
Fixes #37469.
SingleToMultiValueMapAdapter(returned byMultiValueMap.fromSingleValue()) exposes singleton-list values throughentrySet()and compares those inequals(), buthashCode()delegated to the underlying single-value map. As a result, the adapter could be equal to a regularMap<K, List<V>>with the same entries while reporting a different hash code, breaking theMap.hashCode()contract and lookups in hash-based collections.The symmetric
MultiToSingleValueMapAdapter(returned byMultiValueMap.asSingleValueMap()) has the same problem in the opposite direction: it exposes the first value per key, buthashCode()delegated to the underlying multi-value map.Changes
SingleToMultiValueMapAdapter.hashCode()now sums the hash codes of its adapted entries.MultiToSingleValueMapAdapter.hashCode()now sumshash(key) ^ hash(firstValue)for each entry, using the same first-value adaptation asget(), so keys mapped to an empty list contribute like anullvalue instead of failing.Testing
SingleToMultiValueMapAdapterTests: equal/hashCode/HashSetlookup against a regular map, including anullvalue.MultiValueMapTests:asSingleValueMap()equal/hashCode/HashSetlookup against a regular map, and hashCode with an empty value list, for all maps under test.Result: BUILD SUCCESSFUL. The new tests fail without the fix (12 failures).