[kotlin] Map file and binary to ByteArray (#19840)

* [kotlin] Map file and binary to ByteArray

* [kotlin] Map file and binary to ByteArray
This commit is contained in:
Bruno Coelho 2024-10-11 08:59:16 +01:00 committed by GitHub
parent 185c0639c0
commit 462f450366
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
26 changed files with 358 additions and 5 deletions

View File

@ -5,6 +5,7 @@ templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-petstore-string
serializableModel: "true"
mapFileBinaryToByteArray: "true"
sortModelPropertiesByRequiredFlag: "false"
sortParamsByRequiredFlag: "false"
dateLibrary: string

View File

@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|idea|Add IntellJ Idea plugin and mark Kotlin main and test folders as source folders.| |false|
|library|Library template (sub-template) to use|<dl><dt>**jvm-ktor**</dt><dd>Platform: Java Virtual Machine. HTTP client: Ktor 1.6.7. JSON processing: Gson, Jackson (default).</dd><dt>**jvm-okhttp4**</dt><dd>[DEFAULT] Platform: Java Virtual Machine. HTTP client: OkHttp 4.2.0 (Android 5.0+ and Java 8+). JSON processing: Moshi 1.8.0.</dd><dt>**jvm-spring-webclient**</dt><dd>Platform: Java Virtual Machine. HTTP: Spring 5 (or 6 with useSpringBoot3 enabled) WebClient. JSON processing: Jackson.</dd><dt>**jvm-spring-restclient**</dt><dd>Platform: Java Virtual Machine. HTTP: Spring 6 RestClient. JSON processing: Jackson.</dd><dt>**jvm-retrofit2**</dt><dd>Platform: Java Virtual Machine. HTTP client: Retrofit 2.6.2.</dd><dt>**multiplatform**</dt><dd>Platform: Kotlin multiplatform. HTTP client: Ktor 1.6.7. JSON processing: Kotlinx Serialization: 1.2.1.</dd><dt>**jvm-volley**</dt><dd>Platform: JVM for Android. HTTP client: Volley 1.2.1. JSON processing: gson 2.8.9</dd><dt>**jvm-vertx**</dt><dd>Platform: Java Virtual Machine. HTTP client: Vert.x Web Client. JSON processing: Moshi, Gson or Jackson.</dd></dl>|jvm-okhttp4|
|mapFileBinaryToByteArray|Map File and Binary to ByteArray (default: false)| |false|
|modelMutable|Create mutable models| |false|
|moshiCodeGen|Whether to enable codegen with the Moshi library. Refer to the [official Moshi doc](https://github.com/square/moshi#codegen) for more info.| |false|
|nullableReturnType|Nullable return type| |false|

View File

@ -93,10 +93,13 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
public static final String SUPPORT_ANDROID_API_LEVEL_25_AND_BELLOW = "supportAndroidApiLevel25AndBelow";
public static final String MAP_FILE_BINARY_TO_BYTE_ARRAY = "mapFileBinaryToByteArray";
public static final String GENERATE_ONEOF_ANYOF_WRAPPERS = "generateOneOfAnyOfWrappers";
protected static final String VENDOR_EXTENSION_BASE_NAME_LITERAL = "x-base-name-literal";
@Setter protected String dateLibrary = DateLibrary.JAVA8.value;
@Setter protected String requestDateConverter = RequestDateConverter.TO_JSON.value;
@Setter protected String collectionType = CollectionType.LIST.value;
@ -108,6 +111,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
protected boolean generateRoomModels = false;
@Setter protected String roomModelPackage = "";
@Setter protected boolean omitGradleWrapper = false;
@Setter protected boolean mapFileBinaryToByteArray = false;
@Setter protected boolean generateOneOfAnyOfWrappers = true;
@Getter @Setter protected boolean failOnUnknownProperties = false;
@ -269,6 +273,8 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
cliOptions.add(CliOption.newBoolean(SUPPORT_ANDROID_API_LEVEL_25_AND_BELLOW, "[WARNING] This flag will generate code that has a known security vulnerability. It uses `kotlin.io.createTempFile` instead of `java.nio.file.Files.createTempFile` in order 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"));
cliOptions.add(new CliOption(MAP_FILE_BINARY_TO_BYTE_ARRAY, "Map File and Binary to ByteArray (default: false)").defaultValue(Boolean.FALSE.toString()));
cliOptions.add(CliOption.newBoolean(GENERATE_ONEOF_ANYOF_WRAPPERS, "Generate oneOf, anyOf schemas as wrappers."));
CliOption serializationLibraryOpt = new CliOption(CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_DESC);
@ -437,6 +443,15 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
additionalProperties.put(this.serializationLibrary.name(), true);
}
if (additionalProperties.containsKey(MAP_FILE_BINARY_TO_BYTE_ARRAY)) {
setMapFileBinaryToByteArray(convertPropertyToBooleanAndWriteBack(MAP_FILE_BINARY_TO_BYTE_ARRAY));
}
additionalProperties.put(MAP_FILE_BINARY_TO_BYTE_ARRAY, mapFileBinaryToByteArray);
if (mapFileBinaryToByteArray) {
typeMapping.put("file", "kotlin.ByteArray");
typeMapping.put("binary", "kotlin.ByteArray");
}
if (additionalProperties.containsKey(GENERATE_ONEOF_ANYOF_WRAPPERS)) {
setGenerateOneOfAnyOfWrappers(Boolean.parseBoolean(additionalProperties.get(GENERATE_ONEOF_ANYOF_WRAPPERS).toString()));
}

View File

@ -87,6 +87,21 @@ import com.squareup.moshi.adapter
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -100,6 +115,7 @@ import com.squareup.moshi.adapter
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -60,6 +60,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -73,6 +88,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -61,6 +61,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -74,6 +89,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -360,7 +360,7 @@ uploads an image
val apiInstance = PetApi()
val petId : kotlin.Long = 789 // kotlin.Long | ID of pet to update
val additionalMetadata : kotlin.String = additionalMetadata_example // kotlin.String | Additional data to pass to server
val file : java.io.File = BINARY_DATA_HERE // java.io.File | file to upload
val file : kotlin.ByteArray = BINARY_DATA_HERE // kotlin.ByteArray | file to upload
try {
val result : ModelApiResponse = apiInstance.uploadFile(petId, additionalMetadata, file)
println(result)
@ -378,7 +378,7 @@ try {
| **additionalMetadata** | **kotlin.String**| Additional data to pass to server | [optional] |
| Name | Type | Description | Notes |
| ------------- | ------------- | ------------- | ------------- |
| **file** | **java.io.File**| file to upload | [optional] |
| **file** | **kotlin.ByteArray**| file to upload | [optional] |
### Return type

View File

@ -589,7 +589,7 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factory = A
*/
@Suppress("UNCHECKED_CAST")
@Throws(IllegalStateException::class, IOException::class, UnsupportedOperationException::class, ClientException::class, ServerException::class)
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: java.io.File? = null) : ModelApiResponse {
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: kotlin.ByteArray? = null) : ModelApiResponse {
val localVarResponse = uploadFileWithHttpInfo(petId = petId, additionalMetadata = additionalMetadata, file = file)
return when (localVarResponse.responseType) {
@ -619,7 +619,7 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factory = A
*/
@Suppress("UNCHECKED_CAST")
@Throws(IllegalStateException::class, IOException::class)
fun uploadFileWithHttpInfo(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : ApiResponse<ModelApiResponse?> {
fun uploadFileWithHttpInfo(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: kotlin.ByteArray?) : ApiResponse<ModelApiResponse?> {
val localVariableConfig = uploadFileRequestConfig(petId = petId, additionalMetadata = additionalMetadata, file = file)
return request<Map<String, PartConfig<*>>, ModelApiResponse>(
@ -635,7 +635,7 @@ class PetApi(basePath: kotlin.String = defaultBasePath, client: Call.Factory = A
* @param file file to upload (optional)
* @return RequestConfig
*/
fun uploadFileRequestConfig(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : RequestConfig<Map<String, PartConfig<*>>> {
fun uploadFileRequestConfig(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: kotlin.ByteArray?) : RequestConfig<Map<String, PartConfig<*>>> {
val localVariableBody = mapOf(
"additionalMetadata" to PartConfig(body = additionalMetadata, headers = mutableMapOf()),
"file" to PartConfig(body = file, headers = mutableMapOf()),)

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -59,6 +59,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -72,6 +87,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()

View File

@ -58,6 +58,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}
/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
* @param byteArray The given file
* @return The guessed Content-Type
*/
protected fun guessContentTypeFromByteArray(byteArray: ByteArray): String {
val contentType = try {
URLConnection.guessContentTypeFromStream(byteArray.inputStream())
} catch (io: IOException) {
"application/octet-stream"
}
return contentType
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
@ -71,6 +86,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected inline fun <reified T> requestBody(content: T, mediaType: String?): RequestBody =
when {
content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull())
content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull())
mediaType == FormDataMediaType ->
MultipartBody.Builder()