mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-02 21:50:55 +00:00
[Kotlin] update ApiClient to register all adapters for GsonBuilder (#18965)
* update api client to register type adapter * update samples * remove json array variable name
This commit is contained in:
parent
cf303d4e03
commit
7747cc93e0
@ -163,6 +163,7 @@ import {{packageName}}.infrastructure.ITransformForStorage
|
||||
{{/vars}}
|
||||
{{/hasEnums}}
|
||||
{{#generateOneOfAnyOfWrappers}}
|
||||
{{#gson}}
|
||||
|
||||
class CustomTypeAdapterFactory : TypeAdapterFactory {
|
||||
override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
|
||||
@ -249,24 +250,22 @@ import {{packageName}}.infrastructure.ITransformForStorage
|
||||
throw IllegalArgumentException(String.format("Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj["{{{baseName}}}"].toString()))
|
||||
}
|
||||
|
||||
val jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}")
|
||||
// validate the required field `{{{baseName}}}` (array)
|
||||
for (i in 0 until jsonArray{{name}}.size()) {
|
||||
{{{items.dataType}}}.validateJsonElement(jsonArray{{name}}.get(i))
|
||||
for (i in 0 until jsonObj.getAsJsonArray("{{{baseName}}}").size()) {
|
||||
{{{items.dataType}}}.validateJsonElement(jsonObj.getAsJsonArray("{{{baseName}}}").get(i))
|
||||
}
|
||||
{{/required}}
|
||||
{{^required}}
|
||||
if (jsonObj["{{{baseName}}}"] != null && !jsonObj["{{{baseName}}}"].isJsonNull) {
|
||||
val jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}")
|
||||
if (jsonArray{{name}} != null) {
|
||||
if (jsonObj.getAsJsonArray("{{{baseName}}}") != null) {
|
||||
// ensure the json data is an array
|
||||
require(jsonObj["{{{baseName}}}"].isJsonArray) {
|
||||
String.format("Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj["{{{baseName}}}"].toString())
|
||||
}
|
||||
|
||||
// validate the optional field `{{{baseName}}}` (array)
|
||||
for (i in 0 until jsonArray{{name}}.size()) {
|
||||
{{{items.dataType}}}.validateJsonElement(jsonArray{{name}}[i])
|
||||
for (i in 0 until jsonObj.getAsJsonArray("{{{baseName}}}").size()) {
|
||||
{{{items.dataType}}}.validateJsonElement(jsonObj.getAsJsonArray("{{{baseName}}}").get(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -356,6 +355,7 @@ import {{packageName}}.infrastructure.ITransformForStorage
|
||||
{{/discriminator}}
|
||||
}
|
||||
}
|
||||
{{/gson}}
|
||||
{{/generateOneOfAnyOfWrappers}}
|
||||
|
||||
{{#vendorExtensions.x-has-data-class-body}}
|
||||
|
@ -64,7 +64,7 @@ import okhttp3.MediaType.Companion.toMediaType
|
||||
private var baseUrl: String = defaultBasePath,
|
||||
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
|
||||
{{^kotlinx_serialization}}
|
||||
private val serializerBuilder: {{#gson}}GsonBuilder{{/gson}}{{#moshi}}Moshi.Builder{{/moshi}}{{#jackson}}ObjectMapper{{/jackson}} = Serializer.{{#gson}}gsonBuilder{{/gson}}{{#moshi}}moshiBuilder{{/moshi}}{{#jackson}}jacksonObjectMapper{{/jackson}},
|
||||
private val serializerBuilder: {{#gson}}GsonBuilder{{/gson}}{{#moshi}}Moshi.Builder{{/moshi}}{{#jackson}}ObjectMapper{{/jackson}} = {{#generateOneOfAnyOfWrappers}}{{#gson}}registerTypeAdapterFactoryForAllModels({{/gson}}{{/generateOneOfAnyOfWrappers}}Serializer.{{#gson}}gsonBuilder{{/gson}}{{#generateOneOfAnyOfWrappers}}{{#gson}}){{/gson}}{{/generateOneOfAnyOfWrappers}}{{#moshi}}moshiBuilder{{/moshi}}{{#jackson}}jacksonObjectMapper{{/jackson}},
|
||||
{{/kotlinx_serialization}}
|
||||
private val callFactory: Call.Factory? = null,
|
||||
private val callAdapterFactories: List<CallAdapter.Factory> = listOf(
|
||||
@ -177,21 +177,6 @@ import okhttp3.MediaType.Companion.toMediaType
|
||||
password: String
|
||||
) : this(baseUrl, okHttpClientBuilder, {{^kotlinx_serialization}}serializerBuilder, {{/kotlinx_serialization}}arrayOf(authName)) {
|
||||
setCredentials(username, password)
|
||||
{{#generateOneOfAnyOfWrappers}}
|
||||
{{^kotlinx_serialization}}
|
||||
{{#gson}}
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{^isEnum}}
|
||||
{{^hasChildren}}
|
||||
serializerBuilder.registerTypeAdapterFactory({{modelPackage}}.{{{classname}}}.CustomTypeAdapterFactory())
|
||||
{{/hasChildren}}
|
||||
{{/isEnum}}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
{{/gson}}
|
||||
{{/kotlinx_serialization}}
|
||||
{{/generateOneOfAnyOfWrappers}}
|
||||
}
|
||||
|
||||
{{/isBasicBasic}}
|
||||
@ -204,21 +189,6 @@ import okhttp3.MediaType.Companion.toMediaType
|
||||
bearerToken: String
|
||||
) : this(baseUrl, okHttpClientBuilder, {{^kotlinx_serialization}}serializerBuilder, {{/kotlinx_serialization}}arrayOf(authName)) {
|
||||
setBearerToken(bearerToken)
|
||||
{{#generateOneOfAnyOfWrappers}}
|
||||
{{^kotlinx_serialization}}
|
||||
{{#gson}}
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{^isEnum}}
|
||||
{{^hasChildren}}
|
||||
serializerBuilder.registerTypeAdapterFactory({{modelPackage}}.{{{classname}}}.CustomTypeAdapterFactory())
|
||||
{{/hasChildren}}
|
||||
{{/isEnum}}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
{{/gson}}
|
||||
{{/kotlinx_serialization}}
|
||||
{{/generateOneOfAnyOfWrappers}}
|
||||
}
|
||||
|
||||
{{/isBasicBearer}}
|
||||
@ -240,21 +210,6 @@ import okhttp3.MediaType.Companion.toMediaType
|
||||
?.setClientSecret(secret)
|
||||
?.setUsername(username)
|
||||
?.setPassword(password)
|
||||
{{#generateOneOfAnyOfWrappers}}
|
||||
{{^kotlinx_serialization}}
|
||||
{{#gson}}
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{^isEnum}}
|
||||
{{^hasChildren}}
|
||||
serializerBuilder.registerTypeAdapterFactory({{modelPackage}}.{{{classname}}}.CustomTypeAdapterFactory())
|
||||
{{/hasChildren}}
|
||||
{{/isEnum}}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
{{/gson}}
|
||||
{{/kotlinx_serialization}}
|
||||
{{/generateOneOfAnyOfWrappers}}
|
||||
}
|
||||
|
||||
{{/hasOAuthMethods}}
|
||||
@ -391,6 +346,20 @@ import okhttp3.MediaType.Companion.toMediaType
|
||||
return retrofitBuilder.callFactory(usedCallFactory).build().create(serviceClass)
|
||||
}
|
||||
|
||||
{{#generateOneOfAnyOfWrappers}}
|
||||
{{^kotlinx_serialization}}
|
||||
{{#gson}}
|
||||
/**
|
||||
* Gets the serializer builder.
|
||||
* @return serial builder
|
||||
*/
|
||||
fun getSerializerBuilder(): GsonBuilder {
|
||||
return serializerBuilder
|
||||
}
|
||||
|
||||
{{/gson}}
|
||||
{{/kotlinx_serialization}}
|
||||
{{/generateOneOfAnyOfWrappers}}
|
||||
private fun normalizeBaseUrl() {
|
||||
if (!baseUrl.endsWith("/")) {
|
||||
baseUrl += "/"
|
||||
@ -416,3 +385,28 @@ import okhttp3.MediaType.Companion.toMediaType
|
||||
}
|
||||
}
|
||||
}
|
||||
{{#generateOneOfAnyOfWrappers}}
|
||||
{{^kotlinx_serialization}}
|
||||
{{#gson}}
|
||||
|
||||
/**
|
||||
* Registers all models with the type adapter factory.
|
||||
*
|
||||
* @param gsonBuilder gson builder
|
||||
* @return GSON builder
|
||||
*/
|
||||
fun registerTypeAdapterFactoryForAllModels(gsonBuilder: GsonBuilder): GsonBuilder {
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{^isEnum}}
|
||||
{{^hasChildren}}
|
||||
gsonBuilder.registerTypeAdapterFactory({{modelPackage}}.{{{classname}}}.CustomTypeAdapterFactory())
|
||||
{{/hasChildren}}
|
||||
{{/isEnum}}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
return gsonBuilder
|
||||
}
|
||||
{{/gson}}
|
||||
{{/kotlinx_serialization}}
|
||||
{{/generateOneOfAnyOfWrappers}}
|
||||
|
@ -23,7 +23,7 @@ import retrofit2.converter.gson.GsonConverterFactory
|
||||
class ApiClient(
|
||||
private var baseUrl: String = defaultBasePath,
|
||||
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
|
||||
private val serializerBuilder: GsonBuilder = Serializer.gsonBuilder,
|
||||
private val serializerBuilder: GsonBuilder = registerTypeAdapterFactoryForAllModels(Serializer.gsonBuilder),
|
||||
private val callFactory: Call.Factory? = null,
|
||||
private val callAdapterFactories: List<CallAdapter.Factory> = listOf(
|
||||
),
|
||||
@ -112,17 +112,6 @@ class ApiClient(
|
||||
?.setClientSecret(secret)
|
||||
?.setUsername(username)
|
||||
?.setPassword(password)
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiAnnotation.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiAnyOfUserOrPet.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiAnyOfUserOrPetOrArrayString.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiApiResponse.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiCategory.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiOrder.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiPet.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiTag.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiUser.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiUserOrPet.CustomTypeAdapterFactory())
|
||||
serializerBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiUserOrPetOrArrayString.CustomTypeAdapterFactory())
|
||||
}
|
||||
|
||||
/**
|
||||
@ -218,6 +207,14 @@ class ApiClient(
|
||||
return retrofitBuilder.callFactory(usedCallFactory).build().create(serviceClass)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the serializer builder.
|
||||
* @return serial builder
|
||||
*/
|
||||
fun getSerializerBuilder(): GsonBuilder {
|
||||
return serializerBuilder
|
||||
}
|
||||
|
||||
private fun normalizeBaseUrl() {
|
||||
if (!baseUrl.endsWith("/")) {
|
||||
baseUrl += "/"
|
||||
@ -243,3 +240,24 @@ class ApiClient(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers all models with the type adapter factory.
|
||||
*
|
||||
* @param gsonBuilder gson builder
|
||||
* @return GSON builder
|
||||
*/
|
||||
fun registerTypeAdapterFactoryForAllModels(gsonBuilder: GsonBuilder): GsonBuilder {
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiAnnotation.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiAnyOfUserOrPet.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiAnyOfUserOrPetOrArrayString.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiApiResponse.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiCategory.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiOrder.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiPet.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiTag.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiUser.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiUserOrPet.CustomTypeAdapterFactory())
|
||||
gsonBuilder.registerTypeAdapterFactory(org.openapitools.client.models.ApiUserOrPetOrArrayString.CustomTypeAdapterFactory())
|
||||
return gsonBuilder
|
||||
}
|
||||
|
@ -156,16 +156,15 @@ data class ApiPet (
|
||||
ApiCategory.validateJsonElement(jsonObj["category"])
|
||||
}
|
||||
if (jsonObj["tags"] != null && !jsonObj["tags"].isJsonNull) {
|
||||
val jsonArraytags = jsonObj.getAsJsonArray("tags")
|
||||
if (jsonArraytags != null) {
|
||||
if (jsonObj.getAsJsonArray("tags") != null) {
|
||||
// ensure the json data is an array
|
||||
require(jsonObj["tags"].isJsonArray) {
|
||||
String.format("Expected the field `tags` to be an array in the JSON string but got `%s`", jsonObj["tags"].toString())
|
||||
}
|
||||
|
||||
// validate the optional field `tags` (array)
|
||||
for (i in 0 until jsonArraytags.size()) {
|
||||
ApiTag.validateJsonElement(jsonArraytags[i])
|
||||
for (i in 0 until jsonObj.getAsJsonArray("tags").size()) {
|
||||
ApiTag.validateJsonElement(jsonObj.getAsJsonArray("tags").get(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,8 @@ import com.google.gson.GsonBuilder
|
||||
import io.kotlintest.matchers.types.shouldBeSameInstanceAs
|
||||
import io.kotlintest.shouldNotBe
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.openapitools.client.infrastructure.ApiClient
|
||||
import org.openapitools.client.infrastructure.registerTypeAdapterFactoryForAllModels
|
||||
import java.io.IOException
|
||||
import java.lang.RuntimeException
|
||||
|
||||
@ -34,13 +36,7 @@ class ApiUserOrPetTest : ShouldSpec() {
|
||||
init {
|
||||
|
||||
should("test custom type adapter") {
|
||||
val gson = GsonBuilder()
|
||||
.registerTypeAdapterFactory(ApiUser.CustomTypeAdapterFactory())
|
||||
.registerTypeAdapterFactory(ApiCategory.CustomTypeAdapterFactory())
|
||||
.registerTypeAdapterFactory(ApiPet.CustomTypeAdapterFactory())
|
||||
.registerTypeAdapterFactory(ApiTag.CustomTypeAdapterFactory())
|
||||
.registerTypeAdapterFactory(ApiUserOrPet.CustomTypeAdapterFactory())
|
||||
.create()
|
||||
val gson = ApiClient().getSerializerBuilder().create()
|
||||
|
||||
// test Category
|
||||
val categoryJson = "{\"id\":123,\"name\":\"category\"}"
|
||||
|
Loading…
x
Reference in New Issue
Block a user