From 5cef4745f70615168d52dafc187f897da188c032 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sat, 1 Jul 2017 10:56:47 +0100 Subject: [PATCH] Polish new response fields test and add equivalent for request fields See gh-399 --- .../payload/RequestFieldsSnippetTests.java | 14 ++++++++++++++ .../payload/ResponseFieldsSnippetTests.java | 19 ++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java index d1c56f3b..cf2ee2f3 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java @@ -368,6 +368,20 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests { .build()); } + @Test + public void requestWithArrayContainingFieldThatIsSometimesNull() throws IOException { + this.snippets.expectRequestFields() + .withContents(tableWithHeader("Path", "Type", "Description") + .row("`assets[].name`", "`String`", "one")); + new RequestFieldsSnippet(Arrays.asList(fieldWithPath("assets[].name") + .description("one").type(JsonFieldType.STRING))) + .document(this.operationBuilder.request("http://localhost") + .content("{\"assets\": [" + "{\"name\": \"sample1\"}, " + + "{\"name\": null}, " + + "{\"name\": \"sample2\"}]}") + .build()); + } + private String escapeIfNecessary(String input) { if (this.templateFormat.equals(TemplateFormats.markdown())) { return input; diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java index 294f4028..1affb789 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java @@ -379,22 +379,19 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests { } @Test - public void responseWithSomeNullOcurrencesOfAField() throws IOException { + public void responseWithArrayContainingFieldThatIsSometimesNull() throws IOException { this.snippets.expectResponseFields() .withContents(tableWithHeader("Path", "Type", "Description") .row("`assets[].name`", "`String`", "one")); - new ResponseFieldsSnippet(Arrays.asList( - fieldWithPath("assets[].name").description("one"))) - .document(this.operationBuilder.response() - .content( - "{\"assets\": [" + - "{\"name\": \"sample1\"}, " + - "{\"name\": null}, " + - "{\"name\": \"sample2\"}]}") - .build()); + new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("assets[].name") + .description("one").type(JsonFieldType.STRING))) + .document(this.operationBuilder.response() + .content("{\"assets\": [" + "{\"name\": \"sample1\"}, " + + "{\"name\": null}, " + + "{\"name\": \"sample2\"}]}") + .build()); } - private String escapeIfNecessary(String input) { if (this.templateFormat.equals(TemplateFormats.markdown())) { return input;