fix kotlin client uppercase enum script

This commit is contained in:
William Cheng 2019-12-28 08:59:29 +08:00
parent 54cfab84b6
commit 3100f8c4e4
12 changed files with 63 additions and 48 deletions

View File

@ -31,4 +31,4 @@ ags="generate -t modules/openapi-generator/src/main/resources/kotlin-client -i m
java ${JAVA_OPTS} -jar ${executable} ${ags} java ${JAVA_OPTS} -jar ${executable} ${ags}
cp CI/samples.ci/client/petstore/kotlin-uppercase-enum/pom.xml samples/client/petstore/kotlin-uppercase-enum/pom.xml #cp CI/samples.ci/client/petstore/kotlin-uppercase-enum/pom.xml samples/client/petstore/kotlin-uppercase-enum/pom.xml

View File

@ -1 +1 @@
4.2.0-SNAPSHOT 4.2.3-SNAPSHOT

View File

@ -7,7 +7,7 @@ wrapper {
} }
buildscript { buildscript {
ext.kotlin_version = '1.3.41' ext.kotlin_version = '1.3.61'
repositories { repositories {
mavenCentral() mavenCentral()
@ -30,8 +30,8 @@ test {
dependencies { dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "com.squareup.moshi:moshi-kotlin:1.8.0" compile "com.squareup.moshi:moshi-kotlin:1.9.2"
compile "com.squareup.moshi:moshi-adapters:1.8.0" compile "com.squareup.moshi:moshi-adapters:1.9.2"
compile "com.squareup.okhttp3:okhttp:4.0.1" compile "com.squareup.okhttp3:okhttp:4.2.2"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.1.0" testCompile "io.kotlintest:kotlintest-runner-junit5:3.1.0"
} }

View File

@ -31,11 +31,15 @@ class EnumApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
* Get enums * Get enums
* *
* @return PetEnum * @return PetEnum
* @throws UnsupportedOperationException If the API returns an informational or redirection response
* @throws ClientException If the API returns a client error response
* @throws ServerException If the API returns a server error response
*/ */
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
fun getEnum() : PetEnum { fun getEnum() : PetEnum {
val localVariableBody: kotlin.Any? = null val localVariableBody: kotlin.Any? = null
val localVariableQuery: MultiValueMap = mapOf() val localVariableQuery: MultiValueMap = mutableMapOf()
val localVariableHeaders: MutableMap<String, String> = mutableMapOf() val localVariableHeaders: MutableMap<String, String> = mutableMapOf()
val localVariableConfig = RequestConfig( val localVariableConfig = RequestConfig(
RequestMethod.GET, RequestMethod.GET,
@ -43,17 +47,23 @@ class EnumApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
query = localVariableQuery, query = localVariableQuery,
headers = localVariableHeaders headers = localVariableHeaders
) )
val response = request<PetEnum>( val localVarResponse = request<PetEnum>(
localVariableConfig, localVariableConfig,
localVariableBody localVariableBody
) )
return when (response.responseType) { return when (localVarResponse.responseType) {
ResponseType.Success -> (response as Success<*>).data as PetEnum ResponseType.Success -> (localVarResponse as Success<*>).data as PetEnum
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.") ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.") ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error") ResponseType.ClientError -> {
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error") val localVarError = localVarResponse as ClientError<*>
throw ClientException(localVarError.body as? String ?: "Client error", localVarError.statusCode)
}
ResponseType.ServerError -> {
val localVarError = localVarResponse as ServerError<*>
throw ServerException(localVarError.message ?: "Server error", localVarError.statusCode)
}
} }
} }

View File

@ -1,12 +1,12 @@
package org.openapitools.client.infrastructure package org.openapitools.client.infrastructure
typealias MultiValueMap = Map<String,List<String>> typealias MultiValueMap = MutableMap<String,List<String>>
fun collectionDelimiter(collectionFormat: String) = when(collectionFormat) { fun collectionDelimiter(collectionFormat: String) = when(collectionFormat) {
"csv" -> "," "csv" -> ","
"tsv" -> "\t" "tsv" -> "\t"
"pipes" -> "|" "pipe" -> "|"
"ssv" -> " " "space" -> " "
else -> "" else -> ""
} }

View File

@ -1,5 +1,6 @@
package org.openapitools.client.infrastructure package org.openapitools.client.infrastructure
import okhttp3.Credentials
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.RequestBody import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody import okhttp3.RequestBody.Companion.asRequestBody
@ -107,7 +108,7 @@ open class ApiClient(val baseUrl: String) {
val contentType = (headers[ContentType] as String).substringBefore(";").toLowerCase() val contentType = (headers[ContentType] as String).substringBefore(";").toLowerCase()
val request = when (requestConfig.method) { val request = when (requestConfig.method) {
RequestMethod.DELETE -> Request.Builder().url(url).delete() RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(body, contentType))
RequestMethod.GET -> Request.Builder().url(url) RequestMethod.GET -> Request.Builder().url(url)
RequestMethod.HEAD -> Request.Builder().url(url).head() RequestMethod.HEAD -> Request.Builder().url(url).head()
RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(body, contentType)) RequestMethod.PATCH -> Request.Builder().url(url).patch(requestBody(body, contentType))
@ -150,4 +151,8 @@ open class ApiClient(val baseUrl: String) {
) )
} }
} }
protected inline fun <reified T: Any> parseDateToQueryString(value : T): String {
return value.toString()
}
} }

