You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In JsonPathExpectationsHelper, the exists() method asserts that the evaluated JsonPath expression results in a value; however, if the value is an array the exists() method throws an AssertionError if the array exists but is empty. Similarly, the doesNotExist() method does not throw an AssertionError if an array exists but is empty.
This specialized treatment for arrays is illogical and inconsistent with the treatment of empty strings and empty maps.
Analysis
It turns out that existing specialized treatment of arrays is a result of the fact that the JsonPath library always returns an empty array if the path is an indefinite path that does not evaluate to a specific result. This can be the case when a JsonPath expression uses a filter to select results based on a predicate for which there is no match in the content. Consult the discussion on what is returned when in the JsonPath documentation for details.
Deliverables
Ensure that empty arrays are considered existent when the JsonPath is definite but nonexistent when the JsonPath is indefinite, in line with current support for empty strings and empty maps.
Introduce a new mechanism for asserting that an array, map, or string is empty.
Consider empty arrays as existent in JsonPath assertions
Prior to this commit, a JsonPath assertion that a path expression evaluated to an array in JsonPathExpectationsHelper (and therefore indirectly in JsonPathResultMatchers in Spring MVC Test) would incorrectly fail if the array was present in the JSON content but empty.
This commit fixes this issue by removing the "not empty" check for arrays and lists.
Differentiate b/t (in)definite results in JsonPath assertions
Prior to this commit, the exists() method in JsonPathExpectationsHelper
correctly asserted that the evaluated JsonPath expression resulted in a
value (i.e., that a non-null value exists); however, if the value was
an empty array, the exists() method always threw an AssertionError.
The existing behavior makes sense if the JsonPath expression is
'indefinite' -- for example, if the expression uses a filter to select
results based on a predicate for which there is no match in the JSON
document, but the existing behavior is illogical and therefore invalid
if the JsonPath expression is 'definite' (i.e., directly references an
array in the JSON document that exists but happens to be empty). For
example, prior to this commit, the following threw an AssertionError.
new JsonPathExpectationsHelper("$.arr").exists("{ 'arr': [] }");
Similar arguments can be made for the doesNotExist() method.
After thorough analysis of the status quo, it has become apparent that
the existing specialized treatment of arrays is a result of the fact
that the JsonPath library always returns an empty list if the path is
an 'indefinite' path that does not evaluate to a specific result.
Consult the discussion on "What is Returned When?" in the JsonPath
documentation for details:
This commit addresses these issues by ensuring that empty arrays are
considered existent if the JsonPath expression is definite but
nonexistent if the expression is indefinite.
Uh oh!
There was an error while loading. Please reload this page.
Sam Brannen opened SPR-13351 and commented
Status Quo
In
JsonPathExpectationsHelper
, theexists()
method asserts that the evaluatedJsonPath
expression results in a value; however, if the value is an array theexists()
method throws anAssertionError
if the array exists but is empty. Similarly, thedoesNotExist()
method does not throw anAssertionError
if an array exists but is empty.This specialized treatment for arrays is illogical and inconsistent with the treatment of empty strings and empty maps.
Analysis
It turns out that existing specialized treatment of arrays is a result of the fact that the
JsonPath
library always returns an empty array if the path is an indefinite path that does not evaluate to a specific result. This can be the case when a JsonPath expression uses a filter to select results based on a predicate for which there is no match in the content. Consult the discussion on what is returned when in the JsonPath documentation for details.Deliverables
Affects: 3.2 GA
Issue Links:
Referenced from: commits 07bb037
The text was updated successfully, but these errors were encountered: