-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Required field in Schema annotation ignored in Kotlin. Fixes #2021.
- Loading branch information
1 parent
cfd6d3a
commit e15c0d6
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
springdoc-openapi-kotlin/src/main/java/org/springdoc/kotlin/SpringDocRequiredModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* | ||
* * | ||
* * * | ||
* * * * 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 org.springdoc.kotlin; | ||
|
||
import com.fasterxml.jackson.databind.introspect.AnnotatedMember; | ||
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
/** | ||
* The type Spring doc required module. | ||
* | ||
* @author bnasslahsen | ||
*/ | ||
public class SpringDocRequiredModule extends SimpleModule { | ||
|
||
@Override | ||
public void setupModule(SetupContext context) { | ||
context.insertAnnotationIntrospector(new RespectSchemaRequiredAnnotationIntrospector()); | ||
} | ||
|
||
/** | ||
* The type Respect schema required annotation introspector. | ||
*/ | ||
private static class RespectSchemaRequiredAnnotationIntrospector extends NopAnnotationIntrospector { | ||
@Override | ||
public Boolean hasRequiredMarker(AnnotatedMember m) { | ||
Schema schemaAnnotation = m.getAnnotation(Schema.class); | ||
return schemaAnnotation != null ? schemaAnnotation.required() : null; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
springdoc-openapi-kotlin/src/test/kotlin/test/org/springdoc/api/app9/DemoController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package test.org.springdoc.api.app9 | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
@RequestMapping("/api/demo") | ||
class DocumentsApiController { | ||
|
||
@GetMapping | ||
suspend fun getDocuments( request: DemoRequest | ||
): DemoDto = DemoDto(42) | ||
} | ||
|
||
data class DemoDto( | ||
var id: Long, | ||
) | ||
|
||
class DemoRequest { | ||
@field:Schema(required = false, description = "Should not be required") | ||
val nonNullableWithDefault: String = "a default value" | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
springdoc-openapi-kotlin/src/test/kotlin/test/org/springdoc/api/app9/SpringDocApp9Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* | ||
* * 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.app9 | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.context.annotation.ComponentScan | ||
import test.org.springdoc.api.AbstractKotlinSpringDocTest | ||
|
||
class SpringDocApp9Test : AbstractKotlinSpringDocTest() { | ||
|
||
@SpringBootApplication | ||
@ComponentScan(basePackages = ["org.springdoc", "test.org.springdoc.api.app9"]) | ||
open class DemoApplication | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
springdoc-openapi-kotlin/src/test/resources/results/app9.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{ | ||
"openapi": "3.0.1", | ||
"info": { | ||
"title": "OpenAPI definition", | ||
"version": "v0" | ||
}, | ||
"servers": [ | ||
{ | ||
"url": "", | ||
"description": "Generated server url" | ||
} | ||
], | ||
"paths": { | ||
"/api/demo": { | ||
"get": { | ||
"tags": [ | ||
"documents-api-controller" | ||
], | ||
"operationId": "getDocuments", | ||
"parameters": [ | ||
{ | ||
"name": "request", | ||
"in": "query", | ||
"required": true, | ||
"schema": { | ||
"$ref": "#/components/schemas/DemoRequest" | ||
} | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"content": { | ||
"*/*": { | ||
"schema": { | ||
"$ref": "#/components/schemas/DemoDto" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"DemoRequest": { | ||
"required": [ | ||
"nonNullableWithDefault" | ||
], | ||
"type": "object", | ||
"properties": { | ||
"nonNullableWithDefault": { | ||
"type": "string", | ||
"description": "Should not be required" | ||
} | ||
} | ||
}, | ||
"DemoDto": { | ||
"required": [ | ||
"id" | ||
], | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "integer", | ||
"format": "int64" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |