Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 0e62ce8

Browse files
Jean-Pierre Bergaminfbenz
authored andcommitted
defaultValue for @RequestParam implies required=false (#179)
* defaultValue for @RequestParam implies required=false This was not handled correctly for non primitive types annotated with @RequestParam. * Restructure condition to enhance redability
1 parent 9ab996a commit 0e62ce8

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/request/RequestParametersSnippet.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,19 @@ public RequestParametersSnippet failOnUndocumentedParams(boolean failOnUndocumen
4646

4747
@Override
4848
protected boolean isRequired(MethodParameter param, RequestParam annot) {
49-
return param.getParameterType().isPrimitive()
50-
? ValueConstants.DEFAULT_NONE.equals(annot.defaultValue())
51-
: annot.required();
49+
if (hasDefaultValue(annot)) {
50+
// Having a defaultValue set implies required=false
51+
return false;
52+
} else if (param.getParameterType().isPrimitive()) {
53+
// A primitive type is required if no defaultValue is set, regardless of the value of the required flag
54+
return true;
55+
} else {
56+
return annot.required();
57+
}
58+
}
59+
60+
private static boolean hasDefaultValue(RequestParam annot) {
61+
return !ValueConstants.DEFAULT_NONE.equals(annot.defaultValue());
5262
}
5363

5464
@Override

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/request/RequestParametersSnippetTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,27 @@ public void simpleRequestWithPrimitivesDefaultValueParameterNotDocumented() thro
118118
.build());
119119
}
120120

121+
@Test
122+
public void simpleRequestWithStringDefaultValueParameter() throws Exception {
123+
HandlerMethod handlerMethod = createHandlerMethod("searchItem2String", double.class,
124+
boolean.class, String.class);
125+
initParameters(handlerMethod);
126+
mockParamComment("searchItem2String", "param1", "A decimal");
127+
mockParamComment("searchItem2String", "param2", "A boolean");
128+
mockParamComment("searchItem2String", "param3", "A String");
129+
130+
this.snippets.expect(REQUEST_PARAMETERS).withContents(
131+
tableWithHeader("Parameter", "Type", "Optional", "Description")
132+
.row("param1", "Decimal", "false", "A decimal.")
133+
.row("param2", "Boolean", "false", "A boolean.")
134+
.row("param3", "String", "true", "A String.\n\nDefault value: \"de\"."));
135+
136+
new RequestParametersSnippet().document(operationBuilder
137+
.attribute(HandlerMethod.class.getName(), handlerMethod)
138+
.attribute(JavadocReader.class.getName(), javadocReader)
139+
.attribute(ConstraintReader.class.getName(), constraintReader)
140+
.build());
141+
}
121142
@Test
122143
public void noParameters() throws Exception {
123144
HandlerMethod handlerMethod = createHandlerMethod("items");
@@ -239,6 +260,12 @@ public void searchItem2(@RequestParam double param1, // required
239260
// NOOP
240261
}
241262

263+
public void searchItem2String(@RequestParam double param1, // required
264+
@RequestParam(required = false) boolean param2, // required anyway
265+
@RequestParam(defaultValue = "de") String param3) { // not required
266+
// NOOP
267+
}
268+
242269
public void items() {
243270
// NOOP
244271
}

0 commit comments

Comments
 (0)