Skip to content

Commit

Permalink
Request parameter parsing error after using @notblank in the from typ…
Browse files Browse the repository at this point in the history
…e interface field. Fixes #2519
  • Loading branch information
bnasslahsen committed Mar 3, 2024
1 parent 9aa78d0 commit 65e9a3c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.customizers.ParameterCustomizer;
import org.springdoc.core.discoverer.SpringDocParameterNameDiscoverer;
Expand All @@ -77,7 +76,6 @@
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpMethod;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
Expand All @@ -93,11 +91,13 @@
import org.springframework.web.util.UriComponentsBuilder;

import static org.springdoc.core.converters.SchemaPropertyDeprecatingConverter.containsDeprecatedAnnotation;
import static org.springdoc.core.service.GenericParameterService.isFile;
import static org.springdoc.core.utils.Constants.OPENAPI_ARRAY_TYPE;
import static org.springdoc.core.utils.Constants.OPENAPI_STRING_TYPE;

/**
* The type Abstract request builder.
*
* @author bnasslahsen
*/
public abstract class AbstractRequestService {
Expand Down Expand Up @@ -183,10 +183,10 @@ public abstract class AbstractRequestService {
/**
* Instantiates a new Abstract request builder.
*
* @param parameterBuilder the parameter builder
* @param requestBodyService the request body builder
* @param operationService the operation builder
* @param parameterCustomizers the parameter customizers
* @param parameterBuilder the parameter builder
* @param requestBodyService the request body builder
* @param operationService the operation builder
* @param parameterCustomizers the parameter customizers
* @param localSpringDocParameterNameDiscoverer the local spring doc parameter name discoverer
*/
protected AbstractRequestService(GenericParameterService parameterBuilder, RequestBodyService requestBodyService,
Expand Down Expand Up @@ -237,7 +237,7 @@ public static boolean isRequestTypeToIgnore(Class<?> rawClass) {
* Gets headers.
*
* @param methodAttributes the method attributes
* @param map the map
* @param map the map
* @return the headers
*/
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -265,11 +265,11 @@ public static Collection<Parameter> getHeaders(MethodAttributes methodAttributes
/**
* Build operation.
*
* @param handlerMethod the handler method
* @param requestMethod the request method
* @param operation the operation
* @param handlerMethod the handler method
* @param requestMethod the request method
* @param operation the operation
* @param methodAttributes the method attributes
* @param openAPI the open api
* @param openAPI the open api
* @return the operation
*/
public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
Expand Down Expand Up @@ -378,10 +378,10 @@ else if (!RequestMethod.GET.equals(requestMethod) || OpenApiVersion.OPENAPI_3_1.
/**
* Gets parameter linked hash map.
*
* @param components the components
* @param methodAttributes the method attributes
* @param components the components
* @param methodAttributes the method attributes
* @param operationParameters the operation parameters
* @param parametersDocMap the parameters doc map
* @param parametersDocMap the parameters doc map
* @return the parameter linked hash map
*/
private LinkedHashMap<ParameterId, Parameter> getParameterLinkedHashMap(Components components, MethodAttributes methodAttributes, List<Parameter> operationParameters, Map<ParameterId, io.swagger.v3.oas.annotations.Parameter> parametersDocMap) {
Expand Down Expand Up @@ -425,8 +425,8 @@ private LinkedHashMap<ParameterId, Parameter> getParameterLinkedHashMap(Componen
/**
* Customise parameter parameter.
*
* @param parameter the parameter
* @param parameterInfo the parameter info
* @param parameter the parameter
* @param parameterInfo the parameter info
* @param operationParameters the operation parameters
*/
protected void customiseParameter(Parameter parameter, ParameterInfo parameterInfo, List<Parameter> operationParameters) {
Expand Down Expand Up @@ -472,9 +472,9 @@ private boolean isRequiredAnnotation(MethodParameter parameter) {
/**
* Sets params.
*
* @param operation the operation
* @param operation the operation
* @param operationParameters the operation parameters
* @param requestBodyInfo the request body info
* @param requestBodyInfo the request body info
*/
private void setParams(Operation operation, List<Parameter> operationParameters, RequestBodyInfo requestBodyInfo) {
if (!CollectionUtils.isEmpty(operationParameters))
Expand All @@ -496,10 +496,10 @@ public boolean isValidParameter(Parameter parameter) {
/**
* Build params parameter.
*
* @param parameterInfo the parameter info
* @param components the components
* @param requestMethod the request method
* @param jsonView the json view
* @param parameterInfo the parameter info
* @param components the components
* @param requestMethod the request method
* @param jsonView the json view
* @param openApiVersion the open api version
* @return the parameter
*/
Expand All @@ -526,8 +526,8 @@ public Parameter buildParams(ParameterInfo parameterInfo, Components components,
* Build param parameter.
*
* @param parameterInfo the parameter info
* @param components the components
* @param jsonView the json view
* @param components the components
* @param jsonView the json view
* @return the parameter
*/
public Parameter buildParam(ParameterInfo parameterInfo, Components components, JsonView jsonView) {
Expand Down Expand Up @@ -573,7 +573,7 @@ public Parameter buildParam(ParameterInfo parameterInfo, Components components,
/**
* Apply bean validator annotations.
*
* @param parameter the parameter
* @param parameter the parameter
* @param annotations the annotations
*/
public void applyBeanValidatorAnnotations(final Parameter parameter, final List<Annotation> annotations) {
Expand All @@ -592,7 +592,7 @@ public void applyBeanValidatorAnnotations(final Parameter parameter, final List<
*
* @param requestBody the request body
* @param annotations the annotations
* @param isOptional the is optional
* @param isOptional the is optional
*/
public void applyBeanValidatorAnnotations(final RequestBody requestBody, final List<Annotation> annotations, boolean isOptional) {
Map<String, Annotation> annos = new HashMap<>();
Expand All @@ -614,10 +614,28 @@ public void applyBeanValidatorAnnotations(final RequestBody requestBody, final L
}
}

/**
* Gets request body builder.
*
* @return the request body builder
*/
public RequestBodyService getRequestBodyBuilder() {
return requestBodyService;
}

/**
* Is default flat param object boolean.
*
* @return the boolean
*/
public boolean isDefaultFlatParamObject() {
return defaultFlatParamObject;
}

/**
* Calculate size.
*
* @param annos the annos
* @param annos the annos
* @param schema the schema
*/
private void calculateSize(Map<String, Annotation> annos, Schema<?> schema) {
Expand All @@ -634,15 +652,6 @@ else if (OPENAPI_STRING_TYPE.equals(schema.getType())) {
}
}

/**
* Gets request body builder.
*
* @return the request body builder
*/
public RequestBodyService getRequestBodyBuilder() {
return requestBodyService;
}

/**
* Gets api parameters.
*
Expand Down Expand Up @@ -673,7 +682,7 @@ private Map<ParameterId, io.swagger.v3.oas.annotations.Parameter> getApiParamete
/**
* Apply validations to schema.
*
* @param annos the annos
* @param annos the annos
* @param schema the schema
*/
private void applyValidationsToSchema(Map<String, Annotation> annos, Schema<?> schema) {
Expand Down Expand Up @@ -713,31 +722,65 @@ private void applyValidationsToSchema(Map<String, Annotation> annos, Schema<?> s
/**
* Is RequestBody param boolean.
*
* @param requestMethod the request method
* @param parameterInfo the parameter info
* @param requestMethod the request method
* @param parameterInfo the parameter info
* @param openApiVersion the open api version
* @return the boolean
*/
private boolean isRequestBodyParam(RequestMethod requestMethod, ParameterInfo parameterInfo, String openApiVersion) {
MethodParameter methodParameter = parameterInfo.getMethodParameter();
DelegatingMethodParameter delegatingMethodParameter = (DelegatingMethodParameter) methodParameter;
Boolean isBodyAllowed = !RequestMethod.GET.equals(requestMethod) || OpenApiVersion.OPENAPI_3_1.getVersion().equals(openApiVersion);
boolean isBodyAllowed = !RequestMethod.GET.equals(requestMethod) || OpenApiVersion.OPENAPI_3_1.getVersion().equals(openApiVersion);

return (isBodyAllowed && (parameterInfo.getParameterModel() == null || parameterInfo.getParameterModel().getIn() == null) && !delegatingMethodParameter.isParameterObject())
&&
((methodParameter.getParameterAnnotation(io.swagger.v3.oas.annotations.parameters.RequestBody.class) != null
|| methodParameter.getParameterAnnotation(org.springframework.web.bind.annotation.RequestBody.class) != null
|| methodParameter.getParameterAnnotation(org.springframework.web.bind.annotation.RequestPart.class) != null
|| AnnotatedElementUtils.findMergedAnnotation(Objects.requireNonNull(methodParameter.getMethod()), io.swagger.v3.oas.annotations.parameters.RequestBody.class) != null)
|| (!ClassUtils.isPrimitiveOrWrapper(methodParameter.getParameterType()) && (!ArrayUtils.isEmpty(methodParameter.getParameterAnnotations()))));
|| checkOperationRequestBody(methodParameter)
|| checkFile(methodParameter)

);
}

/**
* Is default flat param object boolean.
* Check file boolean.
*
* @param methodParameter the method parameter
* @return the boolean
*/
public boolean isDefaultFlatParamObject() {
return defaultFlatParamObject;
private boolean checkFile(MethodParameter methodParameter) {
if (methodParameter.getParameterAnnotation(org.springframework.web.bind.annotation.RequestPart.class) != null)
return true;
else if (methodParameter.getParameterAnnotation(org.springframework.web.bind.annotation.RequestParam.class) != null) {
return isFile(methodParameter.getParameterType());
}
return false;
}

/**
* Check operation request body boolean.
*
* @param methodParameter the method parameter
* @return the boolean
*/
private boolean checkOperationRequestBody(MethodParameter methodParameter) {
if (AnnotatedElementUtils.findMergedAnnotation(Objects.requireNonNull(methodParameter.getMethod()), io.swagger.v3.oas.annotations.Operation.class) != null) {
io.swagger.v3.oas.annotations.Operation operation = AnnotatedElementUtils.findMergedAnnotation(Objects.requireNonNull(methodParameter.getMethod()), io.swagger.v3.oas.annotations.Operation.class);
io.swagger.v3.oas.annotations.parameters.RequestBody requestBody = operation.requestBody();
if (StringUtils.isNotBlank(requestBody.description()))
return true;
else if (StringUtils.isNotBlank(requestBody.ref()))
return true;
else if (requestBody.required())
return true;
else if (requestBody.useParameterTypeSchema())
return true;
else if (requestBody.content().length > 0)
return true;
else
return requestBody.extensions().length > 0;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
"hello-controller"
],
"operationId": "test3",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
"parameters": [
{
"name": "test",
"in": "query",
"required": true,
"schema": {
"type": "string"
}
},
"required": true
},
}
],
"responses": {
"200": {
"description": "OK",
Expand All @@ -41,19 +41,17 @@
}
}
},
"/api1": {
"/api2": {
"post": {
"tags": [
"hello-controller"
],
"operationId": "test1",
"operationId": "test2",
"requestBody": {
"content": {
"application/json": {
"schema": {
"minimum": 2,
"type": "integer",
"format": "int32"
"type": "string"
}
}
},
Expand All @@ -73,17 +71,19 @@
}
}
},
"/api2": {
"/api1": {
"post": {
"tags": [
"hello-controller"
],
"operationId": "test2",
"operationId": "test1",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "string"
"minimum": 2,
"type": "integer",
"format": "int32"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,17 @@
"summary": "Test 3 string.",
"description": "Test 3 string.",
"operationId": "test3",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
"parameters": [
{
"name": "test",
"in": "query",
"description": "the test",
"required": true,
"schema": {
"type": "string"
}
},
"required": true
},
}
],
"responses": {
"200": {
"description": "the string",
Expand Down

0 comments on commit 65e9a3c

Please sign in to comment.