forked from loafle/openapi-generator-original
[kotlin][client] unify jvm libraries (#4827)
* [kotlin] unify jvm envirement * [kotlin] rename retrofit2 to jvm-retrofit2 * [kotlin] update retrofit2 bat script * [kotlin] update docs * [kotlin] update docs * [kotlin] update pet projects
This commit is contained in:
committed by
William Cheng
parent
9feaae8572
commit
56f298a0b4
@@ -27,6 +27,6 @@ fi
|
||||
|
||||
# if you've executed sbt assembly previously it will use that instead.
|
||||
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
|
||||
ags="generate -t modules/openapi-generator/src/main/resources/kotlin-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin --artifact-id kotlin-petstore-retrofit2 --library retrofit2 -o samples/client/petstore/kotlin-retrofit2 $@"
|
||||
ags="generate -t modules/openapi-generator/src/main/resources/kotlin-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin --artifact-id kotlin-petstore-retrofit2 --library jvm-retrofit2 -o samples/client/petstore/kotlin-retrofit2 $@"
|
||||
|
||||
java ${JAVA_OPTS} -jar ${executable} ${ags}
|
||||
@@ -5,6 +5,6 @@ If Not Exist %executable% (
|
||||
)
|
||||
|
||||
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
|
||||
set ags=generate --artifact-id "kotlin-petstore-retrofit2" -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g kotlin --library retrofit2 -o samples\client\petstore\kotlin-retrofit2
|
||||
set ags=generate --artifact-id "kotlin-petstore-retrofit2" -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g kotlin --library jvm-retrofit2 -o samples\client\petstore\kotlin-retrofit2
|
||||
|
||||
java %JAVA_OPTS% -jar %executable% %ags%
|
||||
|
||||
@@ -20,5 +20,5 @@ sidebar_label: kotlin
|
||||
|modelMutable|Create mutable models| |false|
|
||||
|dateLibrary|Option. Date library to use|<dl><dt>**string**</dt><dd>String</dd><dt>**java8**</dt><dd>Java 8 native JSR310 (jvm only)</dd><dt>**threetenbp**</dt><dd>Threetenbp (jvm only)</dd><dl>|java8|
|
||||
|collectionType|Option. Collection type to use|<dl><dt>**array**</dt><dd>kotlin.Array</dd><dt>**list**</dt><dd>kotlin.collections.List</dd><dl>|array|
|
||||
|library|Library template (sub-template) to use|<dl><dt>**jvm-okhttp4**</dt><dd>[DEFAULT] Platform: Java Virtual Machine. HTTP client: OkHttp 4.2.0 (Android 5.0+ and Java 8+). JSON processing: Moshi 1.8.0.</dd><dt>**jvm-okhttp3**</dt><dd>Platform: Java Virtual Machine. HTTP client: OkHttp 3.12.4 (Android 2.3+ and Java 7+). JSON processing: Moshi 1.8.0.</dd><dt>**retrofit2**</dt><dd>Platform: Java Virtual Machine. HTTP client: Retrofit 2.6.2.</dd><dt>**multiplatform**</dt><dd>Platform: Kotlin multiplatform. HTTP client: Ktor 1.2.4. JSON processing: Kotlinx Serialization: 0.12.0.</dd><dl>|jvm-okhttp4|
|
||||
|library|Library template (sub-template) to use|<dl><dt>**jvm-okhttp4**</dt><dd>[DEFAULT] Platform: Java Virtual Machine. HTTP client: OkHttp 4.2.0 (Android 5.0+ and Java 8+). JSON processing: Moshi 1.8.0.</dd><dt>**jvm-okhttp3**</dt><dd>Platform: Java Virtual Machine. HTTP client: OkHttp 3.12.4 (Android 2.3+ and Java 7+). JSON processing: Moshi 1.8.0.</dd><dt>**jvm-retrofit2**</dt><dd>Platform: Java Virtual Machine. HTTP client: Retrofit 2.6.2.</dd><dt>**multiplatform**</dt><dd>Platform: Kotlin multiplatform. HTTP client: Ktor 1.2.4. JSON processing: Kotlinx Serialization: 0.12.0.</dd><dl>|jvm-okhttp4|
|
||||
|requestDateConverter|JVM-Option. Defines in how to handle date-time objects that are used for a request (as query or parameter)|<dl><dt>**toJson**</dt><dd>Date formater option using a json converter.</dd><dt>**toString**</dt><dd>[DEFAULT] Use the 'toString'-method of the date-time object to retrieve the related string representation.</dd><dl>|toString|
|
||||
|
||||
@@ -37,9 +37,10 @@ import java.util.stream.Stream;
|
||||
public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
|
||||
protected static final String JVM = "jvm";
|
||||
protected static final String JVM_OKHTTP = "jvm-okhttp";
|
||||
protected static final String JVM_OKHTTP4 = "jvm-okhttp4";
|
||||
protected static final String JVM_OKHTTP3 = "jvm-okhttp3";
|
||||
protected static final String RETROFIT2 = "retrofit2";
|
||||
protected static final String JVM_RETROFIT2 = "jvm-retrofit2";
|
||||
protected static final String MULTIPLATFORM = "multiplatform";
|
||||
|
||||
public static final String DATE_LIBRARY = "dateLibrary";
|
||||
@@ -127,7 +128,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
|
||||
supportedLibraries.put(JVM_OKHTTP4, "[DEFAULT] Platform: Java Virtual Machine. HTTP client: OkHttp 4.2.0 (Android 5.0+ and Java 8+). JSON processing: Moshi 1.8.0.");
|
||||
supportedLibraries.put(JVM_OKHTTP3, "Platform: Java Virtual Machine. HTTP client: OkHttp 3.12.4 (Android 2.3+ and Java 7+). JSON processing: Moshi 1.8.0.");
|
||||
supportedLibraries.put(RETROFIT2, "Platform: Java Virtual Machine. HTTP client: Retrofit 2.6.2.");
|
||||
supportedLibraries.put(JVM_RETROFIT2, "Platform: Java Virtual Machine. HTTP client: Retrofit 2.6.2.");
|
||||
supportedLibraries.put(MULTIPLATFORM, "Platform: Kotlin multiplatform. HTTP client: Ktor 1.2.4. JSON processing: Kotlinx Serialization: 0.12.0.");
|
||||
|
||||
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "Library template (sub-template) to use");
|
||||
@@ -194,10 +195,10 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
switch (getLibrary()) {
|
||||
case JVM_OKHTTP3:
|
||||
case JVM_OKHTTP4:
|
||||
processJVMLibrary(infrastructureFolder);
|
||||
processJVMOkHttpLibrary(infrastructureFolder);
|
||||
break;
|
||||
case RETROFIT2:
|
||||
processRetrofit2Library(infrastructureFolder);
|
||||
case JVM_RETROFIT2:
|
||||
processJVMRetrofit2Library(infrastructureFolder);
|
||||
break;
|
||||
case MULTIPLATFORM:
|
||||
processMultiplatformLibrary(infrastructureFolder);
|
||||
@@ -266,31 +267,37 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
additionalProperties.put(DateLibrary.JAVA8.value, true);
|
||||
}
|
||||
|
||||
private void processRetrofit2Library(String infrastructureFolder) {
|
||||
additionalProperties.put(RETROFIT2, true);
|
||||
private void processJVMRetrofit2Library(String infrastructureFolder) {
|
||||
additionalProperties.put(JVM, true);
|
||||
additionalProperties.put(JVM_RETROFIT2, true);
|
||||
supportingFiles.add(new SupportingFile("infrastructure/ApiClient.kt.mustache", infrastructureFolder, "ApiClient.kt"));
|
||||
supportingFiles.add(new SupportingFile("infrastructure/CollectionFormats.kt.mustache", infrastructureFolder, "CollectionFormats.kt"));
|
||||
addSupportingSerializerAdapters(infrastructureFolder);
|
||||
}
|
||||
|
||||
private void addSupportingSerializerAdapters(final String infrastructureFolder) {
|
||||
supportingFiles.add(new SupportingFile("infrastructure/Serializer.kt.mustache", infrastructureFolder, "Serializer.kt"));
|
||||
supportingFiles.add(new SupportingFile("infrastructure/ByteArrayAdapter.kt.mustache", infrastructureFolder, "ByteArrayAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("infrastructure/UUIDAdapter.kt.mustache", infrastructureFolder, "UUIDAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/Serializer.kt.mustache", infrastructureFolder, "Serializer.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/ByteArrayAdapter.kt.mustache", infrastructureFolder, "ByteArrayAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
|
||||
|
||||
if (getSerializationLibrary() == SERIALIZATION_LIBRARY_TYPE.gson) {
|
||||
supportingFiles.add(new SupportingFile("infrastructure/DateAdapter.kt.mustache", infrastructureFolder,
|
||||
"DateAdapter.kt"));
|
||||
switch (getSerializationLibrary()) {
|
||||
case moshi:
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/UUIDAdapter.kt.mustache", infrastructureFolder, "UUIDAdapter.kt"));
|
||||
break;
|
||||
|
||||
case gson:
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/DateAdapter.kt.mustache", infrastructureFolder, "DateAdapter.kt"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void processJVMLibrary(final String infrastructureFolder) {
|
||||
private void processJVMOkHttpLibrary(final String infrastructureFolder) {
|
||||
commonJvmMultiplatformSupportingFiles(infrastructureFolder);
|
||||
addSupportingSerializerAdapters(infrastructureFolder);
|
||||
|
||||
additionalProperties.put(JVM, true);
|
||||
additionalProperties.put(JVM_OKHTTP, true);
|
||||
|
||||
if (JVM_OKHTTP4.equals(getLibrary())) {
|
||||
additionalProperties.put(JVM_OKHTTP4, true);
|
||||
@@ -298,8 +305,8 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
additionalProperties.put(JVM_OKHTTP3, true);
|
||||
}
|
||||
|
||||
supportedLibraries.put(JVM, "A workaround to use the same template folder for both 'jvm-okhttp3' and 'jvm-okhttp4'.");
|
||||
setLibrary(JVM);
|
||||
supportedLibraries.put(JVM_OKHTTP, "A workaround to use the same template folder for both 'jvm-okhttp3' and 'jvm-okhttp4'.");
|
||||
setLibrary(JVM_OKHTTP);
|
||||
|
||||
// jvm specific supporting files
|
||||
supportingFiles.add(new SupportingFile("infrastructure/ApplicationDelegates.kt.mustache", infrastructureFolder, "ApplicationDelegates.kt"));
|
||||
|
||||
@@ -8,9 +8,9 @@ wrapper {
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.3.61'
|
||||
{{#retrofit2}}
|
||||
{{#jvm-retrofit2}}
|
||||
ext.retrofitVersion = '2.6.2'
|
||||
{{/retrofit2}}
|
||||
{{/jvm-retrofit2}}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@@ -58,7 +58,7 @@ dependencies {
|
||||
{{#threetenbp}}
|
||||
compile "org.threeten:threetenbp:1.4.0"
|
||||
{{/threetenbp}}
|
||||
{{#retrofit2}}
|
||||
{{#jvm-retrofit2}}
|
||||
compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
|
||||
{{#gson}}
|
||||
compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
|
||||
@@ -67,6 +67,6 @@ dependencies {
|
||||
compile "com.squareup.retrofit2:converter-moshi:$retrofitVersion"
|
||||
{{/moshi}}
|
||||
compile "com.squareup.retrofit2:converter-scalars:$retrofitVersion"
|
||||
{{/retrofit2}}
|
||||
{{/jvm-retrofit2}}
|
||||
testCompile "io.kotlintest:kotlintest-runner-junit5:3.1.0"
|
||||
}
|
||||
|
||||
@@ -46,7 +46,6 @@ import java.util.Date
|
||||
.registerTypeAdapter(Date::class.java, DateAdapter())
|
||||
.registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeAdapter())
|
||||
.registerTypeAdapter(LocalDate::class.java, LocalDateAdapter())
|
||||
.registerTypeAdapter(UUID::class.java, UUIDAdapter())
|
||||
.registerTypeAdapter(ByteArray::class.java, ByteArrayAdapter())
|
||||
|
||||
@JvmStatic
|
||||
@@ -0,0 +1,13 @@
|
||||
package {{packageName}}.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.util.UUID
|
||||
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class UUIDAdapter {
|
||||
@ToJson
|
||||
fun toJson(uuid: UUID) = uuid.toString()
|
||||
|
||||
@FromJson
|
||||
fun fromJson(s: String) = UUID.fromString(s)
|
||||
}
|
||||
@@ -31,7 +31,7 @@ interface {{classname}} {
|
||||
{{/prioritizedContentTypes}}
|
||||
{{/formParams}}
|
||||
@{{httpMethod}}("{{{path}}}")
|
||||
fun {{operationId}}({{^allParams}}){{/allParams}}{{#allParams}}{{>libraries/retrofit2/queryParams}}{{>libraries/retrofit2/pathParams}}{{>libraries/retrofit2/headerParams}}{{>libraries/retrofit2/bodyParams}}{{>libraries/retrofit2/formParams}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/allParams}}: Call<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>
|
||||
fun {{operationId}}({{^allParams}}){{/allParams}}{{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/allParams}}: Call<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>
|
||||
|
||||
{{/operation}}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package {{packageName}}.infrastructure
|
||||
|
||||
{{#moshi}}
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.google.gson.stream.JsonToken.NULL
|
||||
import java.io.IOException
|
||||
{{/gson}}
|
||||
import java.util.UUID
|
||||
|
||||
{{#moshi}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class UUIDAdapter {
|
||||
@ToJson
|
||||
fun toJson(uuid: UUID) = uuid.toString()
|
||||
|
||||
@FromJson
|
||||
fun fromJson(s: String) = UUID.fromString(s)
|
||||
}
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class UUIDAdapter : TypeAdapter<UUID>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: UUID?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
out?.value(value.toString())
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(out: JsonReader?): UUID? {
|
||||
out ?: return null
|
||||
|
||||
when (out.peek()) {
|
||||
NULL -> {
|
||||
out.nextNull()
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return UUID.fromString(out.nextString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/gson}}
|
||||
@@ -1,50 +0,0 @@
|
||||
package {{packageName}}.infrastructure
|
||||
|
||||
{{#moshi}}
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.google.gson.stream.JsonToken.NULL
|
||||
import java.io.IOException
|
||||
{{/gson}}
|
||||
|
||||
{{#moshi}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class ByteArrayAdapter {
|
||||
@ToJson
|
||||
fun toJson(data: ByteArray): String = String(data)
|
||||
|
||||
@FromJson
|
||||
fun fromJson(data: String): ByteArray = data.toByteArray()
|
||||
}
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class ByteArrayAdapter : TypeAdapter<ByteArray>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: ByteArray?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
out?.value(String(value))
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(out: JsonReader?): ByteArray? {
|
||||
out ?: return null
|
||||
|
||||
when (out.peek()) {
|
||||
NULL -> {
|
||||
out.nextNull()
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return out.nextString().toByteArray()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/gson}}
|
||||
@@ -1,37 +0,0 @@
|
||||
package {{packageName}}.infrastructure
|
||||
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.google.gson.stream.JsonToken.NULL
|
||||
import java.io.IOException
|
||||
import java.text.DateFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class DateAdapter(val formatter: DateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())) : TypeAdapter<Date>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: Date?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
out?.value(formatter.format(value))
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(out: JsonReader?): Date? {
|
||||
out ?: return null
|
||||
|
||||
when (out.peek()) {
|
||||
NULL -> {
|
||||
out.nextNull()
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return formatter.parse(out.nextString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package {{packageName}}.infrastructure
|
||||
|
||||
{{#moshi}}
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.google.gson.stream.JsonToken.NULL
|
||||
import java.io.IOException
|
||||
{{/gson}}
|
||||
{{^threetenbp}}
|
||||
import java.time.LocalDate
|
||||
import java.time.format.DateTimeFormatter
|
||||
{{/threetenbp}}
|
||||
{{#threetenbp}}
|
||||
import org.threeten.bp.LocalDate
|
||||
import org.threeten.bp.format.DateTimeFormatter
|
||||
{{/threetenbp}}
|
||||
|
||||
{{#moshi}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}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)
|
||||
}
|
||||
|
||||
}
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateAdapter(private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE) : TypeAdapter<LocalDate>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: LocalDate?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
out?.value(formatter.format(value))
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(out: JsonReader?): LocalDate? {
|
||||
out ?: return null
|
||||
|
||||
when (out.peek()) {
|
||||
NULL -> {
|
||||
out.nextNull()
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return LocalDate.parse(out.nextString(), formatter)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/gson}}
|
||||
@@ -1,63 +0,0 @@
|
||||
package {{packageName}}.infrastructure
|
||||
|
||||
{{#moshi}}
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.google.gson.stream.JsonToken.NULL
|
||||
import java.io.IOException
|
||||
{{/gson}}
|
||||
{{^threetenbp}}
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
{{/threetenbp}}
|
||||
{{#threetenbp}}
|
||||
import org.threeten.bp.LocalDateTime
|
||||
import org.threeten.bp.format.DateTimeFormatter
|
||||
{{/threetenbp}}
|
||||
|
||||
{{#moshi}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}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)
|
||||
}
|
||||
|
||||
}
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateTimeAdapter(private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME) : TypeAdapter<LocalDateTime>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: LocalDateTime?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
out?.value(formatter.format(value))
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(out: JsonReader?): LocalDateTime? {
|
||||
out ?: return null
|
||||
|
||||
when (out.peek()) {
|
||||
NULL -> {
|
||||
out.nextNull()
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return LocalDateTime.parse(out.nextString(), formatter)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/gson}}
|
||||
@@ -1,45 +0,0 @@
|
||||
package {{packageName}}.infrastructure
|
||||
|
||||
{{#moshi}}
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
{{^threetenbp}}
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
{{/threetenbp}}
|
||||
{{#threetenbp}}
|
||||
import org.threeten.bp.LocalDate
|
||||
import org.threeten.bp.LocalDateTime
|
||||
{{/threetenbp}}
|
||||
import java.util.UUID
|
||||
{{/gson}}
|
||||
import java.util.Date
|
||||
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}object Serializer {
|
||||
{{#moshi}}
|
||||
@JvmStatic
|
||||
val moshi: Moshi = Moshi.Builder()
|
||||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
||||
.add(LocalDateTimeAdapter())
|
||||
.add(LocalDateAdapter())
|
||||
.add(UUIDAdapter())
|
||||
.add(ByteArrayAdapter())
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
@JvmStatic
|
||||
val gson: Gson = GsonBuilder()
|
||||
.registerTypeAdapter(Date::class.java, DateAdapter())
|
||||
.registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeAdapter())
|
||||
.registerTypeAdapter(LocalDate::class.java, LocalDateAdapter())
|
||||
.registerTypeAdapter(UUID::class.java, UUIDAdapter())
|
||||
.registerTypeAdapter(ByteArray::class.java, ByteArrayAdapter())
|
||||
.create()
|
||||
{{/gson}}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package {{packageName}}.infrastructure
|
||||
|
||||
{{#moshi}}
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.google.gson.stream.JsonToken.NULL
|
||||
import java.io.IOException
|
||||
{{/gson}}
|
||||
import java.util.UUID
|
||||
|
||||
{{#moshi}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class UUIDAdapter {
|
||||
@ToJson
|
||||
fun toJson(uuid: UUID) = uuid.toString()
|
||||
|
||||
@FromJson
|
||||
fun fromJson(s: String) = UUID.fromString(s)
|
||||
}
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class UUIDAdapter : TypeAdapter<UUID>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: UUID?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
out?.value(value.toString())
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(out: JsonReader?): UUID? {
|
||||
out ?: return null
|
||||
|
||||
when (out.peek()) {
|
||||
NULL -> {
|
||||
out.nextNull()
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return UUID.fromString(out.nextString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/gson}}
|
||||
@@ -13,7 +13,6 @@ object Serializer {
|
||||
.registerTypeAdapter(Date::class.java, DateAdapter())
|
||||
.registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeAdapter())
|
||||
.registerTypeAdapter(LocalDate::class.java, LocalDateAdapter())
|
||||
.registerTypeAdapter(UUID::class.java, UUIDAdapter())
|
||||
.registerTypeAdapter(ByteArray::class.java, ByteArrayAdapter())
|
||||
|
||||
@JvmStatic
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.google.gson.stream.JsonToken.NULL
|
||||
import java.io.IOException
|
||||
import java.util.UUID
|
||||
|
||||
class UUIDAdapter : TypeAdapter<UUID>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: UUID?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
out?.value(value.toString())
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(out: JsonReader?): UUID? {
|
||||
out ?: return null
|
||||
|
||||
when (out.peek()) {
|
||||
NULL -> {
|
||||
out.nextNull()
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return UUID.fromString(out.nextString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,16 @@ import java.util.Date
|
||||
|
||||
object Serializer {
|
||||
@JvmStatic
|
||||
val moshi: Moshi = Moshi.Builder()
|
||||
val moshiBuilder: Moshi.Builder = Moshi.Builder()
|
||||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
||||
.add(LocalDateTimeAdapter())
|
||||
.add(LocalDateAdapter())
|
||||
.add(UUIDAdapter())
|
||||
.add(ByteArrayAdapter())
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
|
||||
@JvmStatic
|
||||
val moshi: Moshi by lazy {
|
||||
moshiBuilder.build()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,19 @@
|
||||
|
||||
## Requires
|
||||
|
||||
* Kotlin 1.3.41
|
||||
* Gradle 4.9
|
||||
|
||||
## Build
|
||||
|
||||
First, create the gradle wrapper script:
|
||||
|
||||
```
|
||||
gradle wrapper
|
||||
```
|
||||
|
||||
Then, run:
|
||||
|
||||
```
|
||||
./gradlew check assemble
|
||||
```
|
||||
@@ -16,7 +26,7 @@ This runs all tests and packages the library.
|
||||
* Supports JSON inputs/outputs, File inputs, and Form inputs.
|
||||
* Supports collection formats for query parameters: csv, tsv, ssv, pipes.
|
||||
* Some Kotlin and Java types are fully qualified to avoid conflicts with types defined in OpenAPI definitions.
|
||||
|
||||
* Implementation of ApiClient is intended to reduce method counts, specifically to benefit Android targets.
|
||||
|
||||
<a name="documentation-for-api-endpoints"></a>
|
||||
## Documentation for API Endpoints
|
||||
|
||||
@@ -7,12 +7,16 @@ import java.util.Date
|
||||
|
||||
object Serializer {
|
||||
@JvmStatic
|
||||
val moshi: Moshi = Moshi.Builder()
|
||||
val moshiBuilder: Moshi.Builder = Moshi.Builder()
|
||||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
|
||||
.add(LocalDateTimeAdapter())
|
||||
.add(LocalDateAdapter())
|
||||
.add(UUIDAdapter())
|
||||
.add(ByteArrayAdapter())
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
|
||||
@JvmStatic
|
||||
val moshi: Moshi by lazy {
|
||||
moshiBuilder.build()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user