[kotlin-client] Allowing vendor types for json (#10758)

* Using the first serializable 'consumes' mediaType
Using all deserializable 'produces' mediaTypes
Matching json vendor types as json

* updating the generated samples
This commit is contained in:
Guus Bloemsma
2021-12-11 14:48:00 +01:00
committed by GitHub
parent cee5f75912
commit 0236f84c11
84 changed files with 483 additions and 316 deletions

View File

@@ -98,6 +98,7 @@ class DefaultApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath
val localVariableBody = null
val localVariableQuery: MultiValueMap = mutableMapOf()
val localVariableHeaders: MutableMap<String, String> = mutableMapOf()
localVariableHeaders["Accept"] = "application/json"
return RequestConfig(
method = RequestMethod.GET,

View File

@@ -105,7 +105,7 @@ open class ApiClient(val baseUrl: String) {
}
}.build()
}
mediaType == JsonMediaType -> {
mediaType.startsWith("application/") && mediaType.endsWith("json") ->
if (content == null) {
EMPTY_REQUEST
} else {
@@ -114,7 +114,6 @@ open class ApiClient(val baseUrl: String) {
mediaType.toMediaTypeOrNull()
)
}
}
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
// TODO: this should be extended with other serializers
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.")
@@ -138,8 +137,9 @@ open class ApiClient(val baseUrl: String) {
out.close()
return f as T
}
return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter<T>().fromJson(bodyContent)
return when {
mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) ->
Serializer.moshi.adapter<T>().fromJson(bodyContent)
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}
@@ -167,11 +167,11 @@ open class ApiClient(val baseUrl: String) {
}
val headers = requestConfig.headers
if(headers[ContentType] ?: "" == "") {
if(headers[ContentType].isNullOrEmpty()) {
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
}
if(headers[Accept] ?: "" == "") {
if(headers[Accept].isNullOrEmpty()) {
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
}