View File

@ -9,4 +9,4 @@ class ByteArrayAdapter {
@FromJson @FromJson
fun fromJson(data: String): ByteArray = data.toByteArray() fun fromJson(data: String): ByteArray = data.toByteArray()
} }

View File

@ -3,38 +3,14 @@ package org.openapitools.client.infrastructure
import java.lang.RuntimeException import java.lang.RuntimeException
open class ClientException : RuntimeException { open class ClientException(message: kotlin.String? = null, val statusCode: Int = -1) : RuntimeException(message) {
/**
* Constructs an [ClientException] with no detail message.
*/
constructor() : super()
/**
* Constructs an [ClientException] with the specified detail message.
* @param message the detail message.
*/
constructor(message: kotlin.String) : super(message)
companion object { companion object {
private const val serialVersionUID: Long = 123L private const val serialVersionUID: Long = 123L
} }
} }
open class ServerException : RuntimeException { open class ServerException(message: kotlin.String? = null, val statusCode: Int = -1) : RuntimeException(message) {
/**
* Constructs an [ServerException] with no detail message.
*/
constructor() : super()
/**
* Constructs an [ServerException] with the specified detail message.
* @param message the detail message.
*/
constructor(message: kotlin.String) : super(message)
companion object { companion object {
private const val serialVersionUID: Long = 456L private const val serialVersionUID: Long = 456L

View File

@ -0,0 +1,19 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
class OffsetDateTimeAdapter {
@ToJson
fun toJson(value: OffsetDateTime): String {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value)
}
@FromJson
fun fromJson(value: String): OffsetDateTime {
return OffsetDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
}
}

View File

@ -12,5 +12,5 @@ data class RequestConfig(
val method: RequestMethod, val method: RequestMethod,
val path: String, val path: String,
val headers: MutableMap<String, String> = mutableMapOf(), val headers: MutableMap<String, String> = mutableMapOf(),
val query: Map<String, List<String>> = mapOf() val query: MutableMap<String, List<String>> = mutableMapOf()
) )

View File

@ -20,4 +20,4 @@ val Response.isClientError : Boolean get() = this.code in 400..499
/** /**
* Provides an extension to evaluation whether the response is a 5xx (Standard) through 999 (non-standard) code * Provides an extension to evaluation whether the response is a 5xx (Standard) through 999 (non-standard) code
*/ */
val Response.isServerError : Boolean get() = this.code in 500..999 val Response.isServerError : Boolean get() = this.code in 500..999

View File

@ -7,12 +7,17 @@ import java.util.Date
object Serializer { object Serializer {
@JvmStatic @JvmStatic
val moshi: Moshi = Moshi.Builder() val moshiBuilder: Moshi.Builder = Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
.add(OffsetDateTimeAdapter())
.add(LocalDateTimeAdapter()) .add(LocalDateTimeAdapter())
.add(LocalDateAdapter()) .add(LocalDateAdapter())
.add(UUIDAdapter()) .add(UUIDAdapter())
.add(ByteArrayAdapter()) .add(ByteArrayAdapter())
.add(KotlinJsonAdapterFactory()) .add(KotlinJsonAdapterFactory())
.build()
@JvmStatic
val moshi: Moshi by lazy {
moshiBuilder.build()
}
} }