Skip to content

Commit cbe6d77

Browse files
ddaaacwilkinsona
authored andcommitted
Improve exception message when subsection is empty array
See gh-732
1 parent bae549c commit cbe6d77

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public byte[] extractSubsection(byte[] payload, MediaType contentType, List<Fiel
9292
this::prependFieldPath));
9393
if (value instanceof List) {
9494
List<?> extractedList = (List<?>) value;
95+
if (extractedList.isEmpty()) {
96+
throw new PayloadHandlingException(this.fieldPath + " identifies a section of payload, but the section is empty");
97+
}
9598
JsonContentHandler contentHandler = new JsonContentHandler(payload, descriptorsByPath.values());
9699
Set<JsonFieldPath> uncommonPaths = JsonFieldPaths.from(extractedList).getUncommon().stream()
97100
.map((path) -> JsonFieldPath

spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,12 @@ public void extractNonExistentSubsection() throws JsonParseException, JsonMappin
183183
.hasMessage("a.c does not identify a section of the payload");
184184
}
185185

186+
@Test
187+
public void extractEmptySubsection() throws JsonParseException, JsonMappingException, IOException {
188+
assertThatThrownBy(() -> new FieldPathPayloadSubsectionExtractor("a")
189+
.extractSubsection("{\"a\":[]}}".getBytes(), MediaType.APPLICATION_JSON))
190+
.isInstanceOf(PayloadHandlingException.class)
191+
.hasMessage("a identifies a section of payload, but the section is empty");
192+
}
193+
186194
}

0 commit comments

Comments
 (0)