Skip to content

Commit

Permalink
Required field in Schema annotation ignored in Kotlin. Fixes #2021.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Feb 4, 2023
1 parent cfd6d3a commit e15c0d6
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class SpringDocKotlinConfiguration(objectMapperProvider: ObjectMapperProvider) {
.addRequestWrapperToIgnore(Continuation::class.java)
.replaceWithSchema(ByteArray::class.java, ByteArraySchema())
.addDeprecatedType(Deprecated::class.java)
objectMapperProvider.jsonMapper().registerModule(SpringDocRequiredModule())
objectMapperProvider.jsonMapper().registerModule(KotlinModule.Builder().build())
}

Expand Down
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;
}
}
}
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"

}
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 springdoc-openapi-kotlin/src/test/resources/results/app9.json
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"
}
}
}
}
}
}

0 comments on commit e15c0d6

Please sign in to comment.