11package com.papsign.ktor.openapigen.content.type.ktor
22
3- import com.papsign.ktor.openapigen.unitKType
43import com.papsign.ktor.openapigen.OpenAPIGen
54import com.papsign.ktor.openapigen.OpenAPIGenModuleExtension
65import com.papsign.ktor.openapigen.annotations.encodings.APIRequestFormat
@@ -13,15 +12,13 @@ import com.papsign.ktor.openapigen.model.schema.SchemaModel
1312import com.papsign.ktor.openapigen.modules.ModuleProvider
1413import com.papsign.ktor.openapigen.modules.ofType
1514import com.papsign.ktor.openapigen.schema.builder.provider.FinalSchemaBuilderProviderModule
16- import io.ktor.application.ApplicationCall
17- import io.ktor.application.call
18- import io.ktor.application.featureOrNull
19- import io.ktor.features.ContentNegotiation
20- import io.ktor.http.ContentType
21- import io.ktor.http.HttpStatusCode
22- import io.ktor.request.receive
23- import io.ktor.response.respond
24- import io.ktor.util.pipeline.PipelineContext
15+ import com.papsign.ktor.openapigen.unitKType
16+ import io.ktor.application.*
17+ import io.ktor.features.*
18+ import io.ktor.http.*
19+ import io.ktor.request.*
20+ import io.ktor.response.*
21+ import io.ktor.util.pipeline.*
2522import kotlin.reflect.KType
2623import kotlin.reflect.full.findAnnotation
2724import kotlin.reflect.jvm.jvmErasure
@@ -40,45 +37,65 @@ object KtorContentProvider : ContentTypeProvider, BodyParser, ResponseSerializer
4037 return contentTypes
4138 }
4239
43- override fun <T > getMediaType (type : KType , apiGen : OpenAPIGen , provider : ModuleProvider <* >, example : T ? , usage : ContentTypeProvider .Usage ):Map <ContentType , MediaTypeModel <T >>? {
40+ override fun <T > getMediaType (
41+ type : KType ,
42+ apiGen : OpenAPIGen ,
43+ provider : ModuleProvider <* >,
44+ example : T ? ,
45+ usage : ContentTypeProvider .Usage
46+ ): Map <ContentType , MediaTypeModel <T >>? {
4447 if (type == unitKType) return null
4548 val clazz = type.jvmErasure
4649 when (usage) { // check if it is explicitly declared or none is present
4750 ContentTypeProvider .Usage .PARSE -> when {
48- clazz.findAnnotation<KtorRequest >() != null -> {}
49- clazz.annotations.none { it.annotationClass.findAnnotation<APIRequestFormat >() != null } -> {}
51+ clazz.findAnnotation<KtorRequest >() != null -> {
52+ }
53+ clazz.annotations.none { it.annotationClass.findAnnotation<APIRequestFormat >() != null } -> {
54+ }
5055 else -> return null
5156 }
5257 ContentTypeProvider .Usage .SERIALIZE -> when {
53- clazz.findAnnotation<KtorResponse >() != null -> {}
54- clazz.annotations.none { it.annotationClass.findAnnotation<APIResponseFormat >() != null } -> {}
58+ clazz.findAnnotation<KtorResponse >() != null -> {
59+ }
60+ clazz.annotations.none { it.annotationClass.findAnnotation<APIResponseFormat >() != null } -> {
61+ }
5562 else -> return null
5663 }
5764 }
5865 val contentTypes = initContentTypes(apiGen) ? : return null
5966 val schemaBuilder = provider.ofType<FinalSchemaBuilderProviderModule >().last().provide(apiGen, provider)
67+
6068 @Suppress(" UNCHECKED_CAST" )
61- val media = MediaTypeModel (schemaBuilder.build(type) as SchemaModel <T >, example)
69+ val media = MediaTypeModel (schemaBuilder.build(type) as SchemaModel <T >, example)
6270 return contentTypes.associateWith { media.copy() }
6371 }
6472
6573 override fun <T : Any > getParseableContentTypes (type : KType ): List <ContentType > {
6674 return contentTypes!! .toList()
6775 }
6876
69- override suspend fun <T : Any > parseBody (clazz : KType , request : PipelineContext <Unit , ApplicationCall >): T {
77+ override suspend fun <T : Any > parseBody (clazz : KType , request : PipelineContext <Unit , ApplicationCall >): T {
7078 return request.call.receive(clazz)
7179 }
7280
73- override fun <T : Any > getSerializableContentTypes (type : KType ): List <ContentType > {
81+ override fun <T : Any > getSerializableContentTypes (type : KType ): List <ContentType > {
7482 return contentTypes!! .toList()
7583 }
7684
77- override suspend fun <T : Any > respond (response : T , request : PipelineContext <Unit , ApplicationCall >, contentType : ContentType ) {
78- request.call.respond(response)
85+ override suspend fun <T : Any > respond (
86+ response : T ,
87+ request : PipelineContext <Unit , ApplicationCall >,
88+ contentType : ContentType
89+ ) {
90+ request.call.respond(response as Any )
7991 }
8092
81- override suspend fun <T : Any > respond (statusCode : HttpStatusCode , response : T , request : PipelineContext <Unit , ApplicationCall >, contentType : ContentType ) {
82- request.call.respond(statusCode, response)
93+ override suspend fun <T : Any > respond (
94+ statusCode : HttpStatusCode ,
95+ response : T ,
96+ request : PipelineContext <Unit , ApplicationCall >,
97+ contentType : ContentType
98+ ) {
99+ request.call.respond(statusCode, response as Any )
83100 }
84101}
0 commit comments