From 5d29116133539b39db9401e790800629cf51e639 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Thu, 21 Sep 2023 12:36:54 +1000 Subject: [PATCH] fix: Unstable key generation with provider states #1717 --- .../com/dius/pact/core/model/ProviderState.kt | 4 ++-- .../pact/core/model/ProviderStateSpec.groovy | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/ProviderState.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/ProviderState.kt index f1e85cfd87..407c64b13b 100644 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/ProviderState.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/ProviderState.kt @@ -38,8 +38,8 @@ data class ProviderState @JvmOverloads constructor(val name: String?, val params fun uniqueKey(): Int { val builder = HashCodeBuilder().append(name) - for (param in params) { - builder.append(param.key) + for (param in params.keys.sorted()) { + builder.append(param) } return builder.toHashCode() } diff --git a/core/model/src/test/groovy/au/com/dius/pact/core/model/ProviderStateSpec.groovy b/core/model/src/test/groovy/au/com/dius/pact/core/model/ProviderStateSpec.groovy index 6a8acb35a2..2408a231cf 100644 --- a/core/model/src/test/groovy/au/com/dius/pact/core/model/ProviderStateSpec.groovy +++ b/core/model/src/test/groovy/au/com/dius/pact/core/model/ProviderStateSpec.groovy @@ -1,5 +1,6 @@ package au.com.dius.pact.core.model +import spock.lang.Issue import spock.lang.Specification import spock.lang.Unroll @@ -41,4 +42,21 @@ class ProviderStateSpec extends Specification { state1.uniqueKey() != state3.uniqueKey() state1.uniqueKey() != state4.uniqueKey() } + + @Issue("#1717") + def 'uniqueKey should be deterministic'() { + given: + def state = new ProviderState('a user profile exists', [ + email_address: 'test@email.com', + family_name: 'Test' + ]) + def state2 = new ProviderState('a user profile exists', [ + family_name: 'Test', + email_address: 'test@email.com' + ]) + + expect: + state.uniqueKey() == state.uniqueKey() + state.uniqueKey() == state2.uniqueKey() + } }