Skip to content

Added support for RequestBody as a meta-annotation #2760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -618,16 +618,20 @@ public void applyBeanValidatorAnnotations(final Parameter parameter, final List<
*/
public void applyBeanValidatorAnnotations(final RequestBody requestBody, final List<Annotation> annotations, boolean isOptional) {
Map<String, Annotation> annos = new HashMap<>();
boolean requestBodyRequired = false;
boolean springRequestBodyRequired = false;
boolean swaggerRequestBodyRequired = false;
if (!CollectionUtils.isEmpty(annotations)) {
annotations.forEach(annotation -> annos.put(annotation.annotationType().getSimpleName(), annotation));
requestBodyRequired = annotations.stream()
springRequestBodyRequired = annotations.stream()
.filter(annotation -> org.springframework.web.bind.annotation.RequestBody.class.equals(annotation.annotationType()))
.anyMatch(annotation -> ((org.springframework.web.bind.annotation.RequestBody) annotation).required());
swaggerRequestBodyRequired = annotations.stream()
.filter(annotation -> io.swagger.v3.oas.annotations.parameters.RequestBody.class.equals(annotation.annotationType()))
.anyMatch(annotation -> ((io.swagger.v3.oas.annotations.parameters.RequestBody) annotation).required());
}
boolean validationExists = Arrays.stream(ANNOTATIONS_FOR_REQUIRED).anyMatch(annos::containsKey);

if (validationExists || (!isOptional && requestBodyRequired))
if (validationExists || (!isOptional && (springRequestBodyRequired || swaggerRequestBodyRequired)))
requestBody.setRequired(true);
Content content = requestBody.getContent();
for (MediaType mediaType : content.values()) {
Expand Down Expand Up @@ -766,14 +770,25 @@ private boolean isRequestBodyParam(RequestMethod requestMethod, ParameterInfo pa

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
|| AnnotatedElementUtils.findMergedAnnotation(Objects.requireNonNull(methodParameter.getMethod()), io.swagger.v3.oas.annotations.parameters.RequestBody.class) != null)
(checkRequestBodyAnnotation(methodParameter)
|| checkOperationRequestBody(methodParameter)
|| checkFile(methodParameter)
|| Arrays.asList(methodAttributes.getMethodConsumes()).contains(MULTIPART_FORM_DATA_VALUE));
}

/**
* Checks whether Swagger's or Spring's RequestBody annotation is present on a parameter or method
*
* @param methodParameter the method parameter
* @return the boolean
*/
private boolean checkRequestBodyAnnotation(MethodParameter methodParameter) {
return methodParameter.hasParameterAnnotation(org.springframework.web.bind.annotation.RequestBody.class)
|| methodParameter.hasParameterAnnotation(io.swagger.v3.oas.annotations.parameters.RequestBody.class)
|| AnnotatedElementUtils.isAnnotated(Objects.requireNonNull(methodParameter.getParameter()), io.swagger.v3.oas.annotations.parameters.RequestBody.class)
|| AnnotatedElementUtils.isAnnotated(Objects.requireNonNull(methodParameter.getMethod()), io.swagger.v3.oas.annotations.parameters.RequestBody.class);
}

/**
* Check file boolean.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test.org.springdoc.api.v30.app230;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author edudar
*/
@RestController
@RequestMapping
public class App230Controller {

@PostMapping("/")
public String swaggerTest(@App230RequestBody MyRequest myRequest) {
return null;
}

public record MyRequest(int id) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package test.org.springdoc.api.v30.app230;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import io.swagger.v3.oas.annotations.parameters.RequestBody;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;

/**
* @author edudar
*/
@Target({PARAMETER, METHOD, ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@RequestBody(required = true)
@interface App230RequestBody {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
*
* *
* * *
* * * *
* * * * *
* * * * * * Copyright 2019-2024 the original author or authors.
* * * * * *
* * * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * * you may not use this file except in compliance with the License.
* * * * * * You may obtain a copy of the License at
* * * * * *
* * * * * * https://www.apache.org/licenses/LICENSE-2.0
* * * * * *
* * * * * * Unless required by applicable law or agreed to in writing, software
* * * * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * * * See the License for the specific language governing permissions and
* * * * * * limitations under the License.
* * * * *
* * * *
* * *
* *
*
*/

package test.org.springdoc.api.v30.app230;

import test.org.springdoc.api.v30.AbstractSpringDocV30Test;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author edudar
*/
public class SpringDocApp230Test extends AbstractSpringDocV30Test {

@SpringBootApplication
static class SpringDocTestApp {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/": {
"post": {
"tags": [
"app-230-controller"
],
"operationId": "swaggerTest",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MyRequest"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"MyRequest": {
"properties": {
"id": {
"format": "int32",
"type": "integer"
}
},
"type": "object"
}
}
}
}