Skip to content

Commit

Permalink
- remove Content-Type Header for FormUrlEncoded
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Jun 7, 2022
1 parent 838e247 commit 26ba012
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.jensklingenberg.androidonlyexample

import de.jensklingenberg.ktorfit.http.GET
import de.jensklingenberg.ktorfit.http.Path

interface GitHubService {
@GET("repos/{user}/{repo}/releases/latest")
suspend fun getLatestRelease(
@Path("user") user: String,
@Path("repo") repo: String,
): String
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SONATYPE_STAGING_PROFILE=de.jensklingenberg

org.gradle.configureondemand=false
kapt.include.compile.classpath=false

kotlin.native.ignoreDisabledTargets=true
kotlin.native.binary.freezing=disabled
kotlin.mpp.enableGranularSourceSetsMetadata=true
ksp.version.check=false
Expand Down
1 change: 1 addition & 0 deletions ktorfit-ksp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
testImplementation("junit:junit:4.13.2")
testImplementation("com.github.tschuchortdev:kotlin-compile-testing-ksp:1.4.8")
implementation("com.squareup:kotlinpoet:1.11.0")
implementation("com.squareup:kotlinpoet-ksp:1.11.0")
testImplementation("com.google.truth:truth:1.1.3")
compileOnly ("com.google.auto.service:auto-service:1.0.1")
kapt ("com.google.auto.service:auto-service:1.0.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fun getFileSpec(classData: ClassData): FileSpec {
.addImport("de.jensklingenberg.ktorfit.internal", "KtorfitClient")
.addType(
TypeSpec.classBuilder(implClassName)
.addModifiers(classData.modifiers)
.addSuperinterface(ClassName(classData.packageName, classData.name))
.addKtorfitSuperInterface(classData.superClasses)
.primaryConstructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.jensklingenberg.ktorfit.model

import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.squareup.kotlinpoet.KModifier

/**
* @param superClasses List of qualifiedNames of interface that a Ktorfit interface extends
Expand All @@ -11,5 +12,6 @@ data class ClassData(
val functions: List<FunctionData>,
val imports: List<String>,
val superClasses: List<String> = emptyList(),
val properties: List<KSPropertyDeclaration> = emptyList()
val properties: List<KSPropertyDeclaration> = emptyList(),
val modifiers: List<KModifier> = emptyList()
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class KtorfitError {
const val COULD_NOT_FIND_ANY_KTORFIT_ANNOTATIONS_IN_CLASS = "Could not find any Ktorfit annotations in class"
fun MISSING_EITHER_KEYWORD_URL_OrURL_PARAMETER(keyword: String) = "Missing either @$keyword URL or @Url parameter"
const val JAVA_INTERFACES_ARE_NOT_SUPPORTED = "Java Interfaces are not supported"
const val INTERNAL_INTERFACES_ARE_NOT_SUPPORTED = "internal Interfaces are not supported"
const val INTERFACE_NEEDS_TO_HAVE_A_PACKAGE = "Interface needs to have a package"
const val ONLY_ONE_HTTP_METHOD_IS_ALLOWED = "Only one HTTP method is allowed."
const val FORM_URL_ENCODED_CAN_ONLY_BE_SPECIFIED_ON_HTTP_METHODS_WITH_REQUEST_BODY = "FormUrlEncoded can only be specified on HTTP methods with request body (e.g., @POST)."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package de.jensklingenberg.ktorfit.parser
import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.Modifier
import com.squareup.kotlinpoet.ksp.toKModifier
import de.jensklingenberg.ktorfit.ktorfitError
import de.jensklingenberg.ktorfit.model.ClassData
import de.jensklingenberg.ktorfit.model.FunctionData
import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.INTERFACE_NEEDS_TO_HAVE_A_PACKAGE
import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.INTERNAL_INTERFACES_ARE_NOT_SUPPORTED
import de.jensklingenberg.ktorfit.resolveTypeName
import java.io.File

Expand Down Expand Up @@ -58,5 +61,17 @@ fun toClassData(ksClassDeclaration: KSClassDeclaration, logger: KSPLogger): Clas
if (packageName.isEmpty()) {
logger.ktorfitError(INTERFACE_NEEDS_TO_HAVE_A_PACKAGE, ksClassDeclaration)
}
return ClassData(className, packageName, functionDataList, imports, supertypes, properties)

if (ksClassDeclaration.modifiers.contains(Modifier.INTERNAL)) {
logger.ktorfitError(INTERNAL_INTERFACES_ARE_NOT_SUPPORTED, ksClassDeclaration)
}

return ClassData(
className,
packageName,
functionDataList,
imports,
supertypes,
properties,
modifiers = ksClassDeclaration.modifiers.mapNotNull { it.toKModifier() })
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ fun getHeadersArgumentText(
val paramsWithHeaderMap = paramList.filter { it.hasAnnotation<HeaderMap>() }

if (functionAnnotations.any { it is FormUrlEncoded }) {
headerList.add(Pair("\"Content-Type\"", "\"application/x-www-form-urlencoded\""))
/**
* Can't add Content Type Header, because it leads to Ktor issues https://github.com/ktorio/ktor/issues/1127
*/
// headerList.add(Pair("\"Content-Type\"", "\"application/x-www-form-urlencoded\""))
}

paramsWithHeaderAnno.forEach { myParam ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ interface TestService {
val expectedBodyDataArgumentText = """public override suspend fun test(id: String): String {
val requestData = RequestData(method="POST",
relativeUrl="user",
headers = listOf(HeaderData("Content-Type","application/x-www-form-urlencoded")),
fields = listOf(FieldData(false,"id",id,FieldType.FIELD)),
qualifiedRawTypeName="kotlin.String")
Expand Down
4 changes: 2 additions & 2 deletions local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Sun May 08 21:33:07 CEST 2022
sdk.dir=/Users/jensklingenberg/Library/Android/sdk
#Thu May 26 21:30:01 CEST 2022
sdk.dir=/home/jens/Code/Android/Sdk

0 comments on commit 26ba012

Please sign in to comment.