Skip to content

Commit aa49196

Browse files
committed
Add useResponseAsReturnType flag to kotlin-jvm-retrofit2-coroutines.yaml sample
1 parent d8bdbf9 commit aa49196

52 files changed

Lines changed: 3527 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/configs/unmaintained/kotlin-jvm-retrofit2-coroutines.yaml renamed to bin/configs/kotlin-jvm-retrofit2-coroutines.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
generatorName: kotlin
2-
outputDir: samples/openapi3/client/petstore/kotlin-jvm-retrofit2-coroutines
2+
outputDir: samples/client/petstore/kotlin-jvm-retrofit2-coroutines
33
library: jvm-retrofit2
4-
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
55
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
66
additionalProperties:
77
serializationLibrary: gson
88
useCoroutines: "true"
99
artifactId: kotlin-petstore-coroutines-client
1010
serializableModel: "true"
1111
dateLibrary: java8
12+
useResponseAsReturnType: false

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
@@ -155,7 +155,7 @@ import okhttp3.MultipartBody
155155
{{/prioritizedContentTypes}}
156156
{{/formParams}}
157157
@{{httpMethod}}("{{{path}}}")
158-
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}suspend {{/useCoroutines}}{{/doNotUseRxAndCoroutines}}fun {{operationId}}({{^allParams}}){{/allParams}}{{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{#-last}}){{/-last}}{{/allParams}}: {{^doNotUseRxAndCoroutines}}{{#useRxJava}}Observable<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/useRxJava}}{{#useRxJava2}}{{#returnType}}Single<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{/isResponseFile}}>{{/returnType}}{{^returnType}}Completable{{/returnType}}{{/useRxJava2}}{{#useRxJava3}}{{#returnType}}Single<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{/isResponseFile}}>{{/returnType}}{{^returnType}}Completable{{/returnType}}{{/useRxJava3}}{{#useCoroutines}}{{#useResponseAsReturnType}}Response<{{/useResponseAsReturnType}}{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}{{#useResponseAsReturnType}}>{{/useResponseAsReturnType}}{{/useCoroutines}}{{/doNotUseRxAndCoroutines}}{{#doNotUseRxAndCoroutines}}Call<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/doNotUseRxAndCoroutines}}
158+
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}suspend {{/useCoroutines}}{{/doNotUseRxAndCoroutines}}fun {{operationId}}({{^allParams}}){{/allParams}}{{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{#-last}}){{/-last}}{{/allParams}}{{#returnType}}: {{/returnType}}{{^returnType}}{{#useResponseAsReturnType}}: {{/useResponseAsReturnType}}{{/returnType}}{{^doNotUseRxAndCoroutines}}{{#useRxJava}}Observable<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/useRxJava}}{{#useRxJava2}}{{#returnType}}Single<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{/isResponseFile}}>{{/returnType}}{{^returnType}}Completable{{/returnType}}{{/useRxJava2}}{{#useRxJava3}}{{#returnType}}Single<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{/isResponseFile}}>{{/returnType}}{{^returnType}}Completable{{/returnType}}{{/useRxJava3}}{{#useCoroutines}}{{#useResponseAsReturnType}}Response<{{/useResponseAsReturnType}}{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{^returnType}}{{#useResponseAsReturnType}}Unit{{/useResponseAsReturnType}}{{/returnType}}{{/isResponseFile}}{{#useResponseAsReturnType}}>{{/useResponseAsReturnType}}{{/useCoroutines}}{{/doNotUseRxAndCoroutines}}{{#doNotUseRxAndCoroutines}}Call<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/doNotUseRxAndCoroutines}}
159159

160160
{{/operation}}
161161
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.openapitools.codegen.DefaultGenerator;
1010
import org.openapitools.codegen.languages.KotlinClientCodegen;
1111
import org.openapitools.codegen.languages.features.CXFServerFeatures;
12+
import org.testng.Assert;
1213
import org.testng.annotations.DataProvider;
1314
import org.testng.annotations.Test;
1415

@@ -17,6 +18,7 @@
1718
import java.nio.file.Files;
1819
import java.nio.file.Paths;
1920
import java.util.List;
21+
import java.util.stream.Collectors;
2022

2123
import static org.openapitools.codegen.TestUtils.assertFileContains;
2224

@@ -59,11 +61,15 @@ void testPathVariableIsNotEscaped_19930(ClientLibrary library) throws IOExceptio
5961

6062
@DataProvider(name = "useResponseAsReturnType")
6163
public static Object[][] useResponseAsReturnTypeTestData() {
62-
return new Object[][]{{null, "Response<Pet>"}, {true, "Response<Pet>"}, {false, "Pet"}, {"false", "Pet"}}; // null is default
64+
return new Object[][]{
65+
{null, "Response<Pet>", ": Response<Unit>"},
66+
{true, "Response<Pet>", ": Response<Unit>"},
67+
{false, "Pet", ""},
68+
{"false", "Pet", ""}};
6369
}
6470

6571
@Test(dataProvider = "useResponseAsReturnType")
66-
public void testUseResponseAsReturnType(Object useResponseAsReturnType, String expectedResponse) throws IOException {
72+
public void testUseResponseAsReturnType(Object useResponseAsReturnType, String expectedResponse, String expectedUnitResponse) throws IOException {
6773
OpenAPI openAPI = readOpenAPI("3_0/kotlin/petstore.yaml");
6874

6975
KotlinClientCodegen codegen = createCodegen(ClientLibrary.JVM_RETROFIT2);
@@ -80,7 +86,13 @@ public void testUseResponseAsReturnType(Object useResponseAsReturnType, String e
8086

8187
List<File> files = generator.opts(input).generate();
8288
File petApi = files.stream().filter(file -> file.getName().equals("PetApi.kt")).findAny().orElseThrow();
83-
assertFileContains(petApi.toPath(), "suspend fun addPet(@Body pet: Pet): " + expectedResponse);
89+
List<String> lines = Files.readAllLines(petApi.toPath()).stream().map(String::trim).collect(Collectors.toList());
90+
assertFileContainsLine(lines, "suspend fun addPet(@Body pet: Pet): " + expectedResponse);
91+
assertFileContainsLine(lines, "suspend fun deletePet(@Path(\"petId\") petId: kotlin.Long, @Header(\"api_key\") apiKey: kotlin.String? = null)" + expectedUnitResponse);
92+
}
93+
94+
private static void assertFileContainsLine(List<String> lines, String line) {
95+
Assert.assertListContains(lines, s -> s.equals(line), line);
8496
}
8597

8698
private static void enableOnlyApiGeneration(DefaultGenerator generator) {
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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.openapi-generator-ignore
2+
README.md
3+
build.gradle
4+
docs/ApiResponse.md
5+
docs/Category.md
6+
docs/Order.md
7+
docs/Pet.md
8+
docs/PetApi.md
9+
docs/StoreApi.md
10+
docs/Tag.md
11+
docs/User.md
12+
docs/UserApi.md
13+
gradle/wrapper/gradle-wrapper.jar
14+
gradle/wrapper/gradle-wrapper.properties
15+
gradlew
16+
gradlew.bat
17+
settings.gradle
18+
src/main/kotlin/org/openapitools/client/apis/PetApi.kt
19+
src/main/kotlin/org/openapitools/client/apis/StoreApi.kt
20+
src/main/kotlin/org/openapitools/client/apis/UserApi.kt
21+
src/main/kotlin/org/openapitools/client/auth/ApiKeyAuth.kt
22+
src/main/kotlin/org/openapitools/client/auth/OAuth.kt
23+
src/main/kotlin/org/openapitools/client/auth/OAuthFlow.kt
24+
src/main/kotlin/org/openapitools/client/auth/OAuthOkHttpClient.kt
25+
src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt
26+
src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt
27+
src/main/kotlin/org/openapitools/client/infrastructure/CollectionFormats.kt
28+
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt
29+
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt
30+
src/main/kotlin/org/openapitools/client/infrastructure/OffsetDateTimeAdapter.kt
31+
src/main/kotlin/org/openapitools/client/infrastructure/ResponseExt.kt
32+
src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt
33+
src/main/kotlin/org/openapitools/client/models/Category.kt
34+
src/main/kotlin/org/openapitools/client/models/ModelApiResponse.kt
35+
src/main/kotlin/org/openapitools/client/models/Order.kt
36+
src/main/kotlin/org/openapitools/client/models/Pet.kt
37+
src/main/kotlin/org/openapitools/client/models/Tag.kt
38+
src/main/kotlin/org/openapitools/client/models/User.kt
39+
src/test/kotlin/org/openapitools/client/apis/PetApiTest.kt
40+
src/test/kotlin/org/openapitools/client/apis/StoreApiTest.kt
41+
src/test/kotlin/org/openapitools/client/apis/UserApiTest.kt
42+
src/test/kotlin/org/openapitools/client/models/ApiResponseTest.kt
43+
src/test/kotlin/org/openapitools/client/models/CategoryTest.kt
44+
src/test/kotlin/org/openapitools/client/models/OrderTest.kt
45+
src/test/kotlin/org/openapitools/client/models/PetTest.kt
46+
src/test/kotlin/org/openapitools/client/models/TagTest.kt
47+
src/test/kotlin/org/openapitools/client/models/UserTest.kt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.12.0-SNAPSHOT
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# org.openapitools.client - Kotlin client library for OpenAPI Petstore
2+
3+
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
4+
5+
## Overview
6+
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [openapi-spec](https://github.com/OAI/OpenAPI-Specification) from a remote server, you can easily generate an API client.
7+
8+
- API version: 1.0.0
9+
- Package version:
10+
- Generator version: 7.12.0-SNAPSHOT
11+
- Build package: org.openapitools.codegen.languages.KotlinClientCodegen
12+
13+
## Requires
14+
15+
* Kotlin 1.7.21
16+
* Gradle 7.5
17+
18+
## Build
19+
20+
First, create the gradle wrapper script:
21+
22+
```
23+
gradle wrapper
24+
```
25+
26+
Then, run:
27+
28+
```
29+
./gradlew check assemble
30+
```
31+
32+
This runs all tests and packages the library.
33+
34+
## Features/Implementation Notes
35+
36+
* Supports JSON inputs/outputs, File inputs, and Form inputs.
37+
* Supports collection formats for query parameters: csv, tsv, ssv, pipes.
38+
* Some Kotlin and Java types are fully qualified to avoid conflicts with types defined in OpenAPI definitions.
39+
* Implementation of ApiClient is intended to reduce method counts, specifically to benefit Android targets.
40+
41+
<a id="documentation-for-api-endpoints"></a>
42+
## Documentation for API Endpoints
43+
44+
All URIs are relative to *http://petstore.swagger.io/v2*
45+
46+
| Class | Method | HTTP request | Description |
47+
| ------------ | ------------- | ------------- | ------------- |
48+
| *PetApi* | [**addPet**](docs/PetApi.md#addpet) | **POST** pet | Add a new pet to the store |
49+
| *PetApi* | [**deletePet**](docs/PetApi.md#deletepet) | **DELETE** pet/{petId} | Deletes a pet |
50+
| *PetApi* | [**findPetsByStatus**](docs/PetApi.md#findpetsbystatus) | **GET** pet/findByStatus | Finds Pets by status |
51+
| *PetApi* | [**findPetsByTags**](docs/PetApi.md#findpetsbytags) | **GET** pet/findByTags | Finds Pets by tags |
52+
| *PetApi* | [**getPetById**](docs/PetApi.md#getpetbyid) | **GET** pet/{petId} | Find pet by ID |
53+
| *PetApi* | [**updatePet**](docs/PetApi.md#updatepet) | **PUT** pet | Update an existing pet |
54+
| *PetApi* | [**updatePetWithForm**](docs/PetApi.md#updatepetwithform) | **POST** pet/{petId} | Updates a pet in the store with form data |
55+
| *PetApi* | [**uploadFile**](docs/PetApi.md#uploadfile) | **POST** pet/{petId}/uploadImage | uploads an image |
56+
| *StoreApi* | [**deleteOrder**](docs/StoreApi.md#deleteorder) | **DELETE** store/order/{orderId} | Delete purchase order by ID |
57+
| *StoreApi* | [**getInventory**](docs/StoreApi.md#getinventory) | **GET** store/inventory | Returns pet inventories by status |
58+
| *StoreApi* | [**getOrderById**](docs/StoreApi.md#getorderbyid) | **GET** store/order/{orderId} | Find purchase order by ID |
59+
| *StoreApi* | [**placeOrder**](docs/StoreApi.md#placeorder) | **POST** store/order | Place an order for a pet |
60+
| *UserApi* | [**createUser**](docs/UserApi.md#createuser) | **POST** user | Create user |
61+
| *UserApi* | [**createUsersWithArrayInput**](docs/UserApi.md#createuserswitharrayinput) | **POST** user/createWithArray | Creates list of users with given input array |
62+
| *UserApi* | [**createUsersWithListInput**](docs/UserApi.md#createuserswithlistinput) | **POST** user/createWithList | Creates list of users with given input array |
63+
| *UserApi* | [**deleteUser**](docs/UserApi.md#deleteuser) | **DELETE** user/{username} | Delete user |
64+
| *UserApi* | [**getUserByName**](docs/UserApi.md#getuserbyname) | **GET** user/{username} | Get user by user name |
65+
| *UserApi* | [**loginUser**](docs/UserApi.md#loginuser) | **GET** user/login | Logs user into the system |
66+
| *UserApi* | [**logoutUser**](docs/UserApi.md#logoutuser) | **GET** user/logout | Logs out current logged in user session |
67+
| *UserApi* | [**updateUser**](docs/UserApi.md#updateuser) | **PUT** user/{username} | Updated user |
68+
69+
70+
<a id="documentation-for-models"></a>
71+
## Documentation for Models
72+
73+
- [org.openapitools.client.models.Category](docs/Category.md)
74+
- [org.openapitools.client.models.ModelApiResponse](docs/ModelApiResponse.md)
75+
- [org.openapitools.client.models.Order](docs/Order.md)
76+
- [org.openapitools.client.models.Pet](docs/Pet.md)
77+
- [org.openapitools.client.models.Tag](docs/Tag.md)
78+
- [org.openapitools.client.models.User](docs/User.md)
79+
80+
81+
<a id="documentation-for-authorization"></a>
82+
## Documentation for Authorization
83+
84+
85+
Authentication schemes defined for the API:
86+
<a id="petstore_auth"></a>
87+
### petstore_auth
88+
89+
- **Type**: OAuth
90+
- **Flow**: implicit
91+
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
92+
- **Scopes**:
93+
- write:pets: modify pets in your account
94+
- read:pets: read your pets
95+
96+
<a id="api_key"></a>
97+
### api_key
98+
99+
- **Type**: API key
100+
- **API key parameter name**: api_key
101+
- **Location**: HTTP header
102+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
group 'org.openapitools'
2+
version '1.0.0'
3+
4+
wrapper {
5+
gradleVersion = '8.7'
6+
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
7+
}
8+
9+
buildscript {
10+
ext.kotlin_version = '1.9.23'
11+
ext.retrofitVersion = '2.10.0'
12+
ext.spotless_version = "6.25.0"
13+
14+
repositories {
15+
maven { url "https://repo1.maven.org/maven2" }
16+
}
17+
dependencies {
18+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
19+
classpath "com.diffplug.spotless:spotless-plugin-gradle:$spotless_version"
20+
}
21+
}
22+
23+
apply plugin: 'kotlin'
24+
apply plugin: 'maven-publish'
25+
apply plugin: 'com.diffplug.spotless'
26+
27+
repositories {
28+
maven { url "https://repo1.maven.org/maven2" }
29+
}
30+
31+
// Use spotless plugin to automatically format code, remove unused import, etc
32+
// To apply changes directly to the file, run `gradlew spotlessApply`
33+
// Ref: https://github.com/diffplug/spotless/tree/main/plugin-gradle
34+
spotless {
35+
// comment out below to run spotless as part of the `check` task
36+
enforceCheck false
37+
38+
format 'misc', {
39+
// define the files (e.g. '*.gradle', '*.md') to apply `misc` to
40+
target '.gitignore'
41+
42+
// define the steps to apply to those files
43+
trimTrailingWhitespace()
44+
indentWithSpaces() // Takes an integer argument if you don't like 4
45+
endWithNewline()
46+
}
47+
kotlin {
48+
ktfmt()
49+
}
50+
}
51+
52+
test {
53+
useJUnitPlatform()
54+
}
55+
56+
dependencies {
57+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
58+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0"
59+
implementation "com.google.code.gson:gson:2.10.1"
60+
implementation "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:1.0.2"
61+
implementation "com.squareup.okhttp3:logging-interceptor:4.12.0"
62+
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
63+
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
64+
implementation "com.squareup.retrofit2:converter-scalars:$retrofitVersion"
65+
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
66+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# ModelApiResponse
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+

0 commit comments

Comments
 (0)