Skip to content

Commit

Permalink
All request headers marked as required for Java controllers in mixed …
Browse files Browse the repository at this point in the history
…projects in 2.0.3. Fixes #2187
  • Loading branch information
bnasslahsen committed Apr 1, 2023
1 parent c3de760 commit 39fd721
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Lazy
import org.springframework.core.KotlinDetector
import org.springframework.core.MethodParameter
import org.springframework.core.annotation.AnnotatedElementUtils
import org.springframework.web.bind.annotation.RequestParam
Expand Down Expand Up @@ -71,27 +72,30 @@ class SpringDocKotlinConfiguration(objectMapperProvider: ObjectMapperProvider) {
matchIfMissing = true
)
@ConditionalOnMissingBean
fun nullableKotlinRequestParameterCustomizer(): ParameterCustomizer {
open fun nullableKotlinRequestParameterCustomizer(): ParameterCustomizer {
return ParameterCustomizer { parameterModel, methodParameter ->
if (parameterModel == null) return@ParameterCustomizer null
val kParameter = methodParameter.toKParameter()
if (kParameter != null) {
val parameterDoc = AnnotatedElementUtils.findMergedAnnotation(
AnnotatedElementUtils.forAnnotations(*methodParameter.parameterAnnotations),
Parameter::class.java
)
val requestParam = AnnotatedElementUtils.findMergedAnnotation(
if (KotlinDetector.isKotlinType(methodParameter.parameterType)) {
val kParameter = methodParameter.toKParameter()
if (kParameter != null) {
val parameterDoc = AnnotatedElementUtils.findMergedAnnotation(
AnnotatedElementUtils.forAnnotations(*methodParameter.parameterAnnotations),
Parameter::class.java
)
val requestParam = AnnotatedElementUtils.findMergedAnnotation(
AnnotatedElementUtils.forAnnotations(*methodParameter.parameterAnnotations),
RequestParam::class.java
)
// Swagger @Parameter annotation takes precedence
if (parameterDoc != null && parameterDoc.required)
parameterModel.required = parameterDoc.required
// parameter is not required if a default value is provided in @RequestParam
else if (requestParam != null && ((requestParam.defaultValue != ValueConstants.DEFAULT_NONE) || !requestParam.required))
parameterModel.required = false
else
parameterModel.required = kParameter.type.isMarkedNullable == false
)
// Swagger @Parameter annotation takes precedence
if (parameterDoc != null && parameterDoc.required)
parameterModel.required = parameterDoc.required
// 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
parameterModel.required =
kParameter.type.isMarkedNullable == false
}
}
return@ParameterCustomizer parameterModel
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -17,4 +18,8 @@ public class HelloController {
@GetMapping("/")
public void greet(@RequestParam(required = false) @Parameter(required = false) final String name) {
}

@GetMapping("/test2")
public void greet1(@RequestHeader(required = false) @Parameter(required = false) final String name) {
}
}
23 changes: 23 additions & 0 deletions springdoc-openapi-kotlin/src/test/resources/results/app21.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@
}
],
"paths": {
"/api/v2/test/test2": {
"get": {
"tags": [
"Test"
],
"operationId": "greet1",
"parameters": [
{
"name": "name",
"in": "header",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/v2/test/": {
"get": {
"tags": [
Expand Down

0 comments on commit 39fd721

Please sign in to comment.