Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kotlin][client] fix Android crash on API level 25 and bellow #9578

Merged
merged 7 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/configs/kotlin-json-request-string.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ additionalProperties:
requestDateConverter: toString
artifactId: kotlin-petstore-json-request-string
parcelizeModels: true
supportAndroidApiLevel25AndBelow: true
1 change: 1 addition & 0 deletions docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |null|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |null|
|sourceFolder|source folder for generated code| |src/main/kotlin|
|supportAndroidApiLevel25AndBelow|[WARNING] This flag with generate code that has as a know security vulnerability. It uses `kotlin.io.createTempFile` instead of `java.nio.file.Files.createTempFile` in oder to support Android API level 25 and bellow. For more info, please check the following links https://github.com/OpenAPITools/openapi-generator/security/advisories/GHSA-23x4-m842-fmwf, https://github.com/OpenAPITools/openapi-generator/pull/9284| |false|
|useCoroutines|Whether to use the Coroutines adapter with the retrofit2 library.| |false|
|useRxJava|Whether to use the RxJava adapter with the retrofit2 library.| |false|
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library.| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {

public static final String MOSHI_CODE_GEN = "moshiCodeGen";

public static final String SUPPORT_ANDROID_API_LEVEL_25_AND_BELLOW = "supportAndroidApiLevel25AndBelow";

protected static final String VENDOR_EXTENSION_BASE_NAME_LITERAL = "x-base-name-literal";

protected String dateLibrary = DateLibrary.JAVA8.value;
Expand Down Expand Up @@ -208,6 +210,8 @@ public KotlinClientCodegen() {
cliOptions.add(CliOption.newBoolean(USE_COROUTINES, "Whether to use the Coroutines adapter with the retrofit2 library."));

cliOptions.add(CliOption.newBoolean(MOSHI_CODE_GEN, "Whether to enable codegen with the Moshi library. Refer to the [official Moshi doc](https://github.com/square/moshi#codegen) for more info."));

cliOptions.add(CliOption.newBoolean(SUPPORT_ANDROID_API_LEVEL_25_AND_BELLOW, "[WARNING] This flag with generate code that has as a know security vulnerability. It uses `kotlin.io.createTempFile` instead of `java.nio.file.Files.createTempFile` in oder to support Android API level 25 and bellow. For more info, please check the following links https://github.com/OpenAPITools/openapi-generator/security/advisories/GHSA-23x4-m842-fmwf, https://github.com/OpenAPITools/openapi-generator/pull/9284"));
wing328 marked this conversation as resolved.
Show resolved Hide resolved
}

public CodegenType getTag() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package {{packageName}}.infrastructure

{{#supportAndroidApiLevel25AndBelow}}
import android.os.Build
{{/supportAndroidApiLevel25AndBelow}}
import okhttp3.Credentials
import okhttp3.OkHttpClient
import okhttp3.RequestBody
Expand Down Expand Up @@ -28,7 +31,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
{{^threetenbp}}
import java.time.LocalDate
Expand Down Expand Up @@ -176,7 +178,17 @@ import org.threeten.bp.OffsetTime
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.{{packageName}}", null).toFile()
{{^supportAndroidApiLevel25AndBelow}}
val f = java.nio.file.Files.createTempFile("tmp.{{packageName}}", null).toFile()
{{/supportAndroidApiLevel25AndBelow}}
{{#supportAndroidApiLevel25AndBelow}}
val f = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
java.nio.file.Files.createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null).toFile()
} else {
@Suppress("DEPRECATION")
createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null)
}
{{/supportAndroidApiLevel25AndBelow}}
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openapitools.client.infrastructure

import android.os.Build
import okhttp3.Credentials
import okhttp3.OkHttpClient
import okhttp3.RequestBody
Expand All @@ -16,7 +17,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +119,12 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
java.nio.file.Files.createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null).toFile()
} else {
@Suppress("DEPRECATION")
createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null)
}
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ internal open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -117,7 +116,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import java.net.URLConnection
import java.nio.file.Files
import java.util.Date
import java.time.LocalDate
import java.time.LocalDateTime
Expand Down Expand Up @@ -119,7 +118,7 @@ open class ApiClient(val baseUrl: String) {
}
if (T::class.java == File::class.java) {
// return tempfile
val f = Files.createTempFile("tmp.org.openapitools.client", null).toFile()
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
f.deleteOnExit()
val out = BufferedWriter(FileWriter(f))
out.write(bodyContent)
Expand Down