From ef4071790b2d0ab984fece238406f04eee34b6fb Mon Sep 17 00:00:00 2001 From: Jens Klingenberg Date: Mon, 25 Sep 2023 21:16:39 +0200 Subject: [PATCH] Use @Query parameter name as default value #428 (#429) --- docs/CHANGELOG.md | 6 +++--- .../kotlin/de/jensklingenberg/ktorfit/http/Query.kt | 4 ++-- .../de/jensklingenberg/ktorfit/utils/KSValueParameterExt.kt | 2 +- .../de/jensklingenberg/ktorfit/QueryAnnotationsTest.kt | 4 ++-- .../de/jensklingenberg/ktorfit/demo/JvmPlaceHolderApi.kt | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 6d64e4685..34d9ab21d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -11,9 +11,9 @@ Unreleased ### Added ### Changed -- Allow nullable body type #424 -- Use @Path parameter name as default value #426 - +- Allow nullable body type #424 +- Use @Path parameter name as default value #426 +- Use @Query parameter name as default value #428 ### Deprecated ### Removed ### Fixed diff --git a/ktorfit-annotations/src/commonMain/kotlin/de/jensklingenberg/ktorfit/http/Query.kt b/ktorfit-annotations/src/commonMain/kotlin/de/jensklingenberg/ktorfit/http/Query.kt index b8ab30c9b..7de1ed63d 100644 --- a/ktorfit-annotations/src/commonMain/kotlin/de/jensklingenberg/ktorfit/http/Query.kt +++ b/ktorfit-annotations/src/commonMain/kotlin/de/jensklingenberg/ktorfit/http/Query.kt @@ -15,9 +15,9 @@ package de.jensklingenberg.ktorfit.http * A request with getCommentsById(listOf("3",null,"4")) will result in the relative URL “comments?postId=3&postId=4” * * ===== - * [value] is the key of the query parameter + * @param value The default value will be replaced with the name of the parameter that is annotated.It is the key of the query parameter * null values are ignored * @param encoded true means that this value is already URL encoded and will not be encoded again */ @Target(AnnotationTarget.VALUE_PARAMETER) -annotation class Query(val value: String, val encoded: Boolean = false) \ No newline at end of file +annotation class Query(val value: String = "KTORFIT_DEFAULT_VALUE", val encoded: Boolean = false) \ No newline at end of file diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/KSValueParameterExt.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/KSValueParameterExt.kt index 2a171acb8..b863e9689 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/KSValueParameterExt.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/KSValueParameterExt.kt @@ -35,7 +35,7 @@ fun KSValueParameter.getHeaderMapAnnotation(): HeaderMap? { @OptIn(KspExperimental::class) fun KSValueParameter.getQueryAnnotation(): Query? { return this.getAnnotationsByType(de.jensklingenberg.ktorfit.http.Query::class).firstOrNull()?.let { - return Query(it.value, it.encoded) + return Query(it.value.replace(KTORFIT_DEFAULT_VALUE, this.name.safeString()), it.encoded) } } diff --git a/ktorfit-ksp/src/test/kotlin/de/jensklingenberg/ktorfit/QueryAnnotationsTest.kt b/ktorfit-ksp/src/test/kotlin/de/jensklingenberg/ktorfit/QueryAnnotationsTest.kt index 59c4aac70..144862d32 100644 --- a/ktorfit-ksp/src/test/kotlin/de/jensklingenberg/ktorfit/QueryAnnotationsTest.kt +++ b/ktorfit-ksp/src/test/kotlin/de/jensklingenberg/ktorfit/QueryAnnotationsTest.kt @@ -24,7 +24,7 @@ import de.jensklingenberg.ktorfit.http.Query interface TestService { @GET("posts") - suspend fun test(@Query("name") testQuery: String, @Query("user",true) testQuery2: Int) + suspend fun test(@Query("name") testQuery: String, @Query(encoded = true) testQuery2: Int) } """ ) @@ -32,7 +32,7 @@ interface TestService { val expectedQueriesArgumentText = "url{\n" + " takeFrom(ktorfitClient.baseUrl + \"posts\")\n" + " testQuery?.let{ parameter(\"name\", \"\$it\") }\n" + - " testQuery2?.let{ encodedParameters.append(\"user\", \"\$it\") }\n" + + " testQuery2?.let{ encodedParameters.append(\"testQuery2\", \"\$it\") }\n" + " }" val compilation = getCompilation(listOf(source)) diff --git a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvmPlaceHolderApi.kt b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvmPlaceHolderApi.kt index 02bcb9c0c..8f4890924 100644 --- a/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvmPlaceHolderApi.kt +++ b/sandbox/src/jvmMain/kotlin/de/jensklingenberg/ktorfit/demo/JvmPlaceHolderApi.kt @@ -13,7 +13,7 @@ interface JvmPlaceHolderApi : StarWarsApi { suspend fun getPersonById2(@Path("id") peopleId: Int): People @GET("people/{id}/") - suspend fun testQuery(@Path("id") peopleId: Int, @Query("hello") world: String? = "World"): People + suspend fun testQuery(@Path("id") peopleId: Int, @Query world: String? = "World"): People @GET("people/{id}/") suspend fun testQueryName(@Path("id") peopleId: Int, @QueryName na : List?): People