Skip to content

Commit 3c870c3

Browse files
committed
Add support for placeholders for default value in @RequestParam Annotation. Fixes #904.
1 parent bd5b31c commit 3c870c3

File tree

8 files changed

+302
-219
lines changed

8 files changed

+302
-219
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractRequestBuilder.java

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@
6565
import org.springframework.util.CollectionUtils;
6666
import org.springframework.validation.BindingResult;
6767
import org.springframework.validation.Errors;
68-
import org.springframework.web.bind.annotation.CookieValue;
6968
import org.springframework.web.bind.annotation.PathVariable;
7069
import org.springframework.web.bind.annotation.RequestAttribute;
71-
import org.springframework.web.bind.annotation.RequestHeader;
7270
import org.springframework.web.bind.annotation.RequestMethod;
7371
import org.springframework.web.bind.annotation.RequestParam;
7472
import org.springframework.web.bind.annotation.ValueConstants;
@@ -245,7 +243,7 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
245243
io.swagger.v3.oas.annotations.Parameter.class);
246244

247245
final String pName = methodParameter.getParameterName();
248-
ParameterInfo parameterInfo = new ParameterInfo(pName, methodParameter);
246+
ParameterInfo parameterInfo = new ParameterInfo(pName, methodParameter, parameterBuilder);
249247

