Skip to content

Commit 83d1e70

Browse files
committed
fix: correct hash code calculation for case-insensitive map entries
1 parent 1af0e82 commit 83d1e70

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ 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 = 17 * 31 + key!!.hashCode() + value!!.hashCode()
67+
override fun hashCode(): Int = key.hashCode() xor value.hashCode()
6768

6869
override fun equals(other: Any?): Boolean {
6970
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)