Skip to content

Commit

Permalink
Use @field parameter name as default value #430
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Sep 25, 2023
1 parent ef40717 commit e66f318
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Unreleased
- Allow nullable body type #424
- Use @Path parameter name as default value #426
- Use @Query parameter name as default value #428
- Use @Field parameter name as default value #430

### Deprecated
### Removed
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package de.jensklingenberg.ktorfit.http

/**
* Needs to be used in combination with [FormUrlEncoded]
*
* @param value The default value will be replaced with the name of the parameter that is annotated.
* @param encoded true means that this value is already URL encoded and will not be encoded again
* @see FormUrlEncoded
* @see FieldMap
*/
@Target(AnnotationTarget.VALUE_PARAMETER)
annotation class Field(val value: String, val encoded: Boolean = false)
annotation class Field(val value: String = "KTORFIT_DEFAULT_VALUE", val encoded: Boolean = false)
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun KSValueParameter.getQueryMapAnnotation(): QueryMap? {
@OptIn(KspExperimental::class)
fun KSValueParameter.getFieldAnnotation(): Field? {
return this.getAnnotationsByType(de.jensklingenberg.ktorfit.http.Field::class).firstOrNull()?.let {
return Field(it.value, it.encoded)
return Field(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 @@ -102,6 +102,41 @@ interface TestService {
Assert.assertEquals(true, actualSource.contains(expectedFieldsBuilderText))
}

@Test
fun whenFieldAnnotationWitDefaultValueFoundAddItToFieldsBuilder() {

val source = SourceFile.kotlin(
"Source.kt", """
package com.example.api
import de.jensklingenberg.ktorfit.http.POST
import de.jensklingenberg.ktorfit.http.Field
import de.jensklingenberg.ktorfit.http.FormUrlEncoded
interface TestService {
@FormUrlEncoded
@POST("posts")
suspend fun test(@Field name: String)
}
"""
)

val expectedFieldsBuilderText = """val _formParameters = Parameters.build {
name?.let{ append("name", "ä{it}") }
}
setBody(FormDataContent(_formParameters))""".trimMargin().replace("ä", "$")

val compilation = getCompilation(listOf(source))
val result = compilation.compile()
Assert.assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode)
val generatedSourcesDir = compilation.kspSourcesDir
val generatedFile = File(
generatedSourcesDir,
"/kotlin/com/example/api/_TestServiceImpl.kt"
)
val actualSource = generatedFile.readText()
Assert.assertEquals(true, actualSource.contains(expectedFieldsBuilderText))
}

@Test
fun whenFieldAnnotationWithListFoundAddItToFieldsBuilder() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface KtorSamplesApi {
@FormUrlEncoded
suspend fun signup(
@Field("username", true) headers: String?,
@Field("email") email: String,
@Field email: String,
@Field("password") password: String,
@Field("confirmation") confirmation: String,
@Field("names") names: List<String>
Expand Down

0 comments on commit e66f318

Please sign in to comment.