Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json path fix #862

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ public InternalMatcher(
this(actual, path, description, configuration, "Node \"" + path + "\"");
}

private InternalMatcher(@NotNull Object actual, @NotNull String pathPrefix) {
this(actual, Path.create("", pathPrefix), "", Configuration.empty());
}

@NotNull
public InternalMatcher whenIgnoringPaths(@NotNull String... pathsToBeIgnored) {
return new InternalMatcher(actual, path, description, configuration.whenIgnoringPaths(pathsToBeIgnored));
Expand Down
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,16 +38,31 @@ 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);
}
if (newPathSegment.startsWith("[")) {
return pathPrefix + newPathSegment;
}
return pathPrefix + "." + newPathSegment;
}

private static class MatchRecordingListener implements EvaluationListener {
private final List<String> matchingPaths = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ Different value found in node "test", expected: <2> but was: <1>."""
)
}

@Test
fun `Should assert chained inPath`() {
assertThrows<AssertionError> {
"""{"test": {"nested": 1}}""".inPath("test").inPath("nested") should equalJson("2")
}
.shouldHaveMessage(
"""JSON documents are different:
Different value found in node "test.nested", expected: <2> but was: <1>."""
)
}

@Test
fun `Should assert nested`() {
assertThrows<AssertionError> {
Expand All @@ -70,7 +81,7 @@ Different value found in node "test", expected: <2> but was: <1>."""
.shouldHaveMessage(
"""JSON in path "test"
JSON documents are different:
Different value found in node "nested", expected: <2> but was: <1>."""
Different value found in node "test.nested", expected: <2> but was: <1>."""
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,29 @@
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;

abstract class AbstractSpringMatcher {
private final Path path;
private final Configuration configuration;
private final Consumer<InternalMatcher> matcher;
private final Function<Object, Object> jsonTransformer;

AbstractSpringMatcher(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Consumer<InternalMatcher> matcher,
@NotNull Function<Object, Object> jsonTransformer) {
this.path = path;
this.configuration = configuration;
this.matcher = matcher;
this.jsonTransformer = jsonTransformer;
}

void doMatch(Object actual) {
matcher.accept(new InternalMatcher(jsonTransformer.apply(actual), path, "", configuration));
Object json = jsonTransformer.apply(actual);
String pathPrefix = JsonUtils.getPathPrefix(json);
Path path = Path.create("", pathPrefix);
matcher.accept(new InternalMatcher(json, path, "", configuration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.ConfigurationWhen;
import net.javacrumbs.jsonunit.core.Option;
import net.javacrumbs.jsonunit.core.internal.Path;
import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher;
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
import net.javacrumbs.jsonunit.jsonpath.JsonPathAdapter;
Expand All @@ -35,13 +34,10 @@
* @param <MATCHER> Type of the matcher
*/
abstract class AbstractSpringMatchers<ME, MATCHER> {
final Path path;
final Configuration configuration;
final Function<Object, Object> jsonTransformer;

AbstractSpringMatchers(
@NotNull Path path, @NotNull Configuration configuration, Function<Object, Object> jsonTransformer) {
this.path = path;
AbstractSpringMatchers(@NotNull Configuration configuration, Function<Object, Object> jsonTransformer) {
this.configuration = configuration;
this.jsonTransformer = jsonTransformer;
}
Expand All @@ -50,13 +46,10 @@ abstract class AbstractSpringMatchers<ME, MATCHER> {
abstract MATCHER matcher(@NotNull Consumer<InternalMatcher> matcher);

@NotNull
abstract ME matchers(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Function<Object, Object> jsonTransformer);
abstract ME matchers(@NotNull Configuration configuration, @NotNull Function<Object, Object> jsonTransformer);

protected ME matchers(@NotNull Path path, @NotNull Configuration configuration) {
return matchers(path, configuration, jsonTransformer);
protected ME matchers(@NotNull Configuration configuration) {
return matchers(configuration, jsonTransformer);
}

/**
Expand All @@ -70,16 +63,16 @@ protected ME matchers(@NotNull Path path, @NotNull Configuration configuration)
* @return object comparing only node given by path.
*/
@NotNull
public ME node(String newPath) {
return matchers(path.copy(newPath), configuration);
public ME node(String path) {
return inPath(path);
}

/**
* Uses JsonPath to extract values from the actual value.
*/
@NotNull
public ME inPath(String path) {
return matchers(this.path, configuration, json -> JsonPathAdapter.inPath(json, path));
return matchers(configuration, json -> JsonPathAdapter.inPath(jsonTransformer.apply(json), path));
}

/**
Expand All @@ -88,7 +81,7 @@ public ME inPath(String path) {
*/
@NotNull
public ME ignoring(@NotNull String ignorePlaceholder) {
return matchers(path, configuration.withIgnorePlaceholder(ignorePlaceholder));
return matchers(configuration.withIgnorePlaceholder(ignorePlaceholder));
}

/**
Expand All @@ -105,7 +98,7 @@ public ME withTolerance(double tolerance) {
*/
@NotNull
public ME withMatcher(@NotNull String matcherName, @NotNull Matcher<?> matcher) {
return matchers(path, configuration.withMatcher(matcherName, matcher));
return matchers(configuration.withMatcher(matcherName, matcher));
}

/**
Expand All @@ -114,12 +107,12 @@ public ME withMatcher(@NotNull String matcherName, @NotNull Matcher<?> matcher)
*/
@NotNull
public ME withTolerance(@Nullable BigDecimal tolerance) {
return matchers(path, configuration.withTolerance(tolerance));
return matchers(configuration.withTolerance(tolerance));
}

@NotNull
public ME withDifferenceListener(@NotNull DifferenceListener differenceListener) {
return matchers(path, configuration.withDifferenceListener(differenceListener));
return matchers(configuration.withDifferenceListener(differenceListener));
}

/**
Expand All @@ -130,7 +123,7 @@ public ME withDifferenceListener(@NotNull DifferenceListener differenceListener)
*/
@NotNull
public ME when(@NotNull Option firstOption, @NotNull Option... otherOptions) {
return matchers(path, configuration.withOptions(firstOption, otherOptions));
return matchers(configuration.withOptions(firstOption, otherOptions));
}

/**
Expand All @@ -141,7 +134,7 @@ public ME when(@NotNull Option firstOption, @NotNull Option... otherOptions) {
@NotNull
public ME when(
@NotNull ConfigurationWhen.PathsParam object, @NotNull ConfigurationWhen.ApplicableForPath... actions) {
return matchers(path, configuration.when(object, actions));
return matchers(configuration.when(object, actions));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.function.Consumer;
import java.util.function.Function;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.internal.Path;
import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.client.ClientHttpRequest;
Expand All @@ -38,40 +37,37 @@
*/
public class JsonUnitRequestMatchers extends AbstractSpringMatchers<JsonUnitRequestMatchers, RequestMatcher> {

private JsonUnitRequestMatchers(Path path, Configuration configuration, Function<Object, Object> jsonTransformer) {
super(path, configuration, jsonTransformer);
private JsonUnitRequestMatchers(Configuration configuration, Function<Object, Object> jsonTransformer) {
super(configuration, jsonTransformer);
}

@NotNull
@Override
RequestMatcher matcher(@NotNull Consumer<InternalMatcher> matcher) {
return new JsonRequestMatcher(path, configuration, matcher, jsonTransformer);
return new JsonRequestMatcher(configuration, matcher, jsonTransformer);
}

@Override
@NotNull
JsonUnitRequestMatchers matchers(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Function<Object, Object> jsonTransformer) {
return new JsonUnitRequestMatchers(path, configuration, jsonTransformer);
@NotNull Configuration configuration, @NotNull Function<Object, Object> jsonTransformer) {
return new JsonUnitRequestMatchers(configuration, jsonTransformer);
}

/**
* Creates JsonUnitResultMatchers to be used for JSON assertions.
*/
@NotNull
public static JsonUnitRequestMatchers json() {
return new JsonUnitRequestMatchers(Path.root(), Configuration.empty(), Function.identity());
return new JsonUnitRequestMatchers(Configuration.empty(), Function.identity());
}

private static class JsonRequestMatcher extends AbstractSpringMatcher implements RequestMatcher {
private JsonRequestMatcher(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Consumer<InternalMatcher> matcher,
@NotNull Function<Object, Object> jsonTransformer) {
super(path, configuration, matcher, jsonTransformer);
super(configuration, matcher, jsonTransformer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.function.Consumer;
import java.util.function.Function;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.internal.Path;
import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher;
import org.jetbrains.annotations.NotNull;
import org.springframework.mock.web.MockHttpServletResponse;
Expand All @@ -37,39 +36,36 @@
* </code>
*/
public class JsonUnitResultMatchers extends AbstractSpringMatchers<JsonUnitResultMatchers, ResultMatcher> {
private JsonUnitResultMatchers(Path path, Configuration configuration, Function<Object, Object> jsonTransformer) {
super(path, configuration, jsonTransformer);
private JsonUnitResultMatchers(Configuration configuration, Function<Object, Object> jsonTransformer) {
super(configuration, jsonTransformer);
}

/**
* Creates JsonUnitResultMatchers to be used for JSON assertions.
*/
public static JsonUnitResultMatchers json() {
return new JsonUnitResultMatchers(Path.root(), Configuration.empty(), Function.identity());
return new JsonUnitResultMatchers(Configuration.empty(), Function.identity());
}

@Override
@NotNull
ResultMatcher matcher(@NotNull Consumer<InternalMatcher> matcher) {
return new JsonResultMatcher(path, configuration, matcher, jsonTransformer);
return new JsonResultMatcher(configuration, matcher, jsonTransformer);
}

@Override
@NotNull
JsonUnitResultMatchers matchers(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Function<Object, Object> jsonTransformer) {
return new JsonUnitResultMatchers(path, configuration, jsonTransformer);
@NotNull Configuration configuration, @NotNull Function<Object, Object> jsonTransformer) {
return new JsonUnitResultMatchers(configuration, jsonTransformer);
}

private static class JsonResultMatcher extends AbstractSpringMatcher implements ResultMatcher {
private JsonResultMatcher(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Consumer<InternalMatcher> matcher,
@NotNull Function<Object, Object> jsonTransformer) {
super(path, configuration, matcher, jsonTransformer);
super(configuration, matcher, jsonTransformer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.function.Consumer;
import java.util.function.Function;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.internal.Path;
import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher;
import org.jetbrains.annotations.NotNull;
import org.springframework.test.web.reactive.server.EntityExchangeResult;
Expand All @@ -22,37 +21,34 @@
*/
public class WebTestClientJsonMatcher
extends AbstractSpringMatchers<WebTestClientJsonMatcher, Consumer<EntityExchangeResult<byte[]>>> {
private WebTestClientJsonMatcher(Path path, Configuration configuration, Function<Object, Object> jsonTransformer) {
super(path, configuration, jsonTransformer);
private WebTestClientJsonMatcher(Configuration configuration, Function<Object, Object> jsonTransformer) {
super(configuration, jsonTransformer);
}

public static WebTestClientJsonMatcher json() {
return new WebTestClientJsonMatcher(Path.root(), Configuration.empty(), Function.identity());
return new WebTestClientJsonMatcher(Configuration.empty(), Function.identity());
}

@Override
@NotNull
Consumer<EntityExchangeResult<byte[]>> matcher(@NotNull Consumer<InternalMatcher> matcher) {
return new JsonUnitWebTestClientMatcher(path, configuration, matcher, jsonTransformer);
return new JsonUnitWebTestClientMatcher(configuration, matcher, jsonTransformer);
}

@Override
@NotNull
WebTestClientJsonMatcher matchers(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Function<Object, Object> jsonTransformer) {
return new WebTestClientJsonMatcher(path, configuration, jsonTransformer);
@NotNull Configuration configuration, @NotNull Function<Object, Object> jsonTransformer) {
return new WebTestClientJsonMatcher(configuration, jsonTransformer);
}

private static class JsonUnitWebTestClientMatcher extends AbstractSpringMatcher
implements Consumer<EntityExchangeResult<byte[]>> {
private JsonUnitWebTestClientMatcher(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Consumer<InternalMatcher> matcher,
@NotNull Function<Object, Object> jsonTransformer) {
super(path, configuration, matcher, jsonTransformer);
super(configuration, matcher, jsonTransformer);
}

@Override
Expand Down
Loading