Skip to content

Commit

Permalink
Kotlin enums are always marked as required if used in Java controllers.
Browse files Browse the repository at this point in the history
Fixes #2622
  • Loading branch information
bnasslahsen committed Jun 23, 2024
1 parent cf12547 commit 524c5fa
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 "";
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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": {}
}

0 comments on commit 524c5fa

Please sign in to comment.