Skip to content

Commit 6593fde

Browse files
authored
Merge pull request #59 from vansickle/preserve-generic-type-info
Preserve generic type info when parse body
2 parents 729aef7 + 8c8a556 commit 6593fde

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import io.ktor.application.ApplicationCall
44
import io.ktor.http.ContentType
55
import io.ktor.util.pipeline.PipelineContext
66
import kotlin.reflect.KClass
7+
import kotlin.reflect.KType
78

89
interface BodyParser: ContentTypeProvider {
910
fun <T: Any> getParseableContentTypes(clazz: KClass<T>): List<ContentType>
10-
suspend fun <T: Any> parseBody(clazz: KClass<T>, request: PipelineContext<Unit, ApplicationCall>): T
11+
suspend fun <T: Any> parseBody(clazz: KType, request: PipelineContext<Unit, ApplicationCall>): T
1112
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ object BinaryContentTypeParser: BodyParser, ResponseSerializer, OpenAPIGenModule
5050
}
5151

5252
@Suppress("UNCHECKED_CAST")
53-
override suspend fun <T : Any> parseBody(clazz: KClass<T>, request: PipelineContext<Unit, ApplicationCall>): T {
54-
return clazz.getAcceptableConstructor().call( request.context.receiveStream())
53+
override suspend fun <T : Any> parseBody(clazz: KType, request: PipelineContext<Unit, ApplicationCall>): T {
54+
return (clazz.classifier as KClass<T>).getAcceptableConstructor().call( request.context.receiveStream())
5555
}
5656

5757
override fun <T> getMediaType(type: KType, apiGen: OpenAPIGen, provider: ModuleProvider<*>, example: T?, usage: ContentTypeProvider.Usage): Map<ContentType, MediaTypeModel<T>>? {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ object KtorContentProvider : ContentTypeProvider, BodyParser, ResponseSerializer
6868
return contentTypes!!.toList()
6969
}
7070

71-
override suspend fun <T: Any> parseBody(clazz: KClass<T>, request: PipelineContext<Unit, ApplicationCall>): T {
71+
override suspend fun <T: Any> parseBody(clazz: KType, request: PipelineContext<Unit, ApplicationCall>): T {
7272
return request.call.receive(clazz)
7373
}
7474

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ object MultipartFormDataContentProvider : BodyParser, OpenAPIGenModuleExtension
6868
private val typeContentTypes = HashMap<KType, Map<String, MediaTypeEncodingModel>>()
6969

7070

71-
override suspend fun <T : Any> parseBody(clazz: KClass<T>, request: PipelineContext<Unit, ApplicationCall>): T {
71+
override suspend fun <T : Any> parseBody(clazz: KType, request: PipelineContext<Unit, ApplicationCall>): T {
7272
val objectMap = HashMap<String, Any>()
7373
request.context.receiveMultipart().forEachPart {
7474
val name = it.name
@@ -86,7 +86,7 @@ object MultipartFormDataContentProvider : BodyParser, OpenAPIGenModuleExtension
8686
}
8787
}
8888
}
89-
val ctor = clazz.primaryConstructor!!
89+
val ctor = (clazz.classifier as KClass<T>).primaryConstructor!!
9090
return ctor.callBy(ctor.parameters.associateWith {
9191
val raw = objectMap[it.openAPIName]
9292
if ((raw == null || (raw !is InputStream && streamTypes.contains(it.type))) && it.type.isMarkedNullable) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.ktor.routing.application
2323
import io.ktor.routing.contentType
2424
import io.ktor.util.pipeline.PipelineContext
2525
import kotlin.reflect.KClass
26+
import kotlin.reflect.typeOf
2627

2728
abstract class OpenAPIRoute<T : OpenAPIRoute<T>>(val ktorRoute: Route, val provider: CachingModuleProvider) {
2829
private val log = classLogger()
@@ -58,7 +59,7 @@ abstract class OpenAPIRoute<T : OpenAPIRoute<T>>(val ktorRoute: Route, val provi
5859
getContentTypesMap(B::class).forEach { (contentType, parsers) ->
5960
contentType(contentType) {
6061
handle {
61-
val receive: B = parsers.getBodyParser(call.request.contentType()).parseBody(B::class, this)
62+
val receive: B = parsers.getBodyParser(call.request.contentType()).parseBody(typeOf<B>(), this)
6263
val params: P = if (Unit is P) Unit else parameterHandler.parse(call.parameters, call.request.headers)
6364
pass(this, responder, PHandler.handle(params), BHandler.handle(receive))
6465
}

0 commit comments

Comments
 (0)