Skip to content

Commit 7cf027f

Browse files
4brunuwing328
authored andcommitted
[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
1 parent f9af3c2 commit 7cf027f

File tree

63 files changed

+3201
-32
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+3201
-32
lines changed

bin/kotlin-client-all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/sh
22

3+
./bin/kotlin-client-gson.sh
34
./bin/kotlin-client-nonpublic.sh
45
./bin/kotlin-client-okhttp3.sh
56
./bin/kotlin-client-petstore-multiplatform.sh

bin/kotlin-client-gson.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=$(ls -ld "$SCRIPT")
8+
link=$(expr "$ls" : '.*-> \(.*\)$')
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=$(dirname "$SCRIPT")/"$link"
13+
fi
14+
done
15+
16+
if [ ! -d "${APP_DIR}" ]; then
17+
APP_DIR=$(dirname "$SCRIPT")/..
18+
APP_DIR=$(cd "${APP_DIR}"; pwd)
19+
fi
20+
21+
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
22+
23+
if [ ! -f "$executable" ]
24+
then
25+
mvn -B clean package
26+
fi
27+
28+
# if you've executed sbt assembly previously it will use that instead.
29+
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30+
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 $@"
31+
32+
java ${JAVA_OPTS} -jar ${executable} ${ags}

bin/kotlin-client-nonpublic.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,3 @@ export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
3030
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 $@"
3131

3232
java ${JAVA_OPTS} -jar ${executable} ${ags}
33-
34-
cp CI/samples.ci/client/petstore/kotlin-string/pom.xml samples/client/petstore/kotlin-string/pom.xml

bin/kotlin-client-okhttp3.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,3 @@ export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
3030
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 $@"
3131

3232
java ${JAVA_OPTS} -jar ${executable} ${ags}
33-
34-
#cp CI/samples.ci/client/petstore/kotlin-okhttp3/pom.xml samples/client/petstore/kotlin-okhttp3/pom.xml

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ public void processOpts() {
188188
supportingFiles.add(new SupportingFile("infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
189189
supportingFiles.add(new SupportingFile("infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
190190
supportingFiles.add(new SupportingFile("infrastructure/UUIDAdapter.kt.mustache", infrastructureFolder, "UUIDAdapter.kt"));
191+
if (getSerializationLibrary() == SERIALIZATION_LIBRARY_TYPE.gson) {
192+
supportingFiles.add(new SupportingFile("infrastructure/DateAdapter.kt.mustache", infrastructureFolder,
193+
"DateAdapter.kt"));
194+
}
191195

192196
} else if (MULTIPLATFORM.equals(getLibrary())) {
193197
additionalProperties.put(MULTIPLATFORM, true);

modules/openapi-generator/src/main/resources/kotlin-client/build.gradle.mustache

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ wrapper {
77
}
88

99
buildscript {
10-
ext.kotlin_version = '1.3.41'
10+
ext.kotlin_version = '1.3.50'
1111
1212
repositories {
1313
mavenCentral()
@@ -30,19 +30,21 @@ test {
3030
dependencies {
3131
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
3232
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
33+
{{#moshi}}
3334
compile "com.squareup.moshi:moshi-kotlin:1.8.0"
3435
compile "com.squareup.moshi:moshi-adapters:1.8.0"
36+
{{/moshi}}
3537
{{#gson}}
36-
implementation "com.google.code.gson:gson:2.8.5"
38+
compile "com.google.code.gson:gson:2.8.6"
3739
{{/gson}}
3840
{{#jvm-okhttp3}}
39-
compile "com.squareup.okhttp3:okhttp:3.12.4"
41+
compile "com.squareup.okhttp3:okhttp:3.12.6"
4042
{{/jvm-okhttp3}}
4143
{{#jvm-okhttp4}}
42-
compile "com.squareup.okhttp3:okhttp:4.2.0"
44+
compile "com.squareup.okhttp3:okhttp:4.2.2"
4345
{{/jvm-okhttp4}}
4446
{{#threetenbp}}
4547
compile "org.threeten:threetenbp:1.4.0"
4648
{{/threetenbp}}
47-
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.1.0"
49+
testCompile "io.kotlintest:kotlintest-runner-junit5:3.1.0"
4850
}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/ApiClient.kt.mustache

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ import java.io.File
7171
}
7272
{{#jvm-okhttp3}}
7373
mediaType == JsonMediaType -> RequestBody.create(
74+
{{#moshi}}
7475
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
76+
{{/moshi}}
77+
{{#gson}}
78+
MediaType.parse(mediaType), Serializer.gson.toJson(content, T::class.java)
79+
{{/gson}}
7580
)
7681
{{/jvm-okhttp3}}
7782
{{#jvm-okhttp4}}
78-
mediaType == JsonMediaType -> Serializer.moshi.adapter(T::class.java).toJson(content).toRequestBody(
83+
mediaType == JsonMediaType -> {{#moshi}}Serializer.moshi.adapter(T::class.java).toJson(content){{/moshi}}{{#gson}}Serializer.gson.toJson(content, T::class.java){{/gson}}.toRequestBody(
7984
mediaType.toMediaTypeOrNull()
8085
)
8186
{{/jvm-okhttp4}}
@@ -93,7 +98,7 @@ import java.io.File
9398
return null
9499
}
95100
return when(mediaType) {
96-
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(bodyContent)
101+
JsonMediaType -> {{#moshi}}Serializer.moshi.adapter(T::class.java).fromJson(bodyContent){{/moshi}}{{#gson}}Serializer.gson.fromJson(bodyContent, T::class.java){{/gson}}
97102
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
98103
}
99104
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,50 @@
11
package {{packageName}}.infrastructure
22

3+
{{#moshi}}
34
import com.squareup.moshi.FromJson
45
import com.squareup.moshi.ToJson
6+
{{/moshi}}
7+
{{#gson}}
8+
import com.google.gson.TypeAdapter
9+
import com.google.gson.stream.JsonReader
10+
import com.google.gson.stream.JsonWriter
11+
import com.google.gson.stream.JsonToken.NULL
12+
import java.io.IOException
13+
{{/gson}}
514

15+
{{#moshi}}
616
{{#nonPublicApi}}internal {{/nonPublicApi}}class ByteArrayAdapter {
717
@ToJson
818
fun toJson(data: ByteArray): String = String(data)
919
1020
@FromJson
1121
fun fromJson(data: String): ByteArray = data.toByteArray()
12-
}
22+
}
23+
{{/moshi}}
24+
{{#gson}}
25+
{{#nonPublicApi}}internal {{/nonPublicApi}}class ByteArrayAdapter : TypeAdapter<ByteArray>() {
26+
@Throws(IOException::class)
27+
override fun write(out: JsonWriter?, value: ByteArray?) {
28+
if (value == null) {
29+
out?.nullValue()
30+
} else {
31+
out?.value(String(value))
32+
}
33+
}
34+
35+
@Throws(IOException::class)
36+
override fun read(out: JsonReader?): ByteArray? {
37+
out ?: return null
38+
39+
when (out.peek()) {
40+
NULL -> {
41+
out.nextNull()
42+
return null
43+
}
44+
else -> {
45+
return out.nextString().toByteArray()
46+
}
47+
}
48+
}
49+
}
50+
{{/gson}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package {{packageName}}.infrastructure
2+
3+
import com.google.gson.TypeAdapter
4+
import com.google.gson.stream.JsonReader
5+
import com.google.gson.stream.JsonWriter
6+
import com.google.gson.stream.JsonToken.NULL
7+
import java.io.IOException
8+
import java.text.DateFormat
9+
import java.text.SimpleDateFormat
10+
import java.util.Date
11+
import java.util.Locale
12+
13+
{{#nonPublicApi}}internal {{/nonPublicApi}}class DateAdapter(val formatter: DateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())) : TypeAdapter<Date>() {
14+
@Throws(IOException::class)
15+
override fun write(out: JsonWriter?, value: Date?) {
16+
if (value == null) {
17+
out?.nullValue()
18+
} else {
19+
out?.value(formatter.format(value))
20+
}
21+
}
22+
23+
@Throws(IOException::class)
24+
override fun read(out: JsonReader?): Date? {
25+
out ?: return null
26+
27+
when (out.peek()) {
28+
NULL -> {
29+
out.nextNull()
30+
return null
31+
}
32+
else -> {
33+
return formatter.parse(out.nextString())
34+
}
35+
}
36+
}
37+
}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/LocalDateAdapter.kt.mustache

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package {{packageName}}.infrastructure
22

3+
{{#moshi}}
34
import com.squareup.moshi.FromJson
45
import com.squareup.moshi.ToJson
6+
{{/moshi}}
7+
{{#gson}}
8+
import com.google.gson.TypeAdapter
9+
import com.google.gson.stream.JsonReader
10+
import com.google.gson.stream.JsonWriter
11+
import com.google.gson.stream.JsonToken.NULL
12+
import java.io.IOException
13+
{{/gson}}
514
{{^threetenbp}}
615
import java.time.LocalDate
716
import java.time.format.DateTimeFormatter
@@ -11,6 +20,7 @@ import org.threeten.bp.LocalDate
1120
import org.threeten.bp.format.DateTimeFormatter
1221
{{/threetenbp}}
1322

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

2535
}
36+
{{/moshi}}
37+
{{#gson}}
38+
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateAdapter(private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE) : TypeAdapter<LocalDate>() {
39+
@Throws(IOException::class)
40+
override fun write(out: JsonWriter?, value: LocalDate?) {
41+
if (value == null) {
42+
out?.nullValue()
43+
} else {
44+
out?.value(formatter.format(value))
45+
}
46+
}
47+
48+
@Throws(IOException::class)
49+
override fun read(out: JsonReader?): LocalDate? {
50+
out ?: return null
51+
52+
when (out.peek()) {
53+
NULL -> {
54+
out.nextNull()
55+
return null
56+
}
57+
else -> {
58+
return LocalDate.parse(out.nextString(), formatter)
59+
}
60+
}
61+
}
62+
}
63+
{{/gson}}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/LocalDateTimeAdapter.kt.mustache

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package {{packageName}}.infrastructure
22

3+
{{#moshi}}
34
import com.squareup.moshi.FromJson
45
import com.squareup.moshi.ToJson
6+
{{/moshi}}
7+
{{#gson}}
8+
import com.google.gson.TypeAdapter
9+
import com.google.gson.stream.JsonReader
10+
import com.google.gson.stream.JsonWriter
11+
import com.google.gson.stream.JsonToken.NULL
12+
import java.io.IOException
13+
{{/gson}}
514
{{^threetenbp}}
615
import java.time.LocalDateTime
716
import java.time.format.DateTimeFormatter
@@ -11,6 +20,7 @@ import org.threeten.bp.LocalDateTime
1120
import org.threeten.bp.format.DateTimeFormatter
1221
{{/threetenbp}}
1322

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

2535
}
36+
{{/moshi}}
37+
{{#gson}}
38+
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateTimeAdapter(private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME) : TypeAdapter<LocalDateTime>() {
39+
@Throws(IOException::class)
40+
override fun write(out: JsonWriter?, value: LocalDateTime?) {
41+
if (value == null) {
42+
out?.nullValue()
43+
} else {
44+
out?.value(formatter.format(value))
45+
}
46+
}
47+
48+
@Throws(IOException::class)
49+
override fun read(out: JsonReader?): LocalDateTime? {
50+
out ?: return null
51+
52+
when (out.peek()) {
53+
NULL -> {
54+
out.nextNull()
55+
return null
56+
}
57+
else -> {
58+
return LocalDateTime.parse(out.nextString(), formatter)
59+
}
60+
}
61+
}
62+
}
63+
{{/gson}}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm/infrastructure/Serializer.kt.mustache

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
package {{packageName}}.infrastructure
22

3+
{{#moshi}}
34
import com.squareup.moshi.Moshi
45
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
56
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
7+
{{/moshi}}
8+
{{#gson}}
9+
import com.google.gson.Gson
10+
import com.google.gson.GsonBuilder
11+
{{^threetenbp}}
12+
import java.time.LocalDate
13+
import java.time.LocalDateTime
14+
{{/threetenbp}}
15+
{{#threetenbp}}
16+
import org.threeten.bp.LocalDate
17+
import org.threeten.bp.LocalDateTime
18+
{{/threetenbp}}
19+
import java.util.UUID
20+
{{/gson}}
621
import java.util.Date
722

823
{{#nonPublicApi}}internal {{/nonPublicApi}}object Serializer {
24+
{{#moshi}}
925
@JvmStatic
1026
val moshi: Moshi = Moshi.Builder()
1127
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
@@ -15,4 +31,15 @@ import java.util.Date
1531
.add(ByteArrayAdapter())
1632
.add(KotlinJsonAdapterFactory())
1733
.build()
34+
{{/moshi}}
35+
{{#gson}}
36+
@JvmStatic
37+
val gson: Gson = GsonBuilder()
38+
.registerTypeAdapter(Date::class.java, DateAdapter())
39+
.registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeAdapter())
40+
.registerTypeAdapter(LocalDate::class.java, LocalDateAdapter())
41+
.registerTypeAdapter(UUID::class.java, UUIDAdapter())
42+
.registerTypeAdapter(ByteArray::class.java, ByteArrayAdapter())
43+
.create()
44+
{{/gson}}
1845
}

0 commit comments

Comments
 (0)