From c8a30fac3e3e488b4e360f329486782bfb471f4d Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sat, 18 Jun 2022 06:35:28 +0100 Subject: [PATCH 1/3] Fix for common RequestBody contentType bug. --- build.gradle.kts | 4 +-- .../kotlin/okhttp3/RequestBodyTest.kt | 34 +++++++++++++++++++ .../nonJvmMain/kotlin/okhttp3/RequestBody.kt | 2 +- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 okhttp/src/commonTest/kotlin/okhttp3/RequestBodyTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 6c4cff97936b..5e11521035c6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -115,8 +115,8 @@ subprojects { kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() freeCompilerArgs = listOf( - "-Xjvm-default=compatibility", - "-Xopt-in=kotlin.RequiresOptIn" + "-Xjvm-default=all-compatibility", + "-opt-in=kotlin.RequiresOptIn" ) } } diff --git a/okhttp/src/commonTest/kotlin/okhttp3/RequestBodyTest.kt b/okhttp/src/commonTest/kotlin/okhttp3/RequestBodyTest.kt new file mode 100644 index 000000000000..740109945a92 --- /dev/null +++ b/okhttp/src/commonTest/kotlin/okhttp3/RequestBodyTest.kt @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2022 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package okhttp3 + +import assertk.assertThat +import assertk.assertions.isEqualTo +import kotlin.test.Test +import okhttp3.RequestBody.Companion.toRequestBody + +class RequestBodyTest { + @Test + fun correctContentType() { + val body = "Body" + val requestBody = body.toRequestBody(MediaType("text/plain", "text", "plain", arrayOf())) + + val contentType = requestBody.contentType()!! + + assertThat(contentType.mediaType).isEqualTo("text/plain; charset=utf-8") + assertThat(contentType.parameter("charset")).isEqualTo("utf-8") + } +} diff --git a/okhttp/src/nonJvmMain/kotlin/okhttp3/RequestBody.kt b/okhttp/src/nonJvmMain/kotlin/okhttp3/RequestBody.kt index d35fe7e9db29..a3804f15ceb3 100644 --- a/okhttp/src/nonJvmMain/kotlin/okhttp3/RequestBody.kt +++ b/okhttp/src/nonJvmMain/kotlin/okhttp3/RequestBody.kt @@ -40,7 +40,7 @@ actual abstract class RequestBody { val bytes = commonAsUtf8ToByteArray() val resolvedContentType = if (contentType != null && contentType.parameter("charset") == null) { - "$this; charset=utf-8".toMediaTypeOrNull() + "$contentType; charset=utf-8".toMediaTypeOrNull() } else { contentType } From d2afc10f44d5f9f9840657029e4ab23e3acc8ec5 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sat, 18 Jun 2022 06:37:01 +0100 Subject: [PATCH 2/3] Fix for common RequestBody contentType bug. --- build.gradle.kts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 5e11521035c6..6c4cff97936b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -115,8 +115,8 @@ subprojects { kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() freeCompilerArgs = listOf( - "-Xjvm-default=all-compatibility", - "-opt-in=kotlin.RequiresOptIn" + "-Xjvm-default=compatibility", + "-Xopt-in=kotlin.RequiresOptIn" ) } } From 839375d98992a4fe149fc2c6776ebb8f5ff18c03 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sat, 18 Jun 2022 06:40:12 +0100 Subject: [PATCH 3/3] Fix for common RequestBody contentType bug. --- .../okhttp3/{RequestBodyTest.kt => CommonRequestBodyTest.kt} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename okhttp/src/commonTest/kotlin/okhttp3/{RequestBodyTest.kt => CommonRequestBodyTest.kt} (97%) diff --git a/okhttp/src/commonTest/kotlin/okhttp3/RequestBodyTest.kt b/okhttp/src/commonTest/kotlin/okhttp3/CommonRequestBodyTest.kt similarity index 97% rename from okhttp/src/commonTest/kotlin/okhttp3/RequestBodyTest.kt rename to okhttp/src/commonTest/kotlin/okhttp3/CommonRequestBodyTest.kt index 740109945a92..6e13bf824422 100644 --- a/okhttp/src/commonTest/kotlin/okhttp3/RequestBodyTest.kt +++ b/okhttp/src/commonTest/kotlin/okhttp3/CommonRequestBodyTest.kt @@ -20,7 +20,7 @@ import assertk.assertions.isEqualTo import kotlin.test.Test import okhttp3.RequestBody.Companion.toRequestBody -class RequestBodyTest { +class CommonRequestBodyTest { @Test fun correctContentType() { val body = "Body"