From 524c5fa24593150854ea7ee0fee17cef08caafe1 Mon Sep 17 00:00:00 2001 From: "Badr.NassLahsen" Date: Sun, 23 Jun 2024 10:11:08 +0200 Subject: [PATCH] Kotlin enums are always marked as required if used in Java controllers. Fixes #2622 --- .../SpringDocKotlinConfiguration.kt | 8 ++- .../springdoc/api/app12/EnumController.java | 18 +++++++ .../springdoc/api/app12/SpringDocApp12Test.kt | 35 +++++++++++++ .../src/test/resources/results/app12.json | 50 +++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app12/EnumController.java create mode 100644 springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app12/SpringDocApp12Test.kt create mode 100644 springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/resources/results/app12.json diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/configuration/SpringDocKotlinConfiguration.kt b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/configuration/SpringDocKotlinConfiguration.kt index 7c7dd6070..fc09e6465 100644 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/configuration/SpringDocKotlinConfiguration.kt +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/configuration/SpringDocKotlinConfiguration.kt @@ -92,9 +92,13 @@ class SpringDocKotlinConfiguration() { // parameter is not required if a default value is provided in @RequestParam else if (requestParam != null && requestParam.defaultValue != ValueConstants.DEFAULT_NONE) parameterModel.required = false - else + else{ + val isJavaNullableAnnotationPresent = methodParameter.parameterAnnotations.any { + it.annotationClass.qualifiedName == "jakarta.annotation.Nullable" + } parameterModel.required = - kParameter.type.isMarkedNullable == false + kParameter.type.isMarkedNullable == false && !isJavaNullableAnnotationPresent + } } } return@ParameterCustomizer parameterModel diff --git a/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app12/EnumController.java b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app12/EnumController.java new file mode 100644 index 000000000..7bdd2ffe2 --- /dev/null +++ b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app12/EnumController.java @@ -0,0 +1,18 @@ +package test.org.springdoc.api.app12; + +import jakarta.annotation.Nullable; +import test.org.springdoc.api.app12.SpringDocApp12Test.MyEnum; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author bnasslahsen + */ +@RestController +public class EnumController { + @GetMapping("/test-enum-2") + String testEnum2(@Nullable MyEnum e) { + return ""; + } +} \ No newline at end of file diff --git a/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app12/SpringDocApp12Test.kt b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app12/SpringDocApp12Test.kt new file mode 100644 index 000000000..f522f7de3 --- /dev/null +++ b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app12/SpringDocApp12Test.kt @@ -0,0 +1,35 @@ +/* + * + * * Copyright 2019-2023 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.app12 + +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.context.annotation.ComponentScan +import test.org.springdoc.api.AbstractKotlinSpringDocMVCTest + +class SpringDocApp12Test : AbstractKotlinSpringDocMVCTest() { + + @SpringBootApplication + @ComponentScan(basePackages = ["org.springdoc", "test.org.springdoc.api.app12"]) + class DemoApplication + + enum class MyEnum { + A, B; + } + +} diff --git a/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/resources/results/app12.json b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/resources/results/app12.json new file mode 100644 index 000000000..407e5620a --- /dev/null +++ b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/resources/results/app12.json @@ -0,0 +1,50 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "OpenAPI definition", + "version": "v0" + }, + "servers": [ + { + "url": "http://localhost", + "description": "Generated server url" + } + ], + "paths": { + "/test-enum-2": { + "get": { + "tags": [ + "enum-controller" + ], + "operationId": "testEnum2", + "parameters": [ + { + "name": "e", + "in": "query", + "required": false, + "schema": { + "type": "string", + "enum": [ + "A", + "B" + ] + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "*/*": { + "schema": { + "type": "string" + } + } + } + } + } + } + } + }, + "components": {} +}