Skip to content

Commit

Permalink
Use @query parameter name as default value #428 (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso authored Sep 25, 2023
1 parent 58078f3 commit ef40717
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
annotation class Query(val value: String = "KTORFIT_DEFAULT_VALUE", val encoded: Boolean = false)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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)
}
"""
)

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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String?>?): People
Expand Down

0 comments on commit ef40717

Please sign in to comment.