Skip to content

Commit

Permalink
Fix inPath error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Nov 29, 2024
1 parent 2e18570 commit 080411f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package net.javacrumbs.jsonunit.jsonpath;

import static com.jayway.jsonpath.Configuration.defaultConfiguration;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getPathPrefix;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.jsonSource;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.missingNode;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject;
Expand All @@ -37,13 +38,25 @@ private JsonPathAdapter() {}

@NotNull
public static Object inPath(@Nullable Object json, @NotNull String path) {
String normalizedPath = fromBracketNotation(path);
try {
MatchRecordingListener recordingListener = new MatchRecordingListener();
Object value = readValue(defaultConfiguration().addEvaluationListeners(recordingListener), json, path);
return jsonSource(wrapDeserializedObject(value), normalizedPath, recordingListener.getMatchingPaths());
return jsonSource(wrapDeserializedObject(value), concatJsonPaths(json, path), recordingListener.getMatchingPaths());
} catch (PathNotFoundException e) {
return jsonSource(missingNode(), normalizedPath);
return jsonSource(missingNode(), concatJsonPaths(json, path));
}
}

private static @NotNull String concatJsonPaths(@Nullable Object json, @NotNull String path) {
String newPathSegment = fromBracketNotation(path);
String pathPrefix = getPathPrefix(json);
if (pathPrefix.isEmpty()) {
return newPathSegment;
}
if (newPathSegment.startsWith("$.")) {
return pathPrefix + newPathSegment.substring(1);
} else {
return pathPrefix + newPathSegment;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import java.util.function.Consumer;
import java.util.function.Function;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.internal.JsonUtils;
import net.javacrumbs.jsonunit.core.internal.Path;
import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher;
import org.jetbrains.annotations.NotNull;

import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getPathPrefix;

abstract class AbstractSpringMatcher {
private final Path path;
private final Configuration configuration;
Expand All @@ -40,6 +43,8 @@ abstract class AbstractSpringMatcher {
}

void doMatch(Object actual) {
matcher.accept(new InternalMatcher(jsonTransformer.apply(actual), path, "", configuration));
Object json = jsonTransformer.apply(actual);
String pathPrefix = JsonUtils.getPathPrefix(json);
matcher.accept(new InternalMatcher(json, pathPrefix.isEmpty() ? path : Path.create("", pathPrefix), "", configuration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ME node(String newPath) {
*/
@NotNull
public ME inPath(String path) {
return matchers(this.path, configuration, json -> JsonPathAdapter.inPath(json, path));
return matchers(this.path, configuration, json -> JsonPathAdapter.inPath(jsonTransformer.apply(json), path));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.List;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {SpringConfig.class})
Expand Down Expand Up @@ -84,10 +85,29 @@ void shouldSupportJsonPathError() throws Exception {
exec("/sampleProduces").andExpect(json().inPath("$.result.array[1]").isEqualTo(3))
).hasMessage("""
JSON documents are different:
Different value found in node "", expected: <3> but was: <2>.
Different value found in node "$.result.array[1]", expected: <3> but was: <2>.
""");
}

@Test
void shouldSupportJsonPathChainedError() {
assertThatThrownBy(() ->
exec("/sampleProduces").andExpect(json().inPath("$.result").inPath("$.array[*]").isEqualTo(List.of(1, 3, 3)))
).hasMessage("""
JSON documents are different:
Different value found in node "$.result.array[*][1]", expected: <3> but was: <2>.
""");
}

@Test
void shouldSupportJsonPathChainedWithNodeError() {
assertThatThrownBy(() ->
exec("/sampleProduces").andExpect(json().node("result").inPath("$.array[1]").isEqualTo(3))
).hasMessage("""
JSON documents are different:
Different value found in node "$.result.array[*][1]", expected: <3> but was: <2>.
""");
}

@Test
void shouldPassIfEqualsWithIsoEncoding() throws Exception {
Expand Down

0 comments on commit 080411f

Please sign in to comment.