From 1e83f434a5cd70f613f7b4d91ab323c6aa8f9c60 Mon Sep 17 00:00:00 2001 From: Jens Klingenberg Date: Sat, 3 Jun 2023 08:50:14 +0200 Subject: [PATCH] fix: list parsing error (#326) --- .github/PULL_REQUEST_TEMPLATE.md | 1 - docs/CHANGELOG.md | 1 + .../de/jensklingenberg/ktorfit/Ktorfit.kt | 4 +--- .../DefaultSuspendResponseConverterFactory.kt | 19 ++----------------- .../ktorfit/internal/KtorfitClient.kt | 6 ++++++ 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 84ae6ae47..9a6ed6913 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,4 +1,3 @@ ### :thinking: DOD Checklist -- [ ] My changes are covered with proper tests. - [ ] I did all relevant changes to the documentation and the [changelog](https://github.com/Foso/Ktorfit/blob/master/docs/CHANGELOG.md). diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 6ba6beea2..27a340681 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Removed ### Fixed +#236 Parsing error for list/array ### Security diff --git a/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/Ktorfit.kt b/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/Ktorfit.kt index 4b536b5cd..56c66c26f 100644 --- a/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/Ktorfit.kt +++ b/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/Ktorfit.kt @@ -221,9 +221,7 @@ public class Ktorfit private constructor( baseUrl = _baseUrl, httpClient = _httpClient, responseConverters = _responseConverter, - suspendResponseConverters = _suspendResponseConverter.also { - it.add(DefaultSuspendResponseConverterFactory()) - }, + suspendResponseConverters = _suspendResponseConverter, requestConverters = _requestConverter, converterFactories = listOf(KtorfitDefaultConverterFactory()) + _factories.toList() ) diff --git a/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/converter/builtin/DefaultSuspendResponseConverterFactory.kt b/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/converter/builtin/DefaultSuspendResponseConverterFactory.kt index 86e05f3b4..146a46124 100644 --- a/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/converter/builtin/DefaultSuspendResponseConverterFactory.kt +++ b/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/converter/builtin/DefaultSuspendResponseConverterFactory.kt @@ -2,17 +2,14 @@ package de.jensklingenberg.ktorfit.converter.builtin import de.jensklingenberg.ktorfit.Ktorfit import de.jensklingenberg.ktorfit.converter.Converter -import de.jensklingenberg.ktorfit.converter.SuspendResponseConverter import de.jensklingenberg.ktorfit.internal.TypeData -import io.ktor.client.call.* import io.ktor.client.statement.* -import io.ktor.util.reflect.* /** * Will be used when no other suspend converter was found * It is automatically applied last */ -internal class DefaultSuspendResponseConverterFactory : Converter.Factory, SuspendResponseConverter { +internal class DefaultSuspendResponseConverterFactory : Converter.Factory { class DefaultSuspendResponseConverter(val typeData: TypeData) : Converter.SuspendResponseConverter { @@ -28,21 +25,9 @@ internal class DefaultSuspendResponseConverterFactory : Converter.Factory, Suspe override fun suspendResponseConverter( typeData: TypeData, ktorfit: Ktorfit - ): Converter.SuspendResponseConverter? { + ): Converter.SuspendResponseConverter { return DefaultSuspendResponseConverter(typeData) } - override suspend fun wrapSuspendResponse( - typeData: TypeData, - requestFunction: suspend () -> Pair, - ktorfit: Ktorfit - ): Any { - val (info, response) = requestFunction() - return response.body(info) - } - - override fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean { - return true - } } diff --git a/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/internal/KtorfitClient.kt b/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/internal/KtorfitClient.kt index b1f4eca35..3693f4996 100644 --- a/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/internal/KtorfitClient.kt +++ b/ktorfit-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/internal/KtorfitClient.kt @@ -1,6 +1,7 @@ package de.jensklingenberg.ktorfit.internal import de.jensklingenberg.ktorfit.Ktorfit +import de.jensklingenberg.ktorfit.converter.builtin.DefaultSuspendResponseConverterFactory import io.ktor.client.* import io.ktor.client.request.* import io.ktor.client.statement.* @@ -93,6 +94,11 @@ internal class KtorfitClient(private val ktorfit: Ktorfit) : Client { */ handleDeprecatedSuspendResponseConverters(requestData)?.let { return it + } ?: DefaultSuspendResponseConverterFactory().suspendResponseConverter(returnTypeData, ktorfit)?.let { + val response = httpClient.request { + requestBuilder(requestData) + } + return it.convert(response) as ReturnType? } ?: throw IllegalStateException("No SuspendResponseConverter found for " + returnTypeData.qualifiedName) } catch (exception: Exception) {