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

Some content is hidden

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

46 files changed

+2817
-6
lines changed

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%

bin/windows/kotlin-client-string.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-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)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.2.1-SNAPSHOT
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# org.openapitools.client - Kotlin client library for OpenAPI Petstore
2+
3+
## Requires
4+
5+
* Kotlin 1.3.41
6+
* Gradle 4.9
7+
8+
## Build
9+
10+
First, create the gradle wrapper script:
11+
12+
```
13+
gradle wrapper
14+
```
15+
16+
Then, run:
17+
18+
```
19+
./gradlew check assemble
20+
```
21+
22+
This runs all tests and packages the library.
23+
24+
## Features/Implementation Notes
25+
26+
* Supports JSON inputs/outputs, File inputs, and Form inputs.
27+
* Supports collection formats for query parameters: csv, tsv, ssv, pipes.
28+
* Some Kotlin and Java types are fully qualified to avoid conflicts with types defined in OpenAPI definitions.
29+
* Implementation of ApiClient is intended to reduce method counts, specifically to benefit Android targets.
30+
31+
<a name="documentation-for-api-endpoints"></a>
32+
## Documentation for API Endpoints
33+
34+
All URIs are relative to *http://petstore.swagger.io/v2*
35+
36+
Class | Method | HTTP request | Description
37+
------------ | ------------- | ------------- | -------------
38+
*PetApi* | [**addPet**](docs/PetApi.md#addpet) | **POST** /pet | Add a new pet to the store
39+
*PetApi* | [**deletePet**](docs/PetApi.md#deletepet) | **DELETE** /pet/{petId} | Deletes a pet
40+
*PetApi* | [**findPetsByStatus**](docs/PetApi.md#findpetsbystatus) | **GET** /pet/findByStatus | Finds Pets by status
41+
*PetApi* | [**findPetsByTags**](docs/PetApi.md#findpetsbytags) | **GET** /pet/findByTags | Finds Pets by tags
42+
*PetApi* | [**getPetById**](docs/PetApi.md#getpetbyid) | **GET** /pet/{petId} | Find pet by ID
43+
*PetApi* | [**updatePet**](docs/PetApi.md#updatepet) | **PUT** /pet | Update an existing pet
44+
*PetApi* | [**updatePetWithForm**](docs/PetApi.md#updatepetwithform) | **POST** /pet/{petId} | Updates a pet in the store with form data
45+
*PetApi* | [**uploadFile**](docs/PetApi.md#uploadfile) | **POST** /pet/{petId}/uploadImage | uploads an image
46+
*StoreApi* | [**deleteOrder**](docs/StoreApi.md#deleteorder) | **DELETE** /store/order/{orderId} | Delete purchase order by ID
47+
*StoreApi* | [**getInventory**](docs/StoreApi.md#getinventory) | **GET** /store/inventory | Returns pet inventories by status
48+
*StoreApi* | [**getOrderById**](docs/StoreApi.md#getorderbyid) | **GET** /store/order/{orderId} | Find purchase order by ID
49+
*StoreApi* | [**placeOrder**](docs/StoreApi.md#placeorder) | **POST** /store/order | Place an order for a pet
50+
*UserApi* | [**createUser**](docs/UserApi.md#createuser) | **POST** /user | Create user
51+
*UserApi* | [**createUsersWithArrayInput**](docs/UserApi.md#createuserswitharrayinput) | **POST** /user/createWithArray | Creates list of users with given input array
52+
*UserApi* | [**createUsersWithListInput**](docs/UserApi.md#createuserswithlistinput) | **POST** /user/createWithList | Creates list of users with given input array
53+
*UserApi* | [**deleteUser**](docs/UserApi.md#deleteuser) | **DELETE** /user/{username} | Delete user
54+
*UserApi* | [**getUserByName**](docs/UserApi.md#getuserbyname) | **GET** /user/{username} | Get user by user name
55+
*UserApi* | [**loginUser**](docs/UserApi.md#loginuser) | **GET** /user/login | Logs user into the system
56+
*UserApi* | [**logoutUser**](docs/UserApi.md#logoutuser) | **GET** /user/logout | Logs out current logged in user session
57+
*UserApi* | [**updateUser**](docs/UserApi.md#updateuser) | **PUT** /user/{username} | Updated user
58+
59+
60+
<a name="documentation-for-models"></a>
61+
## Documentation for Models
62+
63+
- [org.openapitools.client.models.ApiResponse](docs/ApiResponse.md)
64+
- [org.openapitools.client.models.Category](docs/Category.md)
65+
- [org.openapitools.client.models.Order](docs/Order.md)
66+
- [org.openapitools.client.models.Pet](docs/Pet.md)
67+
- [org.openapitools.client.models.Tag](docs/Tag.md)
68+
- [org.openapitools.client.models.User](docs/User.md)
69+
70+
71+
<a name="documentation-for-authorization"></a>
72+
## Documentation for Authorization
73+
74+
<a name="api_key"></a>
75+
### api_key
76+
77+
- **Type**: API key
78+
- **API key parameter name**: api_key
79+
- **Location**: HTTP header
80+
81+
<a name="petstore_auth"></a>
82+
### petstore_auth
83+
84+
- **Type**: OAuth
85+
- **Flow**: implicit
86+
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
87+
- **Scopes**:
88+
- write:pets: modify pets in your account
89+
- read:pets: read your pets
90+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
group 'org.openapitools'
2+
version '1.0.0'
3+
4+
wrapper {
5+
gradleVersion = '4.9'
6+
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
7+
}
8+
9+
buildscript {
10+
ext.kotlin_version = '1.3.50'
11+
12+
repositories {
13+
mavenCentral()
14+
}
15+
dependencies {
16+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
17+
}
18+
}
19+
20+
apply plugin: 'kotlin'
21+
22+
repositories {
23+
mavenCentral()
24+
}
25+
26+
test {
27+
useJUnitPlatform()
28+
}
29+
30+
dependencies {
31+
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
32+
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
33+
compile "com.squareup.moshi:moshi-kotlin:1.8.0"
34+
compile "com.squareup.moshi:moshi-adapters:1.8.0"
35+
compile "com.squareup.okhttp3:okhttp:4.2.2"
36+
testCompile "io.kotlintest:kotlintest-runner-junit5:3.1.0"
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# ApiResponse
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**code** | **kotlin.Int** | | [optional]
8+
**type** | **kotlin.String** | | [optional]
9+
**message** | **kotlin.String** | | [optional]
10+
11+
12+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# Category
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**id** | **kotlin.Long** | | [optional]
8+
**name** | **kotlin.String** | | [optional]
9+
10+
11+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Order
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**id** | **kotlin.Long** | | [optional]
8+
**petId** | **kotlin.Long** | | [optional]
9+
**quantity** | **kotlin.Int** | | [optional]
10+
**shipDate** | [**java.time.LocalDateTime**](java.time.LocalDateTime.md) | | [optional]
11+
**status** | [**inline**](#StatusEnum) | Order Status | [optional]
12+
**complete** | **kotlin.Boolean** | | [optional]
13+
14+
15+
<a name="StatusEnum"></a>
16+
## Enum: status
17+
Name | Value
18+
---- | -----
19+
status | placed, approved, delivered
20+
21+
22+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Pet
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**id** | **kotlin.Long** | | [optional]
8+
**category** | [**Category**](Category.md) | | [optional]
9+
**name** | **kotlin.String** | |
10+
**photoUrls** | **kotlin.Array&lt;kotlin.String&gt;** | |
11+
**tags** | [**kotlin.Array&lt;Tag&gt;**](Tag.md) | | [optional]
12+
**status** | [**inline**](#StatusEnum) | pet status in the store | [optional]
13+
14+
15+
<a name="StatusEnum"></a>
16+
## Enum: status
17+
Name | Value
18+
---- | -----
19+
status | available, pending, sold
20+
21+
22+

0 commit comments

Comments
 (0)