diff --git a/CHANGELOG.md b/CHANGELOG.md index 28f02918..68c8ad86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Next version (unreleased) -PUT_CHANGELOG_HERE +- The computation of cache keys when multiple key fields are used has changed to avoid potential collisions. Note: this can lead to cache misses after upgrading to this version. (#80) # Version 1.0.0-alpha.1 diff --git a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/api/CacheKey.kt b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/api/CacheKey.kt index a9a59290..3622da9f 100644 --- a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/api/CacheKey.kt +++ b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/api/CacheKey.kt @@ -25,9 +25,7 @@ value class CacheKey( buildString { append(typename) append(":") - values.forEach { - append(it) - } + append(values.joinToString("+") { it.replace("\\", "\\\\").replace("+", "\\+") }) } ) diff --git a/normalized-cache/src/commonTest/kotlin/com/apollographql/cache/normalized/CacheKeyTest.kt b/normalized-cache/src/commonTest/kotlin/com/apollographql/cache/normalized/CacheKeyTest.kt new file mode 100644 index 00000000..22ce2fc3 --- /dev/null +++ b/normalized-cache/src/commonTest/kotlin/com/apollographql/cache/normalized/CacheKeyTest.kt @@ -0,0 +1,21 @@ +package com.apollographql.cache.normalized + +import com.apollographql.cache.normalized.api.CacheKey +import kotlin.test.Test +import kotlin.test.assertNotEquals + +class CacheKeyTest { + @Test + fun noCollisions() { + assertNotEquals( + CacheKey("Person", "ann", "acorn").keyToString(), + CacheKey("Person", "anna", "corn").keyToString(), + ) + + assertNotEquals( + CacheKey("Type", "a+a", "b").keyToString(), + CacheKey("Type", "a\\", "a", "b").keyToString(), + ) + + } +}