Skip to content

Commit 5cef474

Browse files
committed
Polish new response fields test and add equivalent for request fields
See gh-399
1 parent 288ebf7 commit 5cef474

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,20 @@ public void mapRequestWithVaryingKeysMatchedUsingWildcard() throws IOException {
368368
.build());
369369
}
370370

371+
@Test
372+
public void requestWithArrayContainingFieldThatIsSometimesNull() throws IOException {
373+
this.snippets.expectRequestFields()
374+
.withContents(tableWithHeader("Path", "Type", "Description")
375+
.row("`assets[].name`", "`String`", "one"));
376+
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("assets[].name")
377+
.description("one").type(JsonFieldType.STRING)))
378+
.document(this.operationBuilder.request("http://localhost")
379+
.content("{\"assets\": [" + "{\"name\": \"sample1\"}, "
380+
+ "{\"name\": null}, "
381+
+ "{\"name\": \"sample2\"}]}")
382+
.build());
383+
}
384+
371385
private String escapeIfNecessary(String input) {
372386
if (this.templateFormat.equals(TemplateFormats.markdown())) {
373387
return input;

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,22 +379,19 @@ public void mapResponseWithVaryingKeysMatchedUsingWildcard() throws IOException
379379
}
380380

381381
@Test
382-
public void responseWithSomeNullOcurrencesOfAField() throws IOException {
382+
public void responseWithArrayContainingFieldThatIsSometimesNull() throws IOException {
383383
this.snippets.expectResponseFields()
384384
.withContents(tableWithHeader("Path", "Type", "Description")
385385
.row("`assets[].name`", "`String`", "one"));
386-
new ResponseFieldsSnippet(Arrays.asList(
387-
fieldWithPath("assets[].name").description("one")))
388-
.document(this.operationBuilder.response()
389-
.content(
390-
"{\"assets\": [" +
391-
"{\"name\": \"sample1\"}, " +
392-
"{\"name\": null}, " +
393-
"{\"name\": \"sample2\"}]}")
394-
.build());
386+
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("assets[].name")
387+
.description("one").type(JsonFieldType.STRING)))
388+
.document(this.operationBuilder.response()
389+
.content("{\"assets\": [" + "{\"name\": \"sample1\"}, "
390+
+ "{\"name\": null}, "
391+
+ "{\"name\": \"sample2\"}]}")
392+
.build());
395393
}
396394

397-
398395
private String escapeIfNecessary(String input) {
399396
if (this.templateFormat.equals(TemplateFormats.markdown())) {
400397
return input;

0 commit comments

Comments
 (0)