Skip to content

Commit 34c715e

Browse files
Mordagjimschubert
authored andcommitted
[Kotlin][Client] Add option to make all api method return a nullable model (#4422)
* Added new additional parameter to allow nullable api return types * Tweaking samples and .bat files I've added new .bat files for the kotlin-client to alllow windows-devs to re-generate required samples via windows-shell (for CI).
1 parent 4494bf1 commit 34c715e

46 files changed

Lines changed: 2817 additions & 6 deletions

Some content is hidden

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

bin/kotlin-client-all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
./bin/kotlin-client-petstore.sh
88
./bin/kotlin-client-string.sh
99
./bin/kotlin-client-threetenbp.sh
10+
./bin/kotlin-client-nullable.sh

bin/kotlin-client-nullable.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-nullable --additional-properties nullableReturnType=true,serializableModel=true -o samples/client/petstore/kotlin-nullable $@"
31+
32+
java ${JAVA_OPTS} -jar ${executable} ${ags}

bin/windows/kotlin-client-all.bat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
call powershell -command "& '%~dp0\kotlin-client-gson.bat'"
2+
call powershell -command "& '%~dp0\kotlin-client-nonpublic.bat'"
3+
call powershell -command "& '%~dp0\kotlin-client-okhttp3.bat'"
4+
call powershell -command "& '%~dp0\kotlin-client-petstore-multiplatform.bat'"
5+
call powershell -command "& '%~dp0\kotlin-client-petstore.bat'"
6+
call powershell -command "& '%~dp0\kotlin-client-string.bat'"
7+
call powershell -command "& '%~dp0\kotlin-client-threetenbp.bat'"
8+
call powershell -command "& '%~dp0\kotlin-client-nullable.bat'"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
8+
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g kotlin --artifact-id "kotlin-petstore-nullable" --additional-properties nullableReturnType=true,serializableModel=true -o samples\client\petstore\kotlin-nullable
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%

bin/windows/kotlin-client-petstore.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-client" -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g kotlin -o samples\client\petstore\kotlin
8+
set ags=generate --artifact-id "kotlin-petstore-client" -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g kotlin -o samples\client\petstore\kotlin --additional-properties dateLibrary=java8,serializableModel=true
99

1010
java %JAVA_OPTS% -jar %executable% %ags%
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
8+
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g kotlin --artifact-id "kotlin-petstore-string" --additional-properties dateLibrary=string,serializableModel=true -o samples\client\petstore\kotlin-string
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
8+
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g kotlin --artifact-id "kotlin-petstore-threetenbp" --additional-properties dateLibrary=threetenbp -o samples\client\petstore\kotlin-threetenbp
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import {{packageName}}.infrastructure.toMultiValue
2424
* {{summary}}
2525
* {{notes}}
2626
{{#allParams}}* @param {{paramName}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
27-
{{/allParams}}* @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
27+
{{/allParams}}* @return {{#returnType}}{{{returnType}}}{{#nullableReturnType}} or null{{/nullableReturnType}}{{/returnType}}{{^returnType}}void{{/returnType}}
2828
* @throws UnsupportedOperationException If the API returns an informational or redirection response
2929
* @throws ClientException If the API returns a client error response
3030
* @throws ServerException If the API returns a server error response
3131
*/{{#returnType}}
3232
@Suppress("UNCHECKED_CAST"){{/returnType}}
3333
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
34-
fun {{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}} {
34+
fun {{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : {{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}} {
3535
val localVariableBody: kotlin.Any? = {{#hasBodyParam}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}null{{/hasFormParams}}{{#hasFormParams}}mapOf({{#formParams}}"{{{baseName}}}" to "${{paramName}}"{{#hasMore}}, {{/hasMore}}{{/formParams}}){{/hasFormParams}}{{/hasBodyParam}}
3636
val localVariableQuery: MultiValueMap = {{^hasQueryParams}}mutableMapOf()
3737
{{/hasQueryParams}}{{#hasQueryParams}}mutableMapOf<kotlin.String, List<kotlin.String>>()
@@ -68,7 +68,7 @@ import {{packageName}}.infrastructure.toMultiValue
6868
)
6969

7070
return when (localVarResponse.responseType) {
71-
ResponseType.Success -> {{#returnType}}(localVarResponse as Success<*>).data as {{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
71+
ResponseType.Success -> {{#returnType}}(localVarResponse as Success<*>).data as {{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
7272
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
7373
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
7474
ResponseType.ClientError -> throw ClientException((localVarResponse as ClientError<*>).body as? String ?: "Client error")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ val apiInstance = {{{classname}}}()
3030
val {{{paramName}}} : {{{dataType}}} = {{{example}}} // {{{dataType}}} | {{{description}}}
3131
{{/allParams}}
3232
try {
33-
{{#returnType}}val result : {{{returnType}}} = {{/returnType}}apiInstance.{{{operationId}}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}
33+
{{#returnType}}val result : {{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}} = {{/returnType}}apiInstance.{{{operationId}}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}
3434
println(result){{/returnType}}
3535
} catch (e: ClientException) {
3636
println("4xx response calling {{{classname}}}#{{{operationId}}}")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package {{packageName}}.infrastructure
1010
}
1111

1212
{{#nonPublicApi}}internal {{/nonPublicApi}}class Success<T>(
13-
val data: T,
13+
val data: T{{#nullableReturnType}}?{{/nullableReturnType}},
1414
override val statusCode: Int = -1,
1515
override val headers: Map<String, List<String>> = mapOf()
1616
): ApiInfrastructureResponse<T>(ResponseType.Success)

0 commit comments

Comments
 (0)