Skip to content
Open
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 @@ -91,6 +91,12 @@ void compareTrees(JsonNode actual, JsonNode expected, Location pathToHere, List<
return;
}

boolean usingNullMatchedEmpty = findRule(pathToHere, TreeRule.NULL_MATCHES_EMPTY_ARRAY).isPresent();
if (usingNullMatchedEmpty && (
actual.isNull() && expected.isEmpty() || actual.isEmpty() && expected.isNull())) {
return;
}

// early exit if types don't match
if (actual.getNodeType() != expected.getNodeType()) {
failures.add(pathToHere.toString() + " different types: expected " + expected.getNodeType() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public enum TreeRule {
*/
ARRAY_CONTAINS,

/**
* Allow an empty array to match null and vice-versa
*/
NULL_MATCHES_EMPTY_ARRAY,

/**
* Skip over fields that are missing in the other object - implies keys in any order
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public WhereDsl<A> arrayContains() {
return whereDsl.pathRule(new PathRule(pathMatch, TreeRule.ARRAY_CONTAINS));
}

/**
* Allow empty array to match null and vice-versa
*
* @return <code>this</code> for fluent calling
*/
public WhereDsl<A> nullMatchesEmptyArray() {
return whereDsl.pathRule(new PathRule(pathMatch, TreeRule.NULL_MATCHES_EMPTY_ARRAY));
}

/**
* Ignore everything at this path
* @return the {@link WhereDsl} for fluent calling, with this path ignored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public WhereDsl<A> arrayContains() {
return pathRule(new PathRule(TreeRule.ARRAY_CONTAINS));
}

/**
* Allow empty array to match null and vice-versa
*
* @return <code>this</code> for fluent calling
*/
public WhereDsl<A> nullMatchesEmptyArray() {
return pathRule(new PathRule(TreeRule.NULL_MATCHES_EMPTY_ARRAY));
}

/**
* Add common configuration to the where dsl
* @param configurer the configurer to use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,11 @@ void whenUsingArrayNodeWithArrayThenIsNotEmptyWorks() {
assertJson("{foo:[\"bar\"]}")
.at("/foo").array().isNotEmpty();
}

@Test
void emptyArrayCanBeConfiguredToMatchNull() {
assertJson("{foo: null}")
.where().nullMatchesEmptyArray()
.isEqualTo("{foo: []}");
}
}