diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java index 81ee8b4e..cf128d21 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java @@ -68,12 +68,12 @@ public class LinksSnippetTests extends AbstractSnippetTests { } @Test - public void documentedOptionalLink() throws IOException { - this.snippet.expectLinks("documented-optional-link").withContents( + public void presentOptionalLink() throws IOException { + this.snippet.expectLinks("present-optional-link").withContents( tableWithHeader("Relation", "Description").row("foo", "bar")); new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "blah")), Arrays.asList(new LinkDescriptor("foo").description("bar").optional())) - .document(operationBuilder("documented-optional-link").build()); + .document(operationBuilder("present-optional-link").build()); } @Test 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 9245d915..b7e0c872 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 @@ -98,7 +98,6 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests { this.snippet.expectRequestFields("ignore-all-undocumented") .withContents(tableWithHeader("Path", "Type", "Description").row("b", "Number", "Field b")); - new RequestFieldsSnippet(Arrays.asList(fieldWithPath("b").description("Field b")), true).document( operationBuilder("ignore-all-undocumented") @@ -106,6 +105,29 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests { .content("{\"a\": 5, \"b\": 4}").build()); } + @Test + public void missingOptionalRequestField() throws IOException { + this.snippet.expectRequestFields("missing-optional-request-field") + .withContents(tableWithHeader("Path", "Type", "Description").row("a.b", + "String", "one")); + new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one") + .type(JsonFieldType.STRING).optional())) + .document(operationBuilder("missing-optional-request-field") + .request("http://localhost").content("{}").build()); + } + + @Test + public void presentOptionalRequestField() throws IOException { + this.snippet.expectRequestFields("present-optional-request-field") + .withContents(tableWithHeader("Path", "Type", "Description").row("a.b", + "String", "one")); + new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one") + .type(JsonFieldType.STRING).optional())) + .document(operationBuilder("present-optional-request-field") + .request("http://localhost") + .content("{\"a\": { \"b\": \"bravo\"}}").build()); + } + @Test public void requestFieldsWithCustomAttributes() throws IOException { TemplateResourceResolver resolver = mock(TemplateResourceResolver.class); 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 8e777d3f..fb97e351 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 @@ -133,6 +133,28 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests { .response().content("{\"a\": \"foo\"}").build()); } + @Test + public void missingOptionalResponseField() throws IOException { + this.snippet.expectResponseFields("missing-optional-response-field") + .withContents(tableWithHeader("Path", "Type", "Description").row("a.b", + "String", "one")); + new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one") + .type(JsonFieldType.STRING).optional())) + .document(operationBuilder("missing-optional-response-field") + .response().content("{}").build()); + } + + @Test + public void presentOptionalResponseField() throws IOException { + this.snippet.expectResponseFields("present-optional-response-field") + .withContents(tableWithHeader("Path", "Type", "Description").row("a.b", + "String", "one")); + new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one") + .type(JsonFieldType.STRING).optional())).document( + operationBuilder("present-optional-response-field").response() + .content("{\"a\": { \"b\": \"bravo\"}}").build()); + } + @Test public void responseFieldsWithCustomDescriptorAttributes() throws IOException { TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);