From fe8187ba6d03ed90fd97d365ecb8dab97c2e4fff Mon Sep 17 00:00:00 2001 From: Bruno Coelho <4brunu@users.noreply.github.com> Date: Sat, 4 Jun 2022 09:26:59 +0100 Subject: [PATCH] [Kotlin][Client] replace java.nio.* to avoid crash on Android API 25 and bellow (#12529) * [Kotlin][Client] replace java.nio.* to avoid crash on Android API 25 and bellow * [Kotlin][Client] update sample projects * [Kotlin][Client] update sample projects --- .../infrastructure/ApiClient.kt.mustache | 16 ++++++++++------ .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- .../client/infrastructure/ApiClient.kt | 14 +++++++++----- 20 files changed, 181 insertions(+), 101 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache index 50adff43789..32b20282811 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache @@ -206,22 +206,26 @@ import com.squareup.moshi.adapter return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile {{^supportAndroidApiLevel25AndBelow}} // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.{{packageName}}", null).toFile() + val tempFile = java.nio.file.Files.createTempFile("tmp.{{packageName}}", null).toFile() {{/supportAndroidApiLevel25AndBelow}} {{#supportAndroidApiLevel25AndBelow}} - val f = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val tempFile = 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() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 5b21ddef1c8..6db8e7c74f2 100644 --- a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -116,12 +116,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 3dd90f5f36d..bfd00ac6a2e 100644 --- a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 3dd90f5f36d..bfd00ac6a2e 100644 --- a/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 5b21ddef1c8..6db8e7c74f2 100644 --- a/samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -116,12 +116,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 3dd90f5f36d..bfd00ac6a2e 100644 --- a/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 3dd90f5f36d..bfd00ac6a2e 100644 --- a/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 78a723ea49b..17485b2c887 100644 --- a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -117,12 +117,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index e4f45f28927..5040c47697c 100644 --- a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -117,12 +117,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index e00657437a9..804c66706c2 100644 --- a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -119,16 +119,20 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile - val f = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // return tempFile + val tempFile = 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() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index ab05dc14f20..cacd2b743b3 100644 --- a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -120,12 +120,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index c48bc6a8193..4ad22a02df0 100644 --- a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index c48bc6a8193..4ad22a02df0 100644 --- a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 1c2e0a93e8a..61ba8e30416 100644 --- a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ internal open class ApiClient(val baseUrl: String, val client: OkHttpClient = de return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index c48bc6a8193..4ad22a02df0 100644 --- a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index ef5d9840725..9fcf1385050 100644 --- a/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -116,12 +116,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index c48bc6a8193..4ad22a02df0 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index d4ea2444ab5..3d6b7ccb792 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 3dd90f5f36d..bfd00ac6a2e 100644 --- a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) { diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index c48bc6a8193..4ad22a02df0 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie return null } if (T::class.java == File::class.java) { - // return tempfile + // return tempFile // Attention: if you are developing an android app that supports API Level 25 and bellow, please check flag supportAndroidApiLevel25AndBelow in https://openapi-generator.tech/docs/generators/kotlin#config-options - val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() - f.deleteOnExit() - body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) } - return f as T + val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile() + tempFile.deleteOnExit() + body.byteStream().use { inputStream -> + tempFile.outputStream().use { tempFileOutputStream -> + inputStream.copyTo(tempFileOutputStream) + } + } + return tempFile as T } val bodyContent = body.string() if (bodyContent.isEmpty()) {