Skip to content

Commit

Permalink
Merge pull request #1814 from okohub/feature-get-with-body
Browse files Browse the repository at this point in the history
Allow requestBody creation for GET on openapi resource endpoint
  • Loading branch information
bnasslahsen committed Aug 30, 2022
2 parents 8fa89e0 + 3d3e327 commit 0b51a68
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 7 deletions.
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 @@ -494,10 +496,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 @@ -507,7 +510,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 @@ -721,13 +724,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
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
@@ -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;
}
}
@@ -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;

}

}
@@ -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 {

}
}
@@ -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"
}
}
}
}
}
}

0 comments on commit 0b51a68

Please sign in to comment.