Skip to content

Commit 5b32a3a

Browse files
committed
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
1 parent b15ee59 commit 5b32a3a

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void verifyParameterDescriptors(Operation operation) {
124124
}
125125
else {
126126
undocumentedParameters = new HashSet<>(actualParameters);
127-
undocumentedParameters.removeAll(expectedParameters);
127+
undocumentedParameters.removeAll(this.descriptorsByName.keySet());
128128
}
129129
Set<String> missingParameters = new HashSet<>(expectedParameters);
130130
missingParameters.removeAll(actualParameters);

spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ public void missingOptionalPathParameter() throws IOException {
9999
"/{a}").build());
100100
}
101101

102+
@Test
103+
public void presentOptionalPathParameter() throws IOException {
104+
this.snippet
105+
.expectPathParameters(
106+
"present-optional-path-parameter")
107+
.withContents(tableWithTitleAndHeader(
108+
this.templateFormat == TemplateFormats.asciidoctor() ? "/{a}"
109+
: "`/{a}`",
110+
"Parameter", "Description").row("a", "one"));
111+
new PathParametersSnippet(
112+
Arrays.asList(parameterWithName("a").description("one").optional()))
113+
.document(operationBuilder("present-optional-path-parameter")
114+
.attribute(
115+
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
116+
"/{a}")
117+
.build());
118+
}
119+
102120
@Test
103121
public void pathParametersWithQueryString() throws IOException {
104122
this.snippet.expectPathParameters("path-parameters-with-query-string")

spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ public void missingOptionalRequestParameter() throws IOException {
104104
.build());
105105
}
106106

107+
@Test
108+
public void presentOptionalRequestParameter() throws IOException {
109+
this.snippet.expectRequestParameters("present-optional-request-parameter")
110+
.withContents(
111+
tableWithHeader("Parameter", "Description").row("a", "one"));
112+
new RequestParametersSnippet(
113+
Arrays.asList(parameterWithName("a").description("one").optional()))
114+
.document(operationBuilder("present-optional-request-parameter")
115+
.request("http://localhost").param("a", "one").build());
116+
}
117+
107118
@Test
108119
public void requestParametersWithCustomAttributes() throws IOException {
109120
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);

0 commit comments

Comments
 (0)