Skip to content

Commit fe8187b

Browse files
authored
[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
1 parent 45a3b15 commit fe8187b

File tree

20 files changed

+181
-101
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure
  • samples/client/petstore
    • kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure
    • kotlin/src/main/kotlin/org/openapitools/client/infrastructure

20 files changed

+181
-101
lines changed

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,22 +206,26 @@ import com.squareup.moshi.adapter
206206
return null
207207
}
208208
if (T::class.java == File::class.java) {
209-
// return tempfile
209+
// return tempFile
210210
{{^supportAndroidApiLevel25AndBelow}}
211211
// 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
212-
val f = java.nio.file.Files.createTempFile("tmp.{{packageName}}", null).toFile()
212+
val tempFile = java.nio.file.Files.createTempFile("tmp.{{packageName}}", null).toFile()
213213
{{/supportAndroidApiLevel25AndBelow}}
214214
{{#supportAndroidApiLevel25AndBelow}}
215-
val f = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
215+
val tempFile = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
216216
java.nio.file.Files.createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null).toFile()
217217
} else {
218218
@Suppress("DEPRECATION")
219219
createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null)
220220
}
221221
{{/supportAndroidApiLevel25AndBelow}}
222-
f.deleteOnExit()
223-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
224-
return f as T
222+
tempFile.deleteOnExit()
223+
body.byteStream().use { inputStream ->
224+
tempFile.outputStream().use { tempFileOutputStream ->
225+
inputStream.copyTo(tempFileOutputStream)
226+
}
227+
}
228+
return tempFile as T
225229
}
226230
val bodyContent = body.string()
227231
if (bodyContent.isEmpty()) {

samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
116116
return null
117117
}
118118
if (T::class.java == File::class.java) {
119-
// return tempfile
119+
// return tempFile
120120
// 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
121-
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
122-
f.deleteOnExit()
123-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
124-
return f as T
121+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
122+
tempFile.deleteOnExit()
123+
body.byteStream().use { inputStream ->
124+
tempFile.outputStream().use { tempFileOutputStream ->
125+
inputStream.copyTo(tempFileOutputStream)
126+
}
127+
}
128+
return tempFile as T
125129
}
126130
val bodyContent = body.string()
127131
if (bodyContent.isEmpty()) {

samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
118118
return null
119119
}
120120
if (T::class.java == File::class.java) {
121-
// return tempfile
121+
// return tempFile
122122
// 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
123-
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
124-
f.deleteOnExit()
125-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
126-
return f as T
123+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
124+
tempFile.deleteOnExit()
125+
body.byteStream().use { inputStream ->
126+
tempFile.outputStream().use { tempFileOutputStream ->
127+
inputStream.copyTo(tempFileOutputStream)
128+
}
129+
}
130+
return tempFile as T
127131
}
128132
val bodyContent = body.string()
129133
if (bodyContent.isEmpty()) {

samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
118118
return null
119119
}
120120
if (T::class.java == File::class.java) {
121-
// return tempfile
121+
// return tempFile
122122
// 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
123-
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
124-
f.deleteOnExit()
125-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
126-
return f as T
123+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
124+
tempFile.deleteOnExit()
125+
body.byteStream().use { inputStream ->
126+
tempFile.outputStream().use { tempFileOutputStream ->
127+
inputStream.copyTo(tempFileOutputStream)
128+
}
129+
}
130+
return tempFile as T
127131
}
128132
val bodyContent = body.string()
129133
if (bodyContent.isEmpty()) {

samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
116116
return null
117117
}
118118
if (T::class.java == File::class.java) {
119-
// return tempfile
119+
// return tempFile
120120
// 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
121-
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
122-
f.deleteOnExit()
123-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
124-
return f as T
121+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
122+
tempFile.deleteOnExit()
123+
body.byteStream().use { inputStream ->
124+
tempFile.outputStream().use { tempFileOutputStream ->
125+
inputStream.copyTo(tempFileOutputStream)
126+
}
127+
}
128+
return tempFile as T
125129
}
126130
val bodyContent = body.string()
127131
if (bodyContent.isEmpty()) {

samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
118118
return null
119119
}
120120
if (T::class.java == File::class.java) {
121-
// return tempfile
121+
// return tempFile
122122
// 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
123-
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
124-
f.deleteOnExit()
125-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
126-
return f as T
123+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
124+
tempFile.deleteOnExit()
125+
body.byteStream().use { inputStream ->
126+
tempFile.outputStream().use { tempFileOutputStream ->
127+
inputStream.copyTo(tempFileOutputStream)
128+
}
129+
}
130+
return tempFile as T
127131
}
128132
val bodyContent = body.string()
129133
if (bodyContent.isEmpty()) {

samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
118118
return null
119119
}
120120
if (T::class.java == File::class.java) {
121-
// return tempfile
121+
// return tempFile
122122
// 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
123-
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
124-
f.deleteOnExit()
125-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
126-
return f as T
123+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
124+
tempFile.deleteOnExit()
125+
body.byteStream().use { inputStream ->
126+
tempFile.outputStream().use { tempFileOutputStream ->
127+
inputStream.copyTo(tempFileOutputStream)
128+
}
129+
}
130+
return tempFile as T
127131
}
128132
val bodyContent = body.string()
129133
if (bodyContent.isEmpty()) {

samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
117117
return null
118118
}
119119
if (T::class.java == File::class.java) {
120-
// return tempfile
120+
// return tempFile
121121
// 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
122-
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
123-
f.deleteOnExit()
124-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
125-
return f as T
122+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
123+
tempFile.deleteOnExit()
124+
body.byteStream().use { inputStream ->
125+
tempFile.outputStream().use { tempFileOutputStream ->
126+
inputStream.copyTo(tempFileOutputStream)
127+
}
128+
}
129+
return tempFile as T
126130
}
127131
val bodyContent = body.string()
128132
if (bodyContent.isEmpty()) {

samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
117117
return null
118118
}
119119
if (T::class.java == File::class.java) {
120-
// return tempfile
120+
// return tempFile
121121
// 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
122-
val f = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
123-
f.deleteOnExit()
124-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
125-
return f as T
122+
val tempFile = java.nio.file.Files.createTempFile("tmp.org.openapitools.client", null).toFile()
123+
tempFile.deleteOnExit()
124+
body.byteStream().use { inputStream ->
125+
tempFile.outputStream().use { tempFileOutputStream ->
126+
inputStream.copyTo(tempFileOutputStream)
127+
}
128+
}
129+
return tempFile as T
126130
}
127131
val bodyContent = body.string()
128132
if (bodyContent.isEmpty()) {

samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,20 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
119119
return null
120120
}
121121
if (T::class.java == File::class.java) {
122-
// return tempfile
123-
val f = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
122+
// return tempFile
123+
val tempFile = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
124124
java.nio.file.Files.createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null).toFile()
125125
} else {
126126
@Suppress("DEPRECATION")
127127
createTempFile("tmp.net.medicineone.teleconsultationandroid.openapi.openapicommon", null)
128128
}
129-
f.deleteOnExit()
130-
body.byteStream().use { java.nio.file.Files.copy(it, f.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) }
131-
return f as T
129+
tempFile.deleteOnExit()
130+
body.byteStream().use { inputStream ->
131+
tempFile.outputStream().use { tempFileOutputStream ->
132+
inputStream.copyTo(tempFileOutputStream)
133+
}
134+
}
135+
return tempFile as T
132136
}
133137
val bodyContent = body.string()
134138
if (bodyContent.isEmpty()) {

0 commit comments

Comments
 (0)