Skip to content

Commit 14fd258

Browse files
author
Ronald Holshausen
committedJan 25, 2020
fix: correct the type matcher to treat JsonNull as a null #981
1 parent afba8a6 commit 14fd258

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed
 

‎core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fun <M : Mismatch> matchEquality(
130130
mismatchFactory: MismatchFactory<M>
131131
): List<M> {
132132
val matches = when {
133-
actual == null && expected == null -> true
133+
(actual == null || actual is JsonNull) && (expected == null || expected is JsonNull) -> true
134134
actual is Element && expected is Element -> actual.tagName == expected.tagName
135135
else -> actual != null && actual == expected
136136
}
@@ -184,8 +184,8 @@ fun <M : Mismatch> matchType(
184184
(expected.isNumber && actual.isNumber) ||
185185
(expected.isString && actual.isString))) {
186186
emptyList()
187-
} else if (expected == null) {
188-
if (actual == null) {
187+
} else if (expected == null || expected is JsonNull) {
188+
if (actual == null || actual is JsonNull) {
189189
emptyList()
190190
} else {
191191
listOf(mismatchFactory.create(expected, actual, "Expected ${valueOf(actual)} to be null", path))

‎core/matchers/src/test/groovy/au/com/dius/pact/core/matchers/MatcherExecutorSpec.groovy

+23-16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import au.com.dius.pact.core.model.matchingrules.RegexMatcher
1111
import au.com.dius.pact.core.model.matchingrules.TimeMatcher
1212
import au.com.dius.pact.core.model.matchingrules.TimestampMatcher
1313
import au.com.dius.pact.core.model.matchingrules.TypeMatcher
14+
import com.google.gson.JsonNull
1415
import spock.lang.Specification
1516
import spock.lang.Unroll
1617

@@ -35,13 +36,16 @@ class MatcherExecutorSpec extends Specification {
3536
MatcherExecutorKt.domatch(EqualsMatcher.INSTANCE, path, expected, actual, mismatchFactory).empty == mustBeEmpty
3637

3738
where:
38-
expected | actual || mustBeEmpty
39-
'100' | '100' || true
40-
100 | '100' || false
41-
100 | 100 || true
42-
null | null || true
43-
'100' | null || false
44-
null | 100 || false
39+
expected | actual || mustBeEmpty
40+
'100' | '100' || true
41+
100 | '100' || false
42+
100 | 100 || true
43+
null | null || true
44+
'100' | null || false
45+
null | 100 || false
46+
JsonNull.INSTANCE | null || true
47+
null | JsonNull.INSTANCE || true
48+
JsonNull.INSTANCE | JsonNull.INSTANCE || true
4549
}
4650

4751
@Unroll
@@ -62,15 +66,18 @@ class MatcherExecutorSpec extends Specification {
6266
MatcherExecutorKt.domatch(TypeMatcher.INSTANCE, path, expected, actual, mismatchFactory).empty == mustBeEmpty
6367

6468
where:
65-
expected | actual || mustBeEmpty
66-
'Harry' | 'Some other string' || true
67-
100 | 200.3 || true
68-
true | false || true
69-
null | null || true
70-
'200' | 200 || false
71-
200 | null || false
72-
[100, 200, 300] | [200.3] || true
73-
[a: 100] | [a: 200.3, b: 200, c: 300] || true
69+
expected | actual || mustBeEmpty
70+
'Harry' | 'Some other string' || true
71+
100 | 200.3 || true
72+
true | false || true
73+
null | null || true
74+
'200' | 200 || false
75+
200 | null || false
76+
[100, 200, 300] | [200.3] || true
77+
[a: 100] | [a: 200.3, b: 200, c: 300] || true
78+
JsonNull.INSTANCE | null || true
79+
null | JsonNull.INSTANCE || true
80+
JsonNull.INSTANCE | JsonNull.INSTANCE || true
7481
}
7582

7683
@Unroll

0 commit comments

Comments
 (0)
Please sign in to comment.