diff --git a/.changes/c0040355-ffdc-4813-80e9-baf859ef02b9.json b/.changes/c0040355-ffdc-4813-80e9-baf859ef02b9.json new file mode 100644 index 0000000000..42302b920d --- /dev/null +++ b/.changes/c0040355-ffdc-4813-80e9-baf859ef02b9.json @@ -0,0 +1,5 @@ +{ + "id": "c0040355-ffdc-4813-80e9-baf859ef02b9", + "type": "bugfix", + "description": "fix: correct hash code calculation for case-insensitive map entries" +} \ No newline at end of file diff --git a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMap.kt b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMap.kt index 7e6d6751f9..e888ad72bc 100644 --- a/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMap.kt +++ b/runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMap.kt @@ -63,7 +63,7 @@ internal class CaseInsensitiveMap : MutableMap { return value } - override fun hashCode(): Int = 17 * 31 + key!!.hashCode() + value!!.hashCode() + override fun hashCode(): Int = key.hashCode() xor value.hashCode() // Match JVM & K/N stdlib implementations override fun equals(other: Any?): Boolean { if (other == null || other !is Map.Entry<*, *>) return false diff --git a/runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMapTest.kt b/runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMapTest.kt index a36d8ebc31..96fd83612b 100644 --- a/runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMapTest.kt +++ b/runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/collections/CaseInsensitiveMapTest.kt @@ -76,6 +76,22 @@ class CaseInsensitiveMapTest { assertEquals(left.entries, right.entries) } + @Test + fun testEntriesEqualityWithNormalMap() { + val left = CaseInsensitiveMap() + left["A"] = "apple" + left["B"] = "banana" + left["C"] = "cherry" + + val right = mutableMapOf( + "c" to "cherry", + "b" to "banana", + "a" to "apple", + ) + + assertEquals(left.entries, right.entries) + } + @Test fun testToString() { val map = CaseInsensitiveMap()