Skip to content

[kotlin][client] support text/plain in okhttp #20250

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

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,9 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
).trim();
return "multipart/form-data".equals(mediaType)
|| "application/x-www-form-urlencoded".equals(mediaType)
|| (mediaType.startsWith("application/") && (mediaType.endsWith("json") || mediaType.endsWith("octet-stream")));
|| (mediaType.startsWith("application/") && mediaType.endsWith("json"))
|| "application/octet-stream".equals(mediaType)
|| "text/plain".equals(mediaType);
};
operation.consumes = operation.consumes == null ? null : operation.consumes.stream()
.filter(isSerializable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import com.squareup.moshi.adapter
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
protected const val XmlMediaType: String = "application/xml"
protected const val OctetMediaType: String = "application/octet-stream"
protected const val TextMediaType: String = "text/plain"

{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKey: MutableMap<String, String> = mutableMapOf()
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -209,10 +210,10 @@ import com.squareup.moshi.adapter
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
Comment on lines -212 to +214
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Djaler did you deleted OctetMediaType by accident or on propose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On purpose. See message for this commit c5836ff

// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}

{{#moshi}}
Expand Down Expand Up @@ -298,7 +299,8 @@ import com.squareup.moshi.adapter
}}{{#kotlinx_serialization}}Serializer.kotlinxSerializationJson.decodeFromString<T>(bodyContent){{/kotlinx_serialization}}
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
protected const val XmlMediaType: String = "application/xml"
protected const val OctetMediaType: String = "application/octet-stream"
protected const val TextMediaType: String = "text/plain"

val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}

@OptIn(ExperimentalStdlibApi::class)
Expand Down Expand Up @@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
protected const val XmlMediaType: String = "application/xml"
protected const val OctetMediaType: String = "application/octet-stream"
protected const val TextMediaType: String = "text/plain"

val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}

@OptIn(ExperimentalStdlibApi::class)
Expand Down Expand Up @@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
protected const val XmlMediaType: String = "application/xml"
protected const val OctetMediaType: String = "application/octet-stream"
protected const val TextMediaType: String = "text/plain"

val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}

@OptIn(ExperimentalStdlibApi::class)
Expand Down Expand Up @@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
protected const val XmlMediaType: String = "application/xml"
protected const val OctetMediaType: String = "application/octet-stream"
protected const val TextMediaType: String = "text/plain"

val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}

@OptIn(ExperimentalStdlibApi::class)
Expand Down Expand Up @@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
protected const val XmlMediaType: String = "application/xml"
protected const val OctetMediaType: String = "application/octet-stream"
protected const val TextMediaType: String = "text/plain"

val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}

@OptIn(ExperimentalStdlibApi::class)
Expand Down Expand Up @@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
protected const val XmlMediaType: String = "application/xml"
protected const val OctetMediaType: String = "application/octet-stream"
protected const val TextMediaType: String = "text/plain"

val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}

@OptIn(ExperimentalStdlibApi::class)
Expand Down Expand Up @@ -243,7 +244,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
protected const val XmlMediaType: String = "application/xml"
protected const val OctetMediaType: String = "application/octet-stream"
protected const val TextMediaType: String = "text/plain"

public val apiKey: MutableMap<String, String> = mutableMapOf()
public val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -169,10 +170,10 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}

@OptIn(ExperimentalStdlibApi::class)
Expand Down Expand Up @@ -243,7 +244,8 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
Serializer.moshi.adapter<T>().fromJson(bodyContent)
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
protected const val XmlMediaType: String = "application/xml"
protected const val OctetMediaType: String = "application/octet-stream"
protected const val TextMediaType: String = "text/plain"

val apiKey: MutableMap<String, String> = mutableMapOf()
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
Expand Down Expand Up @@ -169,10 +170,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
.toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull())
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
mediaType == OctetMediaType && content is ByteArray ->
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
mediaType == TextMediaType && content is String ->
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
}

protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JsonMediaType): T? {
Expand Down Expand Up @@ -242,7 +243,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType())
}
mediaType == OctetMediaType -> body.bytes() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
mediaType == TextMediaType -> body.string() as? T
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
}
}

Expand Down
Loading
Loading