250248
if (parameterDoc == null)
251249
parameterDoc = parametersDocMap.get(parameterInfo.getpName());
@@ -412,40 +410,23 @@ public boolean isValidParameter(Parameter parameter) {
412410
public Parameter buildParams(ParameterInfo parameterInfo, Components components,
413411
RequestMethod requestMethod, JsonView jsonView) {
414412
MethodParameter methodParameter = parameterInfo.getMethodParameter();
415-
RequestHeader requestHeader = parameterInfo.getRequestHeader();
416-
RequestParam requestParam = parameterInfo.getRequestParam();
417-
PathVariable pathVar = parameterInfo.getPathVar();
418-
CookieValue cookieValue = parameterInfo.getCookieValue();
419-
420-
RequestInfo requestInfo;
421-
422-
if (requestHeader != null) {
423-
requestInfo = new RequestInfo(ParameterIn.HEADER.toString(), parameterInfo.getpName(), requestHeader.required(),
424-
requestHeader.defaultValue());
425-
return buildParam(parameterInfo, components, requestInfo, jsonView);
426-
427-
}
428-
else if (requestParam != null && !parameterBuilder.isFile(parameterInfo.getMethodParameter())) {
429-
requestInfo = new RequestInfo(ParameterIn.QUERY.toString(), parameterInfo.getpName(), requestParam.required() && !methodParameter.isOptional(),
430-
requestParam.defaultValue());
431-
return buildParam(parameterInfo, components, requestInfo, jsonView);
432-
}
433-
else if (pathVar != null) {
434-
requestInfo = new RequestInfo(ParameterIn.PATH.toString(), parameterInfo.getpName(), !methodParameter.isOptional(), null);
435-
return buildParam(parameterInfo, components, requestInfo, jsonView);
436-
}
437-
else if (cookieValue != null) {
438-
requestInfo = new RequestInfo(ParameterIn.COOKIE.toString(), parameterInfo.getpName(), cookieValue.required(),
439-
cookieValue.defaultValue());
440-
return buildParam(parameterInfo, components, requestInfo, jsonView);
413+
if (parameterInfo.getParamType() != null){
414+
if (!ValueConstants.DEFAULT_NONE.equals(parameterInfo.getDefaultValue()))
415+
parameterInfo.setRequired(false);
416+
else
417+
parameterInfo.setDefaultValue(null);
418+
return this.buildParam(parameterInfo, components, jsonView);
441419
}
442420
// By default
443421
DelegatingMethodParameter delegatingMethodParameter = (DelegatingMethodParameter) methodParameter;
444422
if (RequestMethod.GET.equals(requestMethod)
445423
|| (parameterInfo.getParameterModel() != null && (ParameterIn.PATH.toString().equals(parameterInfo.getParameterModel().getIn())))
446-
|| delegatingMethodParameter.isParameterObject())
447-
return this.buildParam(QUERY_PARAM, components, parameterInfo, !methodParameter.isOptional(), null, jsonView);
448-
424+
|| delegatingMethodParameter.isParameterObject()){
425+
parameterInfo.setRequired(!methodParameter.isOptional());
426+
parameterInfo.setParamType(QUERY_PARAM);
427+
parameterInfo.setDefaultValue(null);
428+
return this.buildParam(parameterInfo, components, jsonView);
429+
}
449430
return null;
450431
}
451432

@@ -454,39 +435,10 @@ else if (cookieValue != null) {
454435
*
455436
* @param parameterInfo the parameter info
456437
* @param components the components
457-
* @param requestInfo the request info
458-
* @param jsonView the json view
459-
* @return the parameter
460-
*/
461-
private Parameter buildParam(ParameterInfo parameterInfo, Components components, RequestInfo requestInfo,
462-
JsonView jsonView) {
463-
Parameter parameter;
464-
String pName = parameterInfo.getpName();
465-
String name = StringUtils.isBlank(requestInfo.value()) ? pName : requestInfo.value();
466-
parameterInfo.setpName(name);
467-
468-
if (!ValueConstants.DEFAULT_NONE.equals(requestInfo.defaultValue()))
469-
parameter = this.buildParam(requestInfo.type(), components, parameterInfo, false,
470-
requestInfo.defaultValue(), jsonView);
471-
else
472-
parameter = this.buildParam(requestInfo.type(), components, parameterInfo, requestInfo.required(), null,
473-
jsonView);
474-
return parameter;
475-
}
476-
477-
/**
478-
* Build param parameter.
479-
*
480-
* @param in the in
481-
* @param components the components
482-
* @param parameterInfo the parameter info
483-
* @param required the required
484-
* @param defaultValue the default value
485438
* @param jsonView the json view
486439
* @return the parameter
487440
*/
488-
private Parameter buildParam(String in, Components components, ParameterInfo parameterInfo, Boolean required,
489-
String defaultValue, JsonView jsonView) {
441+
private Parameter buildParam(ParameterInfo parameterInfo, Components components, JsonView jsonView) {
490442
Parameter parameter = parameterInfo.getParameterModel();
491443
String name = parameterInfo.getpName();
492444

@@ -499,19 +451,19 @@ private Parameter buildParam(String in, Components components, ParameterInfo par
499451
parameter.setName(name);
500452

501453
if (StringUtils.isBlank(parameter.getIn()))
502-
parameter.setIn(in);
454+
parameter.setIn(parameterInfo.getParamType());
503455

504-
if (required != null && parameter.getRequired() == null)
505-
parameter.setRequired(required);
456+
if (parameter.getRequired() == null)
457+
parameter.setRequired(parameterInfo.isRequired());
506458

507459
if (containsDeprecatedAnnotation(parameterInfo.getMethodParameter().getParameterAnnotations()))
508460
parameter.setDeprecated(true);
509461

510462
if (parameter.getSchema() == null && parameter.getContent() == null) {
511463
Schema<?> schema = parameterBuilder.calculateSchema(components, parameterInfo, null,
512464
jsonView);
513-
if (defaultValue != null)
514-
schema.setDefault(defaultValue);
465+
if (parameterInfo.getDefaultValue() != null)
466+
schema.setDefault(parameterInfo.getDefaultValue());
515467
parameter.setSchema(schema);
516468
}
517469
return parameter;

springdoc-openapi-common/src/main/java/org/springdoc/core/GenericParameterBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,13 @@ else if (type instanceof WildcardType) {
449449
}
450450
return false;
451451
}
452+
453+
/**
454+
* Gets property resolver utils.
455+
*
456+
* @return the property resolver utils
457+
*/
458+
public PropertyResolverUtils getPropertyResolverUtils() {
459+
return propertyResolverUtils;
460+
}
452461
}

springdoc-openapi-common/src/main/java/org/springdoc/core/ParameterInfo.java

Lines changed: 116 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.lang.reflect.Parameter;
2424

25+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
2526
import org.apache.commons.lang3.StringUtils;
2627

2728
import org.springframework.core.MethodParameter;
@@ -52,38 +53,54 @@ public class ParameterInfo {
5253
private io.swagger.v3.oas.models.parameters.Parameter parameterModel;
5354

5455
/**
55-
* The Request header.
56+
* The Required.
5657
*/
57-
private RequestHeader requestHeader;
58+
private boolean required;
5859

5960
/**
60-
* The Request param.
61+
* The Default value.
6162
*/
62-
private RequestParam requestParam;
63+
private String defaultValue;
6364

6465
/**
65-
* The Path var.
66+
* The Param type.
6667
*/
67-
private PathVariable pathVar;
68+
private String paramType;
6869

69-
/**
70-
* The Cookie value.
71-
*/
72-
private CookieValue cookieValue;
7370

7471
/**
7572
* Instantiates a new Parameter info.
7673
*
77-
* @param pName the p name
74+
* @param pName the parameter name
7875
* @param methodParameter the method parameter
76+
* @param parameterBuilder the parameter builder
7977
*/
80-
public ParameterInfo(String pName, MethodParameter methodParameter) {
78+
public ParameterInfo(String pName, MethodParameter methodParameter, GenericParameterBuilder parameterBuilder) {
79+
PropertyResolverUtils propertyResolverUtils = parameterBuilder.getPropertyResolverUtils();
80+
RequestHeader requestHeader = methodParameter.getParameterAnnotation(RequestHeader.class);
81+
RequestParam requestParam = methodParameter.getParameterAnnotation(RequestParam.class);
82+
PathVariable pathVar = methodParameter.getParameterAnnotation(PathVariable.class);
83+
CookieValue cookieValue = methodParameter.getParameterAnnotation(CookieValue.class);
84+
boolean isFile = parameterBuilder.isFile(methodParameter);
85+
8186
this.methodParameter = methodParameter;
82-
this.requestHeader = methodParameter.getParameterAnnotation(RequestHeader.class);
83-
this.requestParam = methodParameter.getParameterAnnotation(RequestParam.class);
84-
this.pathVar = methodParameter.getParameterAnnotation(PathVariable.class);
85-
this.cookieValue = methodParameter.getParameterAnnotation(CookieValue.class);
86-
this.pName = calculateName(pName, requestHeader, requestParam, pathVar, cookieValue);
87+
this.pName = pName;
88+
89+
if (requestHeader != null)
90+
calculateParams(requestHeader);
91+
else if (requestParam != null)
92+
calculateParams(requestParam, isFile);
93+
else if (pathVar != null)
94+
calculateParams(pathVar);
95+
else if (cookieValue != null)
96+
calculateParams(cookieValue);
97+
98+
if (StringUtils.isNotBlank(this.pName))
99+
this.pName = propertyResolverUtils.resolve(this.pName);
100+
if (StringUtils.isNotBlank(this.defaultValue))
101+
this.defaultValue = propertyResolverUtils.resolve(this.defaultValue);
102+
103+
this.required = this.required && !methodParameter.isOptional();
87104
}
88105

89106
/**
@@ -141,61 +158,109 @@ public void setParameterModel(io.swagger.v3.oas.models.parameters.Parameter para
141158
}
142159

143160
/**
144-
* Gets request header.
161+
* Is required boolean.
145162
*
146-
* @return the request header
163+
* @return the boolean
147164
*/
148-
public RequestHeader getRequestHeader() {
149-
return requestHeader;
165+
public boolean isRequired() {
166+
return required;
150167
}
151168

152169
/**
153-
* Gets request param.
170+
* Gets default value.
154171
*
155-
* @return the request param
172+
* @return the default value
156173
*/
157-
public RequestParam getRequestParam() {
158-
return requestParam;
174+
public String getDefaultValue() {
175+
return defaultValue;
159176
}
160177

161178
/**
162-
* Gets path var.
179+
* Gets param type.
163180
*
164-
* @return the path var
181+
* @return the param type
165182
*/
166-
public PathVariable getPathVar() {
167-
return pathVar;
183+
public String getParamType() {
184+
return paramType;
168185
}
169186

170187
/**
171-
* Gets cookie value.
188+
* Sets param type.
172189
*
173-
* @return the cookie value
190+
* @param paramType the param type
174191
*/
175-
public CookieValue getCookieValue() {
176-
return cookieValue;
192+
public void setParamType(String paramType) {
193+
this.paramType = paramType;
177194
}
178195

179196
/**
180-
* Calculate name string.
197+
* Sets required.
198+
*
199+
* @param required the required
200+
*/
201+
public void setRequired(boolean required) {
202+
this.required = required;
203+
}
204+
205+
/**
206+
* Sets default value.
207+
*
208+
* @param defaultValue the default value
209+
*/
210+
public void setDefaultValue(String defaultValue) {
211+
this.defaultValue = defaultValue;
212+
}
213+
214+
/**
215+
* Calculate params.
181216
*
182-
* @param pName the p name
183-
* @param requestHeader the request header
184-
* @param requestParam the request param
185-
* @param pathVar the path var
186217
* @param cookieValue the cookie value
187-
* @return the string
188-
*/
189-
private String calculateName(String pName, RequestHeader requestHeader, RequestParam requestParam, PathVariable pathVar, CookieValue cookieValue) {
190-
String name = pName;
191-
if (requestHeader != null && StringUtils.isNotEmpty(requestHeader.value()))
192-
name = requestHeader.value();
193-
else if (requestParam != null && StringUtils.isNotEmpty(requestParam.value()))
194-
name = requestParam.value();
195-
else if (pathVar != null && StringUtils.isNotEmpty(pathVar.value()))
196-
name = pathVar.value();
197-
else if (cookieValue != null && StringUtils.isNotEmpty(cookieValue.value()))
198-
name = cookieValue.value();
199-
return name;
218+
*/
219+
private void calculateParams(CookieValue cookieValue) {
220+
if (StringUtils.isNotEmpty(cookieValue.value()))
221+
this.pName = cookieValue.value();
222+
this.required = cookieValue.required();
223+
this.defaultValue = cookieValue.defaultValue();
224+
this.paramType = ParameterIn.COOKIE.toString();
225+
}
226+
227+
/**
228+
* Calculate params.
229+
*
230+
* @param pathVar the path var
231+
*/
232+
private void calculateParams(PathVariable pathVar) {
233+
if (StringUtils.isNotEmpty(pathVar.value()))
234+
this.pName = pathVar.value();
235+
this.required = pathVar.required();
236+
this.paramType = ParameterIn.PATH.toString();
237+
}
238+
239+
/**
240+
* Calculate params.
241+
*
242+
* @param requestParam the request param
243+
* @param isFile the is file
244+
*/
245+
private void calculateParams(RequestParam requestParam, boolean isFile) {
246+
if (StringUtils.isNotEmpty(requestParam.value()))
247+
this.pName = requestParam.value();
248+
this.required = requestParam.required();
249+
this.defaultValue = requestParam.defaultValue();
250+
if (!isFile)
251+
this.paramType = ParameterIn.QUERY.toString();
252+
}
253+
254+
/**
255+
* Calculate params.
256+
*
257+
* @param requestHeader the request header
258+
*/
259+
private void calculateParams(RequestHeader requestHeader) {
260+
if (StringUtils.isNotEmpty(requestHeader.value()))
261+
this.pName = requestHeader.value();
262+
this.required = requestHeader.required();
263+
this.defaultValue = requestHeader.defaultValue();
264+
this.paramType = ParameterIn.HEADER.toString();
200265
}
201266
}

0 commit comments

Comments
 (0)