Skip to content
Permalink
Browse files

[kotlin][client] gson complete integration (#4332)

* [kotlin] update dependencies

* [kotlin][client] complete gson integration

* [kotlin][client] run petstores

* [kotlin][client] fix JsonMediaType and update petstores

* Add kotlin client gson to pom.xml

* Remove org.json dependency

* Try to fix kotlin-gson build

* Add kotlin client nonpublic to pom.xml

* Update samples

* try to fix build

* try to fix build

* try to fix build

* try to fix build

* [kotlin][client] restore libraries version

* [kotlin][client] update kotlin version

* [kotlin][client] update some libraries versions

* [kotlin][client] update some libraries versions

* [kotlin][client] add kotlin-gson and kotlin-nonpublic to CI test suit

* [kotlin][client] update some libraries versions

* try to fix build

* try to fix build
  • Loading branch information...
4brunu authored and wing328 committed Nov 8, 2019
1 parent f9af3c2 commit 7cf027fbf5704cc12c1c4eb180308bff69325e6b
Showing with 3,201 additions and 32 deletions.
  1. +1 −0 bin/kotlin-client-all.sh
  2. +32 −0 bin/kotlin-client-gson.sh
  3. +0 −2 bin/kotlin-client-nonpublic.sh
  4. +0 −2 bin/kotlin-client-okhttp3.sh
  5. +4 −0 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java
  6. +7 −5 modules/openapi-generator/src/main/resources/kotlin-client/build.gradle.mustache
  7. +7 −2 ...api-generator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/ApiClient.kt.mustache
  8. +39 −1 ...erator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/ByteArrayAdapter.kt.mustache
  9. +37 −0 ...i-generator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/DateAdapter.kt.mustache
  10. +38 −0 ...erator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/LocalDateAdapter.kt.mustache
  11. +38 −0 ...or/src/main/resources/kotlin-client/libraries/jvm/infrastructure/LocalDateTimeAdapter.kt.mustache
  12. +27 −0 ...pi-generator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/Serializer.kt.mustache
  13. +38 −0 ...i-generator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/UUIDAdapter.kt.mustache
  14. +2 −0 pom.xml
  15. +23 −0 samples/client/petstore/kotlin-gson/.openapi-generator-ignore
  16. +1 −0 samples/client/petstore/kotlin-gson/.openapi-generator/VERSION
  17. +90 −0 samples/client/petstore/kotlin-gson/README.md
  18. +36 −0 samples/client/petstore/kotlin-gson/build.gradle
  19. +12 −0 samples/client/petstore/kotlin-gson/docs/ApiResponse.md
  20. +11 −0 samples/client/petstore/kotlin-gson/docs/Category.md
  21. +22 −0 samples/client/petstore/kotlin-gson/docs/Order.md
  22. +22 −0 samples/client/petstore/kotlin-gson/docs/Pet.md
  23. +405 −0 samples/client/petstore/kotlin-gson/docs/PetApi.md
  24. +196 −0 samples/client/petstore/kotlin-gson/docs/StoreApi.md
  25. +11 −0 samples/client/petstore/kotlin-gson/docs/Tag.md
  26. +17 −0 samples/client/petstore/kotlin-gson/docs/User.md
  27. +376 −0 samples/client/petstore/kotlin-gson/docs/UserApi.md
  28. +46 −0 samples/client/petstore/kotlin-gson/pom.xml
  29. +2 −0 samples/client/petstore/kotlin-gson/settings.gradle
  30. +318 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt
  31. +168 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt
  32. +309 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/apis/UserApi.kt
  33. +23 −0 ...nt/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiAbstractions.kt
  34. +172 −0 ...s/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt
  35. +40 −0 ...e/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiInfrastructureResponse.kt
  36. +29 −0 ...tstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApplicationDelegates.kt
  37. +33 −0 ...t/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt
  38. +37 −0 ...client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/DateAdapter.kt
  39. +42 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/Errors.kt
  40. +35 −0 ...t/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt
  41. +35 −0 ...tstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt
  42. +16 −0 ...ient/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
  43. +8 −0 ...ient/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/RequestMethod.kt
  44. +23 −0 ...petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ResponseExtensions.kt
  45. +19 −0 .../client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt
  46. +34 −0 ...client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt
  47. +33 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/models/ApiResponse.kt
  48. +30 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/models/Category.kt
  49. +55 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/models/Order.kt
  50. +57 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/models/Pet.kt
  51. +30 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/models/Tag.kt
  52. +49 −0 samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/models/User.kt
  53. +3 −3 samples/client/petstore/kotlin-nonpublic/build.gradle
  54. +46 −0 samples/client/petstore/kotlin-nonpublic/pom.xml
  55. +1 −1 ...store/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt
  56. +3 −3 samples/client/petstore/kotlin-okhttp3/build.gradle
  57. +1 −1 ...etstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt
  58. +3 −3 samples/client/petstore/kotlin-string/build.gradle
  59. +1 −1 ...petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt
  60. +3 −3 samples/client/petstore/kotlin-threetenbp/build.gradle
  61. +1 −1 ...tore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt
  62. +3 −3 samples/client/petstore/kotlin/build.gradle
  63. +1 −1 ...client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt
@@ -1,5 +1,6 @@
#!/bin/sh

./bin/kotlin-client-gson.sh
./bin/kotlin-client-nonpublic.sh
./bin/kotlin-client-okhttp3.sh
./bin/kotlin-client-petstore-multiplatform.sh
@@ -0,0 +1,32 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=$(ls -ld "$SCRIPT")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=$(dirname "$SCRIPT")/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=$(dirname "$SCRIPT")/..
APP_DIR=$(cd "${APP_DIR}"; pwd)
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn -B clean package
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-gson --additional-properties serializationLibrary=gson -o samples/client/petstore/kotlin-gson $@"

java ${JAVA_OPTS} -jar ${executable} ${ags}
@@ -30,5 +30,3 @@ 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-nonpublic --additional-properties nonPublicApi=true -o samples/client/petstore/kotlin-nonpublic $@"

java ${JAVA_OPTS} -jar ${executable} ${ags}

cp CI/samples.ci/client/petstore/kotlin-string/pom.xml samples/client/petstore/kotlin-string/pom.xml
@@ -30,5 +30,3 @@ 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-okhttp3 --library jvm-okhttp3 -o samples/client/petstore/kotlin-okhttp3 $@"

java ${JAVA_OPTS} -jar ${executable} ${ags}

#cp CI/samples.ci/client/petstore/kotlin-okhttp3/pom.xml samples/client/petstore/kotlin-okhttp3/pom.xml
@@ -188,6 +188,10 @@ public void processOpts() {
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"));
if (getSerializationLibrary() == SERIALIZATION_LIBRARY_TYPE.gson) {
supportingFiles.add(new SupportingFile("infrastructure/DateAdapter.kt.mustache", infrastructureFolder,
"DateAdapter.kt"));
}

} else if (MULTIPLATFORM.equals(getLibrary())) {
additionalProperties.put(MULTIPLATFORM, true);
@@ -7,7 +7,7 @@ wrapper {
}

buildscript {
ext.kotlin_version = '1.3.41'
ext.kotlin_version = '1.3.50'

repositories {
mavenCentral()
@@ -30,19 +30,21 @@ test {
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
{{#moshi}}
compile "com.squareup.moshi:moshi-kotlin:1.8.0"
compile "com.squareup.moshi:moshi-adapters:1.8.0"
{{/moshi}}
{{#gson}}
implementation "com.google.code.gson:gson:2.8.5"
compile "com.google.code.gson:gson:2.8.6"
{{/gson}}
{{#jvm-okhttp3}}
compile "com.squareup.okhttp3:okhttp:3.12.4"
compile "com.squareup.okhttp3:okhttp:3.12.6"
{{/jvm-okhttp3}}
{{#jvm-okhttp4}}
compile "com.squareup.okhttp3:okhttp:4.2.0"
compile "com.squareup.okhttp3:okhttp:4.2.2"
{{/jvm-okhttp4}}
{{#threetenbp}}
compile "org.threeten:threetenbp:1.4.0"
{{/threetenbp}}
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.1.0"
testCompile "io.kotlintest:kotlintest-runner-junit5:3.1.0"
}
@@ -71,11 +71,16 @@ import java.io.File
}
{{#jvm-okhttp3}}
mediaType == JsonMediaType -> RequestBody.create(
{{#moshi}}
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
{{/moshi}}
{{#gson}}
MediaType.parse(mediaType), Serializer.gson.toJson(content, T::class.java)
{{/gson}}
)
{{/jvm-okhttp3}}
{{#jvm-okhttp4}}
mediaType == JsonMediaType -> Serializer.moshi.adapter(T::class.java).toJson(content).toRequestBody(
mediaType == JsonMediaType -> {{#moshi}}Serializer.moshi.adapter(T::class.java).toJson(content){{/moshi}}{{#gson}}Serializer.gson.toJson(content, T::class.java){{/gson}}.toRequestBody(
mediaType.toMediaTypeOrNull()
)
{{/jvm-okhttp4}}
@@ -93,7 +98,7 @@ import java.io.File
return null
}
return when(mediaType) {
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
JsonMediaType -> {{#moshi}}Serializer.moshi.adapter(T::class.java).fromJson(bodyContent){{/moshi}}{{#gson}}Serializer.gson.fromJson(bodyContent, T::class.java){{/gson}}
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
}
}
@@ -1,12 +1,50 @@
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}}
@@ -0,0 +1,37 @@
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,7 +1,16 @@
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
@@ -11,6 +20,7 @@ import org.threeten.bp.LocalDate
import org.threeten.bp.format.DateTimeFormatter
{{/threetenbp}}

{{#moshi}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateAdapter {
@ToJson
fun toJson(value: LocalDate): String {
@@ -23,3 +33,31 @@ import org.threeten.bp.format.DateTimeFormatter
}

}
{{/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,7 +1,16 @@
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
@@ -11,6 +20,7 @@ import org.threeten.bp.LocalDateTime
import org.threeten.bp.format.DateTimeFormatter
{{/threetenbp}}

{{#moshi}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateTimeAdapter {
@ToJson
fun toJson(value: LocalDateTime): String {
@@ -23,3 +33,31 @@ import org.threeten.bp.format.DateTimeFormatter
}

}
{{/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,11 +1,27 @@
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())
@@ -15,4 +31,15 @@ import java.util.Date
.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}}
}

0 comments on commit 7cf027f

Please sign in to comment.
You can’t perform that action at this time.