forked from loafle/openapi-generator-original
[BUG][Kotlin-client] Handling default values of parameters (#12255)
* Bugfix Kotlin-client: Handling default values of parameters * Adding object
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package org.openapitools.client.apis
|
||||
|
||||
import org.openapitools.client.infrastructure.CollectionFormats.*
|
||||
import retrofit2.http.*
|
||||
import retrofit2.Call
|
||||
import okhttp3.RequestBody
|
||||
|
||||
|
||||
interface DefaultApi {
|
||||
/**
|
||||
* Tests default values
|
||||
* Tests default values of different parameters
|
||||
* Responses:
|
||||
* - 200: Success
|
||||
*
|
||||
* @param pi0 (default to 10)
|
||||
* @param pi1
|
||||
* @param pn0 (default to 10.0)
|
||||
* @param pn1
|
||||
* @param qi0 (optional, default to 10)
|
||||
* @param qi1 (default to 71)
|
||||
* @param qi2 (optional)
|
||||
* @param qi3
|
||||
* @param qn0 (optional, default to 10.0)
|
||||
* @param qn1 (default to 71.0)
|
||||
* @param qn2 (optional)
|
||||
* @param qn3
|
||||
* @param hi0 (optional, default to 10)
|
||||
* @param hi1 (default to 71)
|
||||
* @param hi2 (optional)
|
||||
* @param hi3
|
||||
* @param hn0 (optional, default to 10.0)
|
||||
* @param hn1 (default to 71.0)
|
||||
* @param hn2 (optional)
|
||||
* @param hn3
|
||||
* @param fi0 (optional, default to 10)
|
||||
* @param fi1 (default to 71)
|
||||
* @param fi2 (optional)
|
||||
* @param fi3
|
||||
* @param fn0 (optional, default to 10.0)
|
||||
* @param fn1 (default to 71.0)
|
||||
* @param fn2 (optional)
|
||||
* @param fn3
|
||||
* @return [Call]<[Unit]>
|
||||
*/
|
||||
@Multipart
|
||||
@POST("test")
|
||||
fun test(@Path("pi0") pi0: kotlin.Int = 10, @Path("pi1") pi1: kotlin.Int, @Path("pn0") pn0: java.math.BigDecimal = java.math.BigDecimal("10.0"), @Path("pn1") pn1: java.math.BigDecimal, @Query("qi0") qi0: kotlin.Int? = 10, @Query("qi1") qi1: kotlin.Int = 71, @Query("qi2") qi2: kotlin.Int? = null, @Query("qi3") qi3: kotlin.Int, @Query("qn0") qn0: java.math.BigDecimal? = java.math.BigDecimal("10.0"), @Query("qn1") qn1: java.math.BigDecimal = java.math.BigDecimal("71.0"), @Query("qn2") qn2: java.math.BigDecimal? = null, @Query("qn3") qn3: java.math.BigDecimal, @Header("hi0") hi0: kotlin.Int? = 10, @Header("hi1") hi1: kotlin.Int = 71, @Header("hi2") hi2: kotlin.Int? = null, @Header("hi3") hi3: kotlin.Int, @Header("hn0") hn0: java.math.BigDecimal? = java.math.BigDecimal("10.0"), @Header("hn1") hn1: java.math.BigDecimal = java.math.BigDecimal("71.0"), @Header("hn2") hn2: java.math.BigDecimal? = null, @Header("hn3") hn3: java.math.BigDecimal, @Part("fi0") fi0: kotlin.Int? = 10, @Part("fi1") fi1: kotlin.Int = 71, @Part("fi2") fi2: kotlin.Int? = null, @Part("fi3") fi3: kotlin.Int, @Part("fn0") fn0: java.math.BigDecimal? = java.math.BigDecimal("10.0"), @Part("fn1") fn1: java.math.BigDecimal = java.math.BigDecimal("71.0"), @Part("fn2") fn2: java.math.BigDecimal? = null, @Part("fn3") fn3: java.math.BigDecimal): Call<Unit>
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
|
||||
import okhttp3.Call
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Retrofit
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Converter
|
||||
import retrofit2.converter.scalars.ScalarsConverterFactory
|
||||
import com.squareup.moshi.Moshi
|
||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||
|
||||
|
||||
class ApiClient(
|
||||
private var baseUrl: String = defaultBasePath,
|
||||
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
|
||||
private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder,
|
||||
private val callFactory : Call.Factory? = null,
|
||||
private val converterFactory: Converter.Factory? = null,
|
||||
) {
|
||||
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
|
||||
var logger: ((String) -> Unit)? = null
|
||||
|
||||
private val retrofitBuilder: Retrofit.Builder by lazy {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.addConverterFactory(MoshiConverterFactory.create(serializerBuilder.build()))
|
||||
.apply {
|
||||
if (converterFactory != null) {
|
||||
addConverterFactory(converterFactory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val clientBuilder: OkHttpClient.Builder by lazy {
|
||||
okHttpClientBuilder ?: defaultClientBuilder
|
||||
}
|
||||
|
||||
private val defaultClientBuilder: OkHttpClient.Builder by lazy {
|
||||
OkHttpClient()
|
||||
.newBuilder()
|
||||
.addInterceptor(HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
|
||||
override fun log(message: String) {
|
||||
logger?.invoke(message)
|
||||
}
|
||||
}).apply {
|
||||
level = HttpLoggingInterceptor.Level.BODY
|
||||
})
|
||||
}
|
||||
|
||||
init {
|
||||
normalizeBaseUrl()
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an authorization to be used by the client
|
||||
* @param authName Authentication name
|
||||
* @param authorization Authorization interceptor
|
||||
* @return ApiClient
|
||||
*/
|
||||
fun addAuthorization(authName: String, authorization: Interceptor): ApiClient {
|
||||
if (apiAuthorizations.containsKey(authName)) {
|
||||
throw RuntimeException("auth name $authName already in api authorizations")
|
||||
}
|
||||
apiAuthorizations[authName] = authorization
|
||||
clientBuilder.addInterceptor(authorization)
|
||||
return this
|
||||
}
|
||||
|
||||
fun setLogger(logger: (String) -> Unit): ApiClient {
|
||||
this.logger = logger
|
||||
return this
|
||||
}
|
||||
|
||||
fun <S> createService(serviceClass: Class<S>): S {
|
||||
val usedCallFactory = this.callFactory ?: clientBuilder.build()
|
||||
return retrofitBuilder.callFactory(usedCallFactory).build().create(serviceClass)
|
||||
}
|
||||
|
||||
private fun normalizeBaseUrl() {
|
||||
if (!baseUrl.endsWith("/")) {
|
||||
baseUrl += "/"
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun <T, reified U> Iterable<T>.runOnFirst(callback: U.() -> Unit) {
|
||||
for (element in this) {
|
||||
if (element is U) {
|
||||
callback.invoke(element)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
protected val baseUrlKey = "org.openapitools.client.baseUrl"
|
||||
|
||||
@JvmStatic
|
||||
val defaultBasePath: String by lazy {
|
||||
System.getProperties().getProperty(baseUrlKey, "http://localhost")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.math.BigDecimal
|
||||
|
||||
class BigDecimalAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: BigDecimal): String {
|
||||
return value.toPlainString()
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): BigDecimal {
|
||||
return BigDecimal(value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.math.BigInteger
|
||||
|
||||
class BigIntegerAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: BigInteger): String {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): BigInteger {
|
||||
return BigInteger(value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
|
||||
class ByteArrayAdapter {
|
||||
@ToJson
|
||||
fun toJson(data: ByteArray): String = String(data)
|
||||
|
||||
@FromJson
|
||||
fun fromJson(data: String): ByteArray = data.toByteArray()
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
class CollectionFormats {
|
||||
|
||||
open class CSVParams {
|
||||
|
||||
var params: List<String>
|
||||
|
||||
constructor(params: List<String>) {
|
||||
this.params = params
|
||||
}
|
||||
|
||||
constructor(vararg params: String) {
|
||||
this.params = listOf(*params)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return params.joinToString(",")
|
||||
}
|
||||
}
|
||||
|
||||
open class SSVParams : CSVParams {
|
||||
|
||||
constructor(params: List<String>) : super(params)
|
||||
|
||||
constructor(vararg params: String) : super(*params)
|
||||
|
||||
override fun toString(): String {
|
||||
return params.joinToString(" ")
|
||||
}
|
||||
}
|
||||
|
||||
class TSVParams : CSVParams {
|
||||
|
||||
constructor(params: List<String>) : super(params)
|
||||
|
||||
constructor(vararg params: String) : super(*params)
|
||||
|
||||
override fun toString(): String {
|
||||
return params.joinToString("\t")
|
||||
}
|
||||
}
|
||||
|
||||
class PIPESParams : CSVParams {
|
||||
|
||||
constructor(params: List<String>) : super(params)
|
||||
|
||||
constructor(vararg params: String) : super(*params)
|
||||
|
||||
override fun toString(): String {
|
||||
return params.joinToString("|")
|
||||
}
|
||||
}
|
||||
|
||||
class SPACEParams : SSVParams()
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.time.LocalDate
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
class LocalDateAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: LocalDate): String {
|
||||
return DateTimeFormatter.ISO_LOCAL_DATE.format(value)
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): LocalDate {
|
||||
return LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
class LocalDateTimeAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: LocalDateTime): String {
|
||||
return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value)
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): LocalDateTime {
|
||||
return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.JsonDataException
|
||||
import com.squareup.moshi.Moshi
|
||||
import retrofit2.Response
|
||||
|
||||
@Throws(JsonDataException::class)
|
||||
inline fun <reified T> Response<*>.getErrorResponse(serializerBuilder: Moshi.Builder = Serializer.moshiBuilder): T? {
|
||||
val serializer = serializerBuilder.build()
|
||||
val parser = serializer.adapter(T::class.java)
|
||||
val response = errorBody()?.string()
|
||||
if (response != null) {
|
||||
return parser.fromJson(response)
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
|
||||
object Serializer {
|
||||
@JvmStatic
|
||||
val moshiBuilder: Moshi.Builder = Moshi.Builder()
|
||||
.add(OffsetDateTimeAdapter())
|
||||
.add(LocalDateTimeAdapter())
|
||||
.add(LocalDateAdapter())
|
||||
.add(UUIDAdapter())
|
||||
.add(ByteArrayAdapter())
|
||||
.add(URIAdapter())
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.add(BigDecimalAdapter())
|
||||
.add(BigIntegerAdapter())
|
||||
|
||||
@JvmStatic
|
||||
val moshi: Moshi by lazy {
|
||||
moshiBuilder.build()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.net.URI
|
||||
|
||||
class URIAdapter {
|
||||
@ToJson
|
||||
fun toJson(uri: URI) = uri.toString()
|
||||
|
||||
@FromJson
|
||||
fun fromJson(s: String): URI = URI.create(s)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.util.UUID
|
||||
|
||||
class UUIDAdapter {
|
||||
@ToJson
|
||||
fun toJson(uuid: UUID) = uuid.toString()
|
||||
|
||||
@FromJson
|
||||
fun fromJson(s: String): UUID = UUID.fromString(s)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Demo
|
||||
*
|
||||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* Please note:
|
||||
* This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* Do not edit this file manually.
|
||||
*/
|
||||
|
||||
@file:Suppress(
|
||||
"ArrayInDataClass",
|
||||
"EnumEntryName",
|
||||
"RemoveRedundantQualifierName",
|
||||
"UnusedImport"
|
||||
)
|
||||
|
||||
package org.openapitools.client.models
|
||||
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param i0
|
||||
* @param n0
|
||||
* @param i1
|
||||
* @param n1
|
||||
*/
|
||||
|
||||
data class Apa (
|
||||
|
||||
@Json(name = "i0")
|
||||
val i0: kotlin.Int,
|
||||
|
||||
@Json(name = "n0")
|
||||
val n0: java.math.BigDecimal,
|
||||
|
||||
@Json(name = "i1")
|
||||
val i1: kotlin.Int? = null,
|
||||
|
||||
@Json(name = "n1")
|
||||
val n1: java.math.BigDecimal? = null
|
||||
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user