Skip to content

Commit 41d5f80

Browse files
committed
Empty route descriptions moved to module to allow for body-less response descriptors.
1 parent f59ab2b commit 41d5f80

File tree

6 files changed

+17
-8
lines changed

6 files changed

+17
-8
lines changed

src/main/kotlin/com/papsign/ktor/openapigen/OpenAPIGen.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import com.papsign.ktor.openapigen.openapi.Server
1111
import io.ktor.application.*
1212
import io.ktor.request.path
1313
import io.ktor.util.AttributeKey
14+
import kotlinx.coroutines.GlobalScope
15+
import kotlinx.coroutines.launch
1416
import org.reflections.Reflections
17+
import javax.xml.bind.JAXBElement
1518
import kotlin.reflect.KType
1619

1720
class OpenAPIGen(
@@ -26,7 +29,7 @@ class OpenAPIGen(
2629
val schemaNamer = object : SchemaNamer {
2730
private val fn = config.schemaNamer
2831

29-
override fun get(type: KType): String = fn(type)
32+
override fun get(type: KType): String = fn(type)
3033
}
3134

3235
private val registrars: Array<out PartialSchemaRegistrar> = config.registrars.plus(PrimitiveSchemas(schemaNamer))
@@ -35,7 +38,7 @@ class OpenAPIGen(
3538
val globalModuleProvider = CachingModuleProvider()
3639

3740
init {
38-
val reflections = Reflections(this::class.java.`package`.name)
41+
val reflections = Reflections(javaClass.`package`.name)
3942
val classes = reflections.getTypesAnnotatedWith(APIFormatter::class.java).mapNotNull { it.kotlin.objectInstance }
4043
classes.forEach {
4144
when (it) {
@@ -66,13 +69,13 @@ class OpenAPIGen(
6669
var swaggerUiPath = "swagger-ui"
6770
var serveSwaggerUi = true
6871

69-
var schemaNamer: (KType)->String = KType::toString
72+
var schemaNamer: (KType) -> String = KType::toString
7073

7174
var registrars: Array<PartialSchemaRegistrar> = arrayOf()
7275
}
7376

7477

75-
inner class Schemas: SimpleSchemaRegistrar(schemaNamer) {
78+
inner class Schemas : SimpleSchemaRegistrar(schemaNamer) {
7679

7780
private val schemas = HashSchemaMap()
7881
private val names = HashMap<KType, String>()
@@ -90,7 +93,7 @@ class OpenAPIGen(
9093
api.components.schemas[name] = schema
9194
schema
9295
}
93-
val name= names[type]!!
96+
val name = names[type]!!
9497
return NamedSchema(name, SchemaRef<Any>("#/components/schemas/$name"))
9598
}
9699
}

src/main/kotlin/com/papsign/ktor/openapigen/content/type/binary/BinaryContentTypeParser.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.papsign.ktor.openapigen.content.type.binary
22

33
import com.papsign.kotlin.reflection.getKType
44
import com.papsign.kotlin.reflection.getObjectSubtypes
5+
import com.papsign.kotlin.reflection.unitKType
56
import com.papsign.ktor.openapigen.OpenAPIGen
67
import com.papsign.ktor.openapigen.annotations.Response
78
import com.papsign.ktor.openapigen.annotations.encodings.APIFormatter
@@ -58,6 +59,7 @@ object BinaryContentTypeParser: BodyParser, ResponseSerializer {
5859
}
5960

6061
override fun <T> getMediaType(type: KType, apiGen: OpenAPIGen, provider: ModuleProvider<*>, example: T?, usage: ContentTypeProvider.Usage): Map<ContentType, MediaType<T>>? {
62+
if (type == unitKType) return null
6163
val contentTypes = when(usage) {
6264
ContentTypeProvider.Usage.PARSE -> {
6365
val binaryRequest = type.jvmErasure.findAnnotation<BinaryRequest>() ?: return null

src/main/kotlin/com/papsign/ktor/openapigen/content/type/ktor/KtorContentProvider.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.papsign.ktor.openapigen.content.type.ktor
22

33
import com.papsign.kotlin.reflection.allTypes
44
import com.papsign.kotlin.reflection.getKType
5+
import com.papsign.kotlin.reflection.unitKType
56
import com.papsign.ktor.openapigen.OpenAPIGen
67
import com.papsign.ktor.openapigen.annotations.encodings.APIFormatter
78
import com.papsign.ktor.openapigen.annotations.encodings.APIRequestFormat
@@ -64,6 +65,7 @@ object KtorContentProvider : ContentTypeProvider, BodyParser, ResponseSerializer
6465
}
6566

6667
override fun <T> getMediaType(type: KType, apiGen: OpenAPIGen, provider: ModuleProvider<*>, example: T?, usage: ContentTypeProvider.Usage):Map<ContentType, MediaType<T>>? {
68+
if (type == unitKType) return null
6769
val clazz = type.jvmErasure
6870
when (usage) { // check if it is explicitly declared or none is present
6971
ContentTypeProvider.Usage.PARSE -> when {

src/main/kotlin/com/papsign/ktor/openapigen/content/type/multipart/MultipartFormDataContentProvider.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.papsign.ktor.openapigen.content.type.multipart
22

33
import com.papsign.kotlin.reflection.getKType
44
import com.papsign.kotlin.reflection.getObjectSubtypes
5+
import com.papsign.kotlin.reflection.unitKType
56
import com.papsign.ktor.openapigen.OpenAPIGen
67
import com.papsign.ktor.openapigen.annotations.encodings.APIFormatter
78
import com.papsign.ktor.openapigen.content.type.BodyParser
@@ -125,6 +126,7 @@ object MultipartFormDataContentProvider : BodyParser {
125126

126127

127128
override fun <T> getMediaType(type: KType, apiGen: OpenAPIGen, provider: ModuleProvider<*>, example: T?, usage: ContentTypeProvider.Usage): Map<ContentType, MediaType<T>>? {
129+
if (type == unitKType) return null
128130
type.jvmErasure.findAnnotation<FormDataRequest>() ?: return null
129131
when (usage) {
130132
ContentTypeProvider.Usage.PARSE -> {

src/main/kotlin/com/papsign/ktor/openapigen/modules/handlers/RequestHandlerModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class RequestHandlerModule<T : Any>(
4444
} else {
4545
description = requestMeta?.description
4646
}
47-
} ?: RequestBody(map.toMutableMap(), description = requestMeta?.description)
47+
} ?: if (map.isNotEmpty()) RequestBody(map.toMutableMap(), description = requestMeta?.description) else null
4848
}
4949

5050
companion object {

src/main/kotlin/com/papsign/ktor/openapigen/route/Functions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ inline fun <reified P : Any, reified R : Any, reified B : Any, T: OpenAPIRoute<T
7070
val path = P::class.findAnnotation<Path>()
7171
val new = if (path != null) child(ktorRoute.createRouteFromPath(path.path)) else child()
7272
new.apply {
73-
if (Unit !is B) provider.registerModule(
73+
provider.registerModule(
7474
RequestHandlerModule.create(
7575
exampleRequest
7676
)
7777
)
78-
if (Unit !is R) provider.registerModule(
78+
provider.registerModule(
7979
ResponseHandlerModule.create(
8080
exampleResponse
8181
)

0 commit comments

Comments
 (0)