Skip to content

Commit 56f298a

Browse files
4brunuwing328
authored andcommitted
[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
1 parent 9feaae8 commit 56f298a

File tree

36 files changed

+70
-428
lines changed

36 files changed

+70
-428
lines changed

bin/kotlin-client-retrofit2.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ fi
2727

2828
# if you've executed sbt assembly previously it will use that instead.
2929
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-retrofit2 --library retrofit2 -o samples/client/petstore/kotlin-retrofit2 $@"
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-retrofit2 --library jvm-retrofit2 -o samples/client/petstore/kotlin-retrofit2 $@"
3131

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

bin/windows/kotlin-client-retrofit2.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ If Not Exist %executable% (
55
)
66

77
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
8-
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
8+
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
99

1010
java %JAVA_OPTS% -jar %executable% %ags%

docs/generators/kotlin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ sidebar_label: kotlin
2020
|modelMutable|Create mutable models| |false|
2121
|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|
2222
|collectionType|Option. Collection type to use|<dl><dt>**array**</dt><dd>kotlin.Array</dd><dt>**list**</dt><dd>kotlin.collections.List</dd><dl>|array|
23-
|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|
23+
|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|
2424
|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|

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737
public class KotlinClientCodegen extends AbstractKotlinCodegen {
3838

3939
protected static final String JVM = "jvm";
40+
protected static final String JVM_OKHTTP = "jvm-okhttp";
4041
protected static final String JVM_OKHTTP4 = "jvm-okhttp4";
4142
protected static final String JVM_OKHTTP3 = "jvm-okhttp3";
42-
protected static final String RETROFIT2 = "retrofit2";
43+
protected static final String JVM_RETROFIT2 = "jvm-retrofit2";
4344
protected static final String MULTIPLATFORM = "multiplatform";
4445

4546
public static final String DATE_LIBRARY = "dateLibrary";
@@ -127,7 +128,7 @@ public KotlinClientCodegen() {
127128

128129
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.");
129130
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.");
130-
supportedLibraries.put(RETROFIT2, "Platform: Java Virtual Machine. HTTP client: Retrofit 2.6.2.");
131+
supportedLibraries.put(JVM_RETROFIT2, "Platform: Java Virtual Machine. HTTP client: Retrofit 2.6.2.");
131132
supportedLibraries.put(MULTIPLATFORM, "Platform: Kotlin multiplatform. HTTP client: Ktor 1.2.4. JSON processing: Kotlinx Serialization: 0.12.0.");
132133

133134
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "Library template (sub-template) to use");
@@ -194,10 +195,10 @@ public void processOpts() {
194195
switch (getLibrary()) {
195196
case JVM_OKHTTP3:
196197
case JVM_OKHTTP4:
197-
processJVMLibrary(infrastructureFolder);
198+
processJVMOkHttpLibrary(infrastructureFolder);
198199
break;
199-
case RETROFIT2:
200-
processRetrofit2Library(infrastructureFolder);
200+
case JVM_RETROFIT2:
201+
processJVMRetrofit2Library(infrastructureFolder);
201202
break;
202203
case MULTIPLATFORM:
203204
processMultiplatformLibrary(infrastructureFolder);
@@ -266,40 +267,46 @@ private void processJava8Date() {
266267
additionalProperties.put(DateLibrary.JAVA8.value, true);
267268
}
268269

269-
private void processRetrofit2Library(String infrastructureFolder) {
270-
additionalProperties.put(RETROFIT2, true);
270+
private void processJVMRetrofit2Library(String infrastructureFolder) {
271+
additionalProperties.put(JVM, true);
272+
additionalProperties.put(JVM_RETROFIT2, true);
271273
supportingFiles.add(new SupportingFile("infrastructure/ApiClient.kt.mustache", infrastructureFolder, "ApiClient.kt"));
272274
supportingFiles.add(new SupportingFile("infrastructure/CollectionFormats.kt.mustache", infrastructureFolder, "CollectionFormats.kt"));
273275
addSupportingSerializerAdapters(infrastructureFolder);
274276
}
275277

276278
private void addSupportingSerializerAdapters(final String infrastructureFolder) {
277-
supportingFiles.add(new SupportingFile("infrastructure/Serializer.kt.mustache", infrastructureFolder, "Serializer.kt"));
278-
supportingFiles.add(new SupportingFile("infrastructure/ByteArrayAdapter.kt.mustache", infrastructureFolder, "ByteArrayAdapter.kt"));
279-
supportingFiles.add(new SupportingFile("infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
280-
supportingFiles.add(new SupportingFile("infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
281-
supportingFiles.add(new SupportingFile("infrastructure/UUIDAdapter.kt.mustache", infrastructureFolder, "UUIDAdapter.kt"));
282-
283-
if (getSerializationLibrary() == SERIALIZATION_LIBRARY_TYPE.gson) {
284-
supportingFiles.add(new SupportingFile("infrastructure/DateAdapter.kt.mustache", infrastructureFolder,
285-
"DateAdapter.kt"));
279+
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/Serializer.kt.mustache", infrastructureFolder, "Serializer.kt"));
280+
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/ByteArrayAdapter.kt.mustache", infrastructureFolder, "ByteArrayAdapter.kt"));
281+
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
282+
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
283+
284+
switch (getSerializationLibrary()) {
285+
case moshi:
286+
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/UUIDAdapter.kt.mustache", infrastructureFolder, "UUIDAdapter.kt"));
287+
break;
288+
289+
case gson:
290+
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/DateAdapter.kt.mustache", infrastructureFolder, "DateAdapter.kt"));
291+
break;
286292
}
287293
}
288294

289-
private void processJVMLibrary(final String infrastructureFolder) {
295+
private void processJVMOkHttpLibrary(final String infrastructureFolder) {
290296
commonJvmMultiplatformSupportingFiles(infrastructureFolder);
291297
addSupportingSerializerAdapters(infrastructureFolder);
292298

293299
additionalProperties.put(JVM, true);
300+
additionalProperties.put(JVM_OKHTTP, true);
294301

295302
if (JVM_OKHTTP4.equals(getLibrary())) {
296303
additionalProperties.put(JVM_OKHTTP4, true);
297304
} else if (JVM_OKHTTP3.equals(getLibrary())) {
298305
additionalProperties.put(JVM_OKHTTP3, true);
299306
}
300307

301-
supportedLibraries.put(JVM, "A workaround to use the same template folder for both 'jvm-okhttp3' and 'jvm-okhttp4'.");
302-
setLibrary(JVM);
308+
supportedLibraries.put(JVM_OKHTTP, "A workaround to use the same template folder for both 'jvm-okhttp3' and 'jvm-okhttp4'.");
309+
setLibrary(JVM_OKHTTP);
303310

304311
// jvm specific supporting files
305312
supportingFiles.add(new SupportingFile("infrastructure/ApplicationDelegates.kt.mustache", infrastructureFolder, "ApplicationDelegates.kt"));

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ wrapper {
88

99
buildscript {
1010
ext.kotlin_version = '1.3.61'
11-
{{#retrofit2}}
11+
{{#jvm-retrofit2}}
1212
ext.retrofitVersion = '2.6.2'
13-
{{/retrofit2}}
13+
{{/jvm-retrofit2}}
1414

1515
repositories {
1616
mavenCentral()
@@ -58,7 +58,7 @@ dependencies {
5858
{{#threetenbp}}
5959
compile "org.threeten:threetenbp:1.4.0"
6060
{{/threetenbp}}
61-
{{#retrofit2}}
61+
{{#jvm-retrofit2}}
6262
compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
6363
{{#gson}}
6464
compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
@@ -67,6 +67,6 @@ dependencies {
6767
compile "com.squareup.retrofit2:converter-moshi:$retrofitVersion"
6868
{{/moshi}}
6969
compile "com.squareup.retrofit2:converter-scalars:$retrofitVersion"
70-
{{/retrofit2}}
70+
{{/jvm-retrofit2}}
7171
testCompile "io.kotlintest:kotlintest-runner-junit5:3.1.0"
7272
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import java.util.Date
4646
.registerTypeAdapter(Date::class.java, DateAdapter())
4747
.registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeAdapter())
4848
.registerTypeAdapter(LocalDate::class.java, LocalDateAdapter())
49-
.registerTypeAdapter(UUID::class.java, UUIDAdapter())
5049
.registerTypeAdapter(ByteArray::class.java, ByteArrayAdapter())
5150

5251
@JvmStatic
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package {{packageName}}.infrastructure
2+
3+
import com.squareup.moshi.FromJson
4+
import com.squareup.moshi.ToJson
5+
import java.util.UUID
6+
7+
{{#nonPublicApi}}internal {{/nonPublicApi}}class UUIDAdapter {
8+
@ToJson
9+
fun toJson(uuid: UUID) = uuid.toString()
10+
11+
@FromJson
12+
fun fromJson(s: String) = UUID.fromString(s)
13+
}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/retrofit2/api.mustache renamed to modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ interface {{classname}} {
3131
{{/prioritizedContentTypes}}
3232
{{/formParams}}
3333
@{{httpMethod}}("{{{path}}}")
34-
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}}>
34+
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}}>
3535

3636
{{/operation}}
3737
}

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

Lines changed: 0 additions & 51 deletions
This file was deleted.

modules/openapi-generator/src/main/resources/kotlin-client/libraries/retrofit2/infrastructure/ByteArrayAdapter.kt.mustache

Lines changed: 0 additions & 50 deletions
This file was deleted.

modules/openapi-generator/src/main/resources/kotlin-client/libraries/retrofit2/infrastructure/DateAdapter.kt.mustache

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)