From 5b32a3aa9805189190adebcf4e00dbb2a596bb9c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 5 May 2016 10:34:02 +0100 Subject: [PATCH] Optional path and req param descriptors should count as documented Previously, optional path and request parameter descriptors were ignored when checking that all of the parameters that are present had been documented. This lead to a false negative if a request or path parameter was present and was documented with an optional descriptor. This commit uses all of the descriptors, not just those that are not optional, when checking for undocumented parameters. Closes gh-228 --- .../request/AbstractParametersSnippet.java | 2 +- .../request/PathParametersSnippetTests.java | 18 ++++++++++++++++++ .../request/RequestParametersSnippetTests.java | 11 +++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java index 9d97737d..f2d5bd99 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java @@ -124,7 +124,7 @@ public abstract class AbstractParametersSnippet extends TemplatedSnippet { } else { undocumentedParameters = new HashSet<>(actualParameters); - undocumentedParameters.removeAll(expectedParameters); + undocumentedParameters.removeAll(this.descriptorsByName.keySet()); } Set missingParameters = new HashSet<>(expectedParameters); missingParameters.removeAll(actualParameters); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java index eac8c9d1..054ed7bc 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java @@ -99,6 +99,24 @@ public class PathParametersSnippetTests extends AbstractSnippetTests { "/{a}").build()); } + @Test + public void presentOptionalPathParameter() throws IOException { + this.snippet + .expectPathParameters( + "present-optional-path-parameter") + .withContents(tableWithTitleAndHeader( + this.templateFormat == TemplateFormats.asciidoctor() ? "/{a}" + : "`/{a}`", + "Parameter", "Description").row("a", "one")); + new PathParametersSnippet( + Arrays.asList(parameterWithName("a").description("one").optional())) + .document(operationBuilder("present-optional-path-parameter") + .attribute( + RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, + "/{a}") + .build()); + } + @Test public void pathParametersWithQueryString() throws IOException { this.snippet.expectPathParameters("path-parameters-with-query-string") diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java index 380f1cc3..2bd69444 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java @@ -104,6 +104,17 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests { .build()); } + @Test + public void presentOptionalRequestParameter() throws IOException { + this.snippet.expectRequestParameters("present-optional-request-parameter") + .withContents( + tableWithHeader("Parameter", "Description").row("a", "one")); + new RequestParametersSnippet( + Arrays.asList(parameterWithName("a").description("one").optional())) + .document(operationBuilder("present-optional-request-parameter") + .request("http://localhost").param("a", "one").build()); + } + @Test public void requestParametersWithCustomAttributes() throws IOException { TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);