From f5246573c326afa7e4c2f23bff4debea42f5bd0d Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Tue, 20 Feb 2024 08:51:11 +0100 Subject: [PATCH] #483 Support JsonPath in whenIgnoringPaths --- .../javacrumbs/jsonunit/core/internal/PathMatcher.java | 2 +- .../jsonunit/test/base/AbstractAssertJTest.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/PathMatcher.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/PathMatcher.java index b7b1ae1c..34ce57e3 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/PathMatcher.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/PathMatcher.java @@ -56,7 +56,7 @@ private static class SimplePathMatcher extends PathMatcher { @Override boolean matches(String pathToMatch) { - return path.equals(pathToMatch) || path.equals("$." + pathToMatch); + return path.equals(pathToMatch) || path.equals("$." + pathToMatch) || path.equals("$" + pathToMatch); } } diff --git a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java index e04edfa5..e5fb8a3a 100644 --- a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java +++ b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java @@ -678,6 +678,14 @@ void shouldIgnoreArrayElement() { assertThatJson("{\"root\":[0, 1, 2]}").whenIgnoringPaths("root[1]").isEqualTo("{\"root\":[0, 8, 2]}"); } + @Test + void shouldIgnoreJsonPaths() { + String expected = "[{\"name\":\"123\",\"age\":2},{\"name\":\"321\",\"age\":5}]"; + String actual = "[{\"name\":\"123\",\"age\":5},{\"name\":\"321\",\"age\":8}]"; + + assertThatJson(expected).whenIgnoringPaths("$..age").isEqualTo(actual); + } + @Test void arraySimpleIgnoringOrderNotEqualComparison() { assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}")