Skip to content

Allow requestBody creation for GET on openapi resource endpoint #1814

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 1 commit into from
Aug 30, 2022
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 @@ -64,6 +64,7 @@
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springdoc.core.SpringDocConfigProperties.ApiDocs.OpenApiVersion;
import org.springdoc.core.customizers.ParameterCustomizer;
import org.springdoc.core.providers.JavadocProvider;

Expand Down Expand Up @@ -291,7 +292,8 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
}

if (!isParamToIgnore(methodParameter)) {
parameter = buildParams(parameterInfo, components, requestMethod, methodAttributes.getJsonViewAnnotation());
parameter = buildParams(parameterInfo, components, requestMethod, methodAttributes.getJsonViewAnnotation(),
openAPI.getOpenapi());
// Merge with the operation parameters
parameter = GenericParameterService.mergeParameter(operationParameters, parameter);
List<Annotation> parameterAnnotations = Arrays.asList(methodParameter.getParameterAnnotations());
Expand All @@ -305,7 +307,7 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
}
applyBeanValidatorAnnotations(parameter, parameterAnnotations);
}
else if (!RequestMethod.GET.equals(requestMethod)) {
else if (!RequestMethod.GET.equals(requestMethod) || OpenApiVersion.OPENAPI_3_1.getVersion().equals(openAPI.getOpenapi())) {
if (operation.getRequestBody() != null)
requestBodyInfo.setRequestBody(operation.getRequestBody());
requestBodyService.calculateRequestBodyInfo(components, methodAttributes,
Expand Down Expand Up @@ -488,10 +490,11 @@ public boolean isValidParameter(Parameter parameter) {
* @param components the components
* @param requestMethod the request method
* @param jsonView the json view
* @param openApiVersion the open api version
* @return the parameter
*/
public Parameter buildParams(ParameterInfo parameterInfo, Components components,
RequestMethod requestMethod, JsonView jsonView) {
RequestMethod requestMethod, JsonView jsonView, String openApiVersion) {
MethodParameter methodParameter = parameterInfo.getMethodParameter();
if (parameterInfo.getParamType() != null) {
if (!ValueConstants.DEFAULT_NONE.equals(parameterInfo.getDefaultValue()))
Expand All @@ -501,7 +504,7 @@ public Parameter buildParams(ParameterInfo parameterInfo, Components components,
return this.buildParam(parameterInfo, components, jsonView);
}
// By default
if (!isRequestBodyParam(requestMethod, parameterInfo)) {
if (!isRequestBodyParam(requestMethod, parameterInfo, openApiVersion)) {
parameterInfo.setRequired(!((DelegatingMethodParameter) methodParameter).isNotRequired() && !methodParameter.isOptional());
parameterInfo.setParamType(QUERY_PARAM);
parameterInfo.setDefaultValue(null);
Expand Down Expand Up @@ -715,13 +718,15 @@ private void applyValidationsToSchema(Map<String, Annotation> annos, Schema<?> s
*
* @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) {
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);

return (!RequestMethod.GET.equals(requestMethod) && (parameterInfo.getParameterModel() == null || parameterInfo.getParameterModel().getIn() == null) && !delegatingMethodParameter.isParameterObject())
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ else if (methodParameter.getParameterAnnotation(BackendId.class) != null) {
parameterInfo.setParameterModel(parameter);
}
if (!ArrayUtils.isEmpty(methodParameter.getParameterAnnotations()))
parameter = requestBuilder.buildParams(parameterInfo, openAPI.getComponents(), requestMethod, null);
parameter = requestBuilder.buildParams(parameterInfo, openAPI.getComponents(), requestMethod, null,
openAPI.getOpenapi());

addParameters(openAPI, requestMethod, methodAttributes, operation, methodParameter, parameterInfo, parameter);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
*
* *
* * *
* * * * Copyright 2019-2022 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.v31.app6;

import java.time.Instant;

public class DummyData {

String trackerId;

Instant timestamp;

Double value;

public String getTrackerId() {
return trackerId;
}

public void setTrackerId(String trackerId) {
this.trackerId = trackerId;
}

public Instant getTimestamp() {
return timestamp;
}

public void setTimestamp(Instant timestamp) {
this.timestamp = timestamp;
}

public Double getValue() {
return value;
}

public void setValue(Double value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* *
* * *
* * * * Copyright 2019-2022 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.v31.app6;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import test.org.springdoc.api.v31.app4.TrackerData;

@RestController
public class GetBodyController {

@GetMapping(value = "/get/body")
DummyData getBody(@RequestBody DummyData dummyData) {
return dummyData;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* *
* * *
* * * * Copyright 2019-2022 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.v31.app6;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import test.org.springdoc.api.v31.AbstractSpringDocV31Test;

public class SpringDocApp6Test extends AbstractSpringDocV31Test {

@SpringBootApplication
static class SpringDocTestApp {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"openapi": "3.1.0",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/get/body": {
"get": {
"tags": [
"get-body-controller"
],
"operationId": "getBody",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/DummyData"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/DummyData"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"DummyData": {
"properties": {
"trackerId": {
"type": "string"
},
"timestamp": {
"type": "string",
"format": "date-time"
},
"value": {
"type": "number",
"format": "double"
}
}
}
}
}
}