From 945f0e9e8cbfe98accf6bb98cc7274552fa47441 Mon Sep 17 00:00:00 2001 From: Ruud Welling Date: Fri, 24 Jan 2020 14:46:03 +0100 Subject: [PATCH] Recognize that a @RequestParam is not required if the type of the parameter is wrapped in Optional --- .../core/AbstractRequestBuilder.java | 3 +- .../springdoc/api/app78/HelloController.java | 36 +++++++++++++++ .../api/app78/SpringDocApp78Test.java | 28 +++++++++++ .../src/test/resources/results/app78.json | 46 +++++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app78/HelloController.java create mode 100644 springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app78/SpringDocApp78Test.java create mode 100644 springdoc-openapi-webmvc-core/src/test/resources/results/app78.json diff --git a/springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractRequestBuilder.java b/springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractRequestBuilder.java index 793616813..d249f1a39 100644 --- a/springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractRequestBuilder.java +++ b/springdoc-openapi-common/src/main/java/org/springdoc/core/AbstractRequestBuilder.java @@ -263,7 +263,8 @@ private Parameter buildParams(ParameterInfo parameterInfo, Components components } else if (requestParam != null) { - requestInfo = new RequestInfo(ParameterType.QUERY_PARAM, requestParam.value(), requestParam.required(), + boolean isOptional = Optional.class.equals(parameters.getType()); + requestInfo = new RequestInfo(ParameterType.QUERY_PARAM, requestParam.value(), requestParam.required() && !isOptional, requestParam.defaultValue()); parameter = buildParam(parameterInfo, components, requestInfo, jsonView); } diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app78/HelloController.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app78/HelloController.java new file mode 100644 index 000000000..d687fb103 --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app78/HelloController.java @@ -0,0 +1,36 @@ +/* + * + * * Copyright 2019-2020 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.app78; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Optional; + +@RestController +public class HelloController { + + @GetMapping(produces = MediaType.TEXT_PLAIN_VALUE, path = "/test") + public String echo(@RequestParam Optional text) { + return text.orElse("not-specified"); + } + +} diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app78/SpringDocApp78Test.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app78/SpringDocApp78Test.java new file mode 100644 index 000000000..20e24b46f --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app78/SpringDocApp78Test.java @@ -0,0 +1,28 @@ +/* + * + * * Copyright 2019-2020 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.app78; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import test.org.springdoc.api.AbstractSpringDocTest; + +public class SpringDocApp78Test extends AbstractSpringDocTest { + + @SpringBootApplication + static class SpringDocTestApp {} +} diff --git a/springdoc-openapi-webmvc-core/src/test/resources/results/app78.json b/springdoc-openapi-webmvc-core/src/test/resources/results/app78.json new file mode 100644 index 000000000..8c239edcd --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/resources/results/app78.json @@ -0,0 +1,46 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "OpenAPI definition", + "version": "v0" + }, + "servers": [ + { + "url": "http://localhost", + "description": "Generated server url" + } + ], + "paths": { + "/test": { + "get": { + "tags": [ + "hello-controller" + ], + "operationId": "echo", + "parameters": [ + { + "name": "text", + "in": "query", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "default response", + "content": { + "text/plain": { + "schema": { + "type": "string" + } + } + } + } + } + } + } + }, + "components": {} +}