Skip to content

Commit c0477a6

Browse files
committed
Merge pull request #732 from ddaaac
* gh-732: Polish "Improve exception message when subsection is empty array" Improve exception message when subsection is empty array Closes gh-732
2 parents bae549c + 6c0960d commit c0477a6

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 an empty section of the payload");
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 extractEmptyArraySubsection() throws JsonParseException, JsonMappingException, IOException {
188+
assertThatThrownBy(() -> new FieldPathPayloadSubsectionExtractor("a")
189+
.extractSubsection("{\"a\":[]}}".getBytes(), MediaType.APPLICATION_JSON))
190+
.isInstanceOf(PayloadHandlingException.class)
191+
.hasMessage("a identifies an empty section of the payload");
192+
}
193+
186194
}

0 commit comments

Comments
 (0)