Skip to content

Commit

Permalink
Fix headers code generation #752 (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso authored Feb 4, 2025
1 parent d6b9dcb commit 737ab5c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ ktlint_standard_comment-wrapping = disabled
# noinspection EditorConfigKeyCorrectness
ktlint_standard_block-comment-initial-star-alignment = disabled
# noinspection EditorConfigKeyCorrectness
ktlint_standard_no-single-line-block-comment = disabled
ktlint_standard_no-single-line-block-comment = disabled
max_line_length=off
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Note: This project needs KSP to work and every new Ktorfit with an update of the
But there is no intent to bump the Ktorfit major version for every KSP update.

# Unreleased
## Added
- Provide option to not use the compiler plugin #764
You can now set the Kotlin version for the compiler plugin.
By default, it will use the Kotlin version of the project. You can set it to "-" to disable the plugin.
Expand All @@ -20,6 +21,9 @@ ktorfit{
}
```

## Fixed
- @Headers annotation produces unexpected newline in generated code by ksp plugin #752

# [2.2.0]()
* Supported Kotlin version: 2.0.0; 2.0.10; 2.0.20, 2.1.0-Beta1; 2.0.21-RC, 2.0.21, 2.1.0-RC, 2.1.0-RC2, 2.1.0, 2.1.10
* Supported KSP version: 1.0.27, 1.0.28, 1.0.29
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ detekt = "1.23.7"
junit = "4.13.2"
kctfork = "0.7.0"
kotlin = "2.1.10"
kotlinPoet = "1.18.1"
kotlinPoet = "2.0.0"
kspVersion = "2.1.10-1.0.29"

groupId = "de.jensklingenberg.ktorfit"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fun KSValueParameter.getParamAnnotationList(logger: KSPLogger): List<ParameterAn

ksValueParameter.getHeaderMapAnnotation()?.let {
// TODO: Find out how isAssignableFrom works
if (!ksValueParameter.type.toString().endsWith(KEY_MAP)) {
if (!ksValueParameter.type.toString().substringBefore("<").endsWith(KEY_MAP)) {
logger.error(KtorfitError.HEADER_MAP_PARAMETER_TYPE_MUST_BE_MAP, ksValueParameter)
}
val mapKey =
Expand All @@ -143,7 +143,7 @@ fun KSValueParameter.getParamAnnotationList(logger: KSPLogger): List<ParameterAn
}

ksValueParameter.getQueryMapAnnotation()?.let {
if (!ksValueParameter.type.toString().endsWith(KEY_MAP)) {
if (!ksValueParameter.type.toString().substringBefore("<").endsWith(KEY_MAP)) {
logger.error(KtorfitError.QUERY_MAP_PARAMETER_TYPE_MUST_BE_MAP, ksValueParameter)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,8 @@ interface TestService {
val expectedQueriesArgumentText =
"url{\n" +
" takeFrom(_ktorfit.baseUrl + \"posts\")\n" +
" testQueryMap?.forEach { entry -> entry.value?.let{ parameter(entry.key, \"\${entry.value}\") }\n" +
" }\n" +
" testQueryMap2?.forEach { entry -> entry.value?.let{ encodedParameters.append(entry.key,\n" +
" \"\${entry.value}\") } }\n" +
" testQueryMap?.forEach { entry -> entry.value?.let{ parameter(entry.key, \"\${entry.value}\") } }\n" +
" testQueryMap2?.forEach { entry -> entry.value?.let{ encodedParameters.append(entry.key, \"\${entry.value}\") } }\n" +
" }"

val compilation = getCompilation(listOf(source))
Expand Down Expand Up @@ -189,8 +187,7 @@ fun example(@Query("name") testQuery: String, @QueryName testQueryName: String,
" takeFrom(_ktorfit.baseUrl + \"posts\")\n" +
" testQuery?.let{ parameter(\"name\", \"\$it\") }\n" +
" parameters.appendAll(\"\$testQueryName\", emptyList())\n" +
" name?.forEach { entry -> entry.value?.let{ encodedParameters.append(entry.key,\n" +
" \"\${entry.value}\") } }\n" +
" name?.forEach { entry -> entry.value?.let{ encodedParameters.append(entry.key, \"\${entry.value}\") } }\n" +
" }"

val compilation = getCompilation(listOf(source))
Expand Down
3 changes: 3 additions & 0 deletions sandbox/src/commonMain/kotlin/com/example/model/ExampleApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package com.example.model

import de.jensklingenberg.ktorfit.Response
import de.jensklingenberg.ktorfit.http.GET
import de.jensklingenberg.ktorfit.http.Headers

interface ExampleApi {

@Headers("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0")
@GET("example.json")
suspend fun getUser(): Response<User>

Expand Down

0 comments on commit 737ab5c

Please sign in to comment.