Skip to content

Commit 5a05cdb

Browse files
committed
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. Issue: SPR-13320
1 parent 8afea1b commit 5a05cdb

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,6 @@ private Object assertExistsAndReturn(String content) throws ParseException {
238238
Object value = evaluateJsonPath(content);
239239
String reason = "No value for JSON path \"" + this.expression + "\"";
240240
assertTrue(reason, value != null);
241-
if (List.class.isInstance(value)) {
242-
assertTrue(reason, !((List<?>) value).isEmpty());
243-
}
244241
return value;
245242
}
246243

spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public void assertValueIsArray() throws Exception {
106106
new JsonPathExpectationsHelper("$.arr").assertValueIsArray(CONTENT);
107107
}
108108

109+
@Test
110+
public void assertValueIsArrayForAnEmptyArray() throws Exception {
111+
new JsonPathExpectationsHelper("$.emptyArray").assertValueIsArray(CONTENT);
112+
}
113+
109114
@Test
110115
public void assertValueIsArrayForNonArray() throws Exception {
111116
exception.expect(AssertionError.class);
@@ -117,6 +122,11 @@ public void assertValueIsMap() throws Exception {
117122
new JsonPathExpectationsHelper("$.colorMap").assertValueIsMap(CONTENT);
118123
}
119124

125+
@Test
126+
public void assertValueIsMapForAnEmptyMap() throws Exception {
127+
new JsonPathExpectationsHelper("$.emptyMap").assertValueIsMap(CONTENT);
128+
}
129+
120130
@Test
121131
public void assertValueIsMapForNonMap() throws Exception {
122132
exception.expect(AssertionError.class);

spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public class JsonPathResultMatchersTests {
3434

35-
private static final String RESPONSE_CONTENT = "{\"foo\": \"bar\", \"qux\": [\"baz\"], \"emptyArray\": [], \"icanhaz\": true, \"howmanies\": 5, \"cheeseburger\": {\"pickles\": true} }";
35+
private static final String RESPONSE_CONTENT = "{\"foo\": \"bar\", \"qux\": [\"baz\"], \"emptyArray\": [], \"icanhaz\": true, \"howmanies\": 5, \"cheeseburger\": {\"pickles\": true}, \"emptyMap\": {} }";
3636

3737
private static final StubMvcResult stubMvcResult;
3838

@@ -93,13 +93,32 @@ public void doesNotExistNoMatch() throws Exception {
9393
public void isArray() throws Exception {
9494
new JsonPathResultMatchers("$.qux").isArray().match(stubMvcResult);
9595
}
96+
97+
@Test
98+
public void isArrayForAnEmptyArray() throws Exception {
99+
new JsonPathResultMatchers("$.emptyArray").isArray().match(stubMvcResult);
96100
}
97101

98102
@Test(expected = AssertionError.class)
99103
public void isArrayNoMatch() throws Exception {
100104
new JsonPathResultMatchers("$.bar").isArray().match(stubMvcResult);
101105
}
102106

107+
@Test
108+
public void isMap() throws Exception {
109+
new JsonPathResultMatchers("$.cheeseburger").isMap().match(stubMvcResult);
110+
}
111+
112+
@Test
113+
public void isMapForAnEmptyMap() throws Exception {
114+
new JsonPathResultMatchers("$.emptyMap").isMap().match(stubMvcResult);
115+
}
116+
117+
@Test(expected = AssertionError.class)
118+
public void isMapNoMatch() throws Exception {
119+
new JsonPathResultMatchers("$.foo").isMap().match(stubMvcResult);
120+
}
121+
103122
@Test
104123
public void isBoolean() throws Exception {
105124
new JsonPathResultMatchers("$.icanhaz").isBoolean().match(stubMvcResult);
@@ -120,16 +139,6 @@ public void isNumberNoMatch() throws Exception {
120139
new JsonPathResultMatchers("$.foo").isNumber().match(stubMvcResult);
121140
}
122141

123-
@Test
124-
public void isMap() throws Exception {
125-
new JsonPathResultMatchers("$.cheeseburger").isMap().match(stubMvcResult);
126-
}
127-
128-
@Test(expected = AssertionError.class)
129-
public void isMapNoMatch() throws Exception {
130-
new JsonPathResultMatchers("$.foo").isMap().match(stubMvcResult);
131-
}
132-
133142
@Test
134143
public void isString() throws Exception {
135144
new JsonPathResultMatchers("$.foo").isString().match(stubMvcResult);

0 commit comments

Comments
 (0)