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 5f07e6dd..fd0a4c5d 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 @@ -92,12 +92,12 @@ public class LinksSnippetTests { } @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(new OperationBuilder("documented-optional-link", + .document(new OperationBuilder("present-optional-link", this.snippet.getOutputDirectory()).build()); } 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 52326fdb..216b5ab7 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 @@ -129,6 +129,32 @@ public class RequestFieldsSnippetTests { .content("{}").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(new OperationBuilder("missing-optional-request-field", + this.snippet.getOutputDirectory()) + .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(new OperationBuilder("present-optional-request-field", + this.snippet.getOutputDirectory()) + .request("http://localhost") + .content("{\"a\": { \"b\": \"bravo\"}}").build()); + } + @Test public void missingOptionalRequestFieldWithNoTypeProvided() throws IOException { this.thrown.expect(FieldTypeRequiredException.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 6c3eff3d..1356d7b3 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 @@ -121,6 +121,30 @@ public class ResponseFieldsSnippetTests { .content("{\"a\": 5, \"b\": 4}").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(new OperationBuilder("missing-optional-response-field", + this.snippet.getOutputDirectory()).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(new OperationBuilder("present-optional-response-field", + this.snippet.getOutputDirectory()).response() + .content("{\"a\": { \"b\": \"bravo\"}}").build()); + } + @Test public void responseFieldsWithCustomDescriptorAttributes() throws IOException { TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);