Skip to content

Commit 120768c

Browse files
authored
fix: correct hash code calculation for case-insensitive map entries (#1238)
* fix: correct hash code calculation for case-insensitive map entries * remove commented-out code
1 parent 1af0e82 commit 120768c

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "c0040355-ffdc-4813-80e9-baf859ef02b9",
3+
"type": "bugfix",
4+
"description": "fix: correct hash code calculation for case-insensitive map entries"
5+
}

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMap.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal class CaseInsensitiveMap<Value> : MutableMap<String, Value> {
6363
return value
6464
}
6565

66-
override fun hashCode(): Int = 17 * 31 + key!!.hashCode() + value!!.hashCode()
66+
override fun hashCode(): Int = key.hashCode() xor value.hashCode() // Match JVM & K/N stdlib implementations
6767

6868
override fun equals(other: Any?): Boolean {
6969
if (other == null || other !is Map.Entry<*, *>) return false

runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMapTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ class CaseInsensitiveMapTest {
7676
assertEquals(left.entries, right.entries)
7777
}
7878

79+
@Test
80+
fun testEntriesEqualityWithNormalMap() {
81+
val left = CaseInsensitiveMap<String>()
82+
left["A"] = "apple"
83+
left["B"] = "banana"
84+
left["C"] = "cherry"
85+
86+
val right = mutableMapOf(
87+
"c" to "cherry",
88+
"b" to "banana",
89+
"a" to "apple",
90+
)
91+
92+
assertEquals(left.entries, right.entries)
93+
}
94+
7995
@Test
8096
fun testToString() {
8197
val map = CaseInsensitiveMap<String>()

0 commit comments

Comments
 (0)