Skip to content

Commit c6a100c

Browse files
authored
Added support for using mutiny instead of coroutines for asynchronous kotlin server APIs (OpenAPITools#15262)
1 parent bf18190 commit c6a100c

File tree

23 files changed

+667
-3
lines changed

23 files changed

+667
-3
lines changed

.github/workflows/samples-kotlin-server.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
- samples/server/petstore/kotlin-springboot-springfox
3030
- samples/server/petstore/kotlin-server/ktor
3131
- samples/server/petstore/kotlin-server/jaxrs-spec
32+
- samples/server/petstore/kotlin-server/jaxrs-spec-mutiny
3233
- samples/server/petstore/kotlin-server-modelMutable
3334
# no build.gradle file
3435
#- samples/server/petstore/kotlin-vertx-modelMutable
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
generatorName: kotlin-server
2+
outputDir: samples/server/petstore/kotlin-server/jaxrs-spec-mutiny
3+
library: jaxrs-spec
4+
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/kotlin-server
6+
additionalProperties:
7+
returnResponse: "true"
8+
interfaceOnly: "true"
9+
useMutiny: "true"

docs/generators/kotlin-server.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4545
|useBeanValidation|Use BeanValidation API annotations. This option is currently supported only when using jaxrs-spec library.| |false|
4646
|useCoroutines|Whether to use the Coroutines. This option is currently supported only when using jaxrs-spec library.| |false|
4747
|useJakartaEe|whether to use Jakarta EE namespace instead of javax| |false|
48+
|useMutiny|Whether to use Mutiny (should not be used with useCoroutines). This option is currently supported only when using jaxrs-spec library.| |false|
4849

4950
## IMPORT MAPPING
5051

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
4848
private boolean interfaceOnly = false;
4949
private boolean useBeanValidation = false;
5050
private boolean useCoroutines = false;
51+
private boolean useMutiny = false;
5152
private boolean returnResponse = false;
5253

5354
// This is here to potentially warn the user when an option is not supported by the target framework.
@@ -64,6 +65,7 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
6465
.put(Constants.JAXRS_SPEC, Arrays.asList(
6566
USE_BEANVALIDATION,
6667
Constants.USE_COROUTINES,
68+
Constants.USE_MUTINY,
6769
Constants.RETURN_RESPONSE,
6870
Constants.INTERFACE_ONLY
6971
))
@@ -128,6 +130,7 @@ public KotlinServerCodegen() {
128130
addSwitch(Constants.INTERFACE_ONLY, Constants.INTERFACE_ONLY_DESC, interfaceOnly);
129131
addSwitch(USE_BEANVALIDATION, Constants.USE_BEANVALIDATION_DESC, useBeanValidation);
130132
addSwitch(Constants.USE_COROUTINES, Constants.USE_COROUTINES_DESC, useCoroutines);
133+
addSwitch(Constants.USE_MUTINY, Constants.USE_MUTINY_DESC, useMutiny);
131134
addSwitch(Constants.RETURN_RESPONSE, Constants.RETURN_RESPONSE_DESC, returnResponse);
132135
addSwitch(USE_JAKARTA_EE, Constants.USE_JAKARTA_EE_DESC, useJakartaEe);
133136
}
@@ -226,6 +229,13 @@ public void processOpts() {
226229
}
227230
}
228231

232+
if (additionalProperties.containsKey(Constants.USE_MUTINY)) {
233+
useMutiny = Boolean.parseBoolean(additionalProperties.get(Constants.USE_MUTINY).toString());
234+
if (!useMutiny) {
235+
additionalProperties.remove(Constants.USE_MUTINY);
236+
}
237+
}
238+
229239
if (additionalProperties.containsKey(Constants.RETURN_RESPONSE)) {
230240
returnResponse = Boolean.parseBoolean(additionalProperties.get(Constants.RETURN_RESPONSE).toString());
231241
if (!returnResponse) {
@@ -348,6 +358,8 @@ public static class Constants {
348358
public static final String RETURN_RESPONSE = "returnResponse";
349359
public static final String RETURN_RESPONSE_DESC = "Whether generate API interface should return javax.ws.rs.core.Response instead of a deserialized entity. Only useful if interfaceOnly is true. This option is currently supported only when using jaxrs-spec library.";
350360
public static final String USE_JAKARTA_EE_DESC = "whether to use Jakarta EE namespace instead of javax";
361+
public static final String USE_MUTINY = "useMutiny";
362+
public static final String USE_MUTINY_DESC = "Whether to use Mutiny (should not be used with useCoroutines). This option is currently supported only when using jaxrs-spec library.";
351363
}
352364

353365
@Override

modules/openapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/apiInterface.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
{{/isOAuth}}{{/authMethods}} }{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-tags}} })
1111
@ApiResponses(value = { {{#responses}}
1212
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#returnContainer}}, responseContainer = "{{{.}}}"{{/returnContainer}}){{^-last}},{{/-last}}{{/responses}} }){{/useSwaggerAnnotations}}
13-
{{#useCoroutines}}suspend {{/useCoroutines}}fun {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}){{#returnResponse}}: Response{{/returnResponse}}{{^returnResponse}}{{#returnType}}: {{{returnType}}}{{/returnType}}{{/returnResponse}}
13+
{{#useCoroutines}}suspend {{/useCoroutines}}fun {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}){{#returnResponse}}: {{#useMutiny}}io.smallrye.mutiny.Uni<{{/useMutiny}}Response{{#useMutiny}}>{{/useMutiny}}{{/returnResponse}}{{^returnResponse}}{{#returnType}}: {{#useMutiny}}io.smallrye.mutiny.Uni<{{/useMutiny}}{{{returnType}}}{{#useMutiny}}>{{/useMutiny}}{{/returnType}}{{/returnResponse}}{{^returnResponse}}{{^returnType}}{{#useMutiny}}: io.smallrye.mutiny.Uni<Void>{{/useMutiny}}{{/returnType}}{{/returnResponse}}

modules/openapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/build.gradle.mustache

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

99
buildscript {
10-
ext.kotlin_version = "1.4.32"
10+
ext.kotlin_version = "1.7.20"
1111
ext.swagger_annotations_version = "1.5.3"
1212
{{#useJakartaEe}}
1313
ext.jakarta_annotations_version = "2.1.1"
@@ -17,6 +17,9 @@ buildscript {
1717
ext.jakarta_annotations_version = "1.3.5"
1818
ext.jakarta_ws_rs_version = "2.1.6"
1919
{{/useJakartaEe}}
20+
{{#useMutiny}}
21+
ext.mutiny_version = "2.2.0"
22+
{{/useMutiny}}
2023
ext.jackson_version = "2.9.9"
2124
{{#useBeanValidation}}
2225
ext.beanvalidation_version = "2.0.2"
@@ -59,5 +62,9 @@ dependencies {
5962
{{#useBeanValidation}}
6063
implementation("jakarta.validation:jakarta.validation-api:$beanvalidation_version")
6164
{{/useBeanValidation}}
65+
{{#useMutiny}}
66+
implementation("io.smallrye.reactive:mutiny:$mutiny_version")
67+
implementation("io.smallrye.reactive:mutiny-kotlin:$mutiny_version")
68+
{{/useMutiny}}
6269
testImplementation("junit:junit:4.13.2")
6370
}
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
README.md
2+
build.gradle
3+
gradle.properties
4+
settings.gradle
5+
src/main/kotlin/org/openapitools/server/apis/PetApi.kt
6+
src/main/kotlin/org/openapitools/server/apis/StoreApi.kt
7+
src/main/kotlin/org/openapitools/server/apis/UserApi.kt
8+
src/main/kotlin/org/openapitools/server/models/Category.kt
9+
src/main/kotlin/org/openapitools/server/models/ModelApiResponse.kt
10+
src/main/kotlin/org/openapitools/server/models/Order.kt
11+
src/main/kotlin/org/openapitools/server/models/Pet.kt
12+
src/main/kotlin/org/openapitools/server/models/Tag.kt
13+
src/main/kotlin/org/openapitools/server/models/User.kt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.0.0-SNAPSHOT
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# org.openapitools.server - Kotlin Server library for OpenAPI Petstore
2+
3+
## Requires
4+
5+
* Kotlin 1.4.31
6+
* Gradle 6.8.2
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+
30+
<a id="documentation-for-api-endpoints"></a>
31+
## Documentation for API Endpoints
32+
33+
All URIs are relative to *http://petstore.swagger.io/v2*
34+
35+
Class | Method | HTTP request | Description
36+
------------ | ------------- | ------------- | -------------
37+
*PetApi* | [**addPet**](docs/PetApi.md#addpet) | **POST** /pet | Add a new pet to the store
38+
*PetApi* | [**deletePet**](docs/PetApi.md#deletepet) | **DELETE** /pet/{petId} | Deletes a pet
39+
*PetApi* | [**findPetsByStatus**](docs/PetApi.md#findpetsbystatus) | **GET** /pet/findByStatus | Finds Pets by status
40+
*PetApi* | [**findPetsByTags**](docs/PetApi.md#findpetsbytags) | **GET** /pet/findByTags | Finds Pets by tags
41+
*PetApi* | [**getPetById**](docs/PetApi.md#getpetbyid) | **GET** /pet/{petId} | Find pet by ID
42+
*PetApi* | [**updatePet**](docs/PetApi.md#updatepet) | **PUT** /pet | Update an existing pet
43+
*PetApi* | [**updatePetWithForm**](docs/PetApi.md#updatepetwithform) | **POST** /pet/{petId} | Updates a pet in the store with form data
44+
*PetApi* | [**uploadFile**](docs/PetApi.md#uploadfile) | **POST** /pet/{petId}/uploadImage | uploads an image
45+
*StoreApi* | [**deleteOrder**](docs/StoreApi.md#deleteorder) | **DELETE** /store/order/{orderId} | Delete purchase order by ID
46+
*StoreApi* | [**getInventory**](docs/StoreApi.md#getinventory) | **GET** /store/inventory | Returns pet inventories by status
47+
*StoreApi* | [**getOrderById**](docs/StoreApi.md#getorderbyid) | **GET** /store/order/{orderId} | Find purchase order by ID
48+
*StoreApi* | [**placeOrder**](docs/StoreApi.md#placeorder) | **POST** /store/order | Place an order for a pet
49+
*UserApi* | [**createUser**](docs/UserApi.md#createuser) | **POST** /user | Create user
50+
*UserApi* | [**createUsersWithArrayInput**](docs/UserApi.md#createuserswitharrayinput) | **POST** /user/createWithArray | Creates list of users with given input array
51+
*UserApi* | [**createUsersWithListInput**](docs/UserApi.md#createuserswithlistinput) | **POST** /user/createWithList | Creates list of users with given input array
52+
*UserApi* | [**deleteUser**](docs/UserApi.md#deleteuser) | **DELETE** /user/{username} | Delete user
53+
*UserApi* | [**getUserByName**](docs/UserApi.md#getuserbyname) | **GET** /user/{username} | Get user by user name
54+
*UserApi* | [**loginUser**](docs/UserApi.md#loginuser) | **GET** /user/login | Logs user into the system
55+
*UserApi* | [**logoutUser**](docs/UserApi.md#logoutuser) | **GET** /user/logout | Logs out current logged in user session
56+
*UserApi* | [**updateUser**](docs/UserApi.md#updateuser) | **PUT** /user/{username} | Updated user
57+
58+
59+
<a id="documentation-for-models"></a>
60+
## Documentation for Models
61+
62+
- [org.openapitools.server.models.Category](docs/Category.md)
63+
- [org.openapitools.server.models.ModelApiResponse](docs/ModelApiResponse.md)
64+
- [org.openapitools.server.models.Order](docs/Order.md)
65+
- [org.openapitools.server.models.Pet](docs/Pet.md)
66+
- [org.openapitools.server.models.Tag](docs/Tag.md)
67+
- [org.openapitools.server.models.User](docs/User.md)
68+
69+
70+
<a id="documentation-for-authorization"></a>
71+
## Documentation for Authorization
72+
73+
74+
Authentication schemes defined for the API:
75+
<a id="petstore_auth"></a>
76+
### petstore_auth
77+
78+
- **Type**: OAuth
79+
- **Flow**: implicit
80+
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
81+
- **Scopes**:
82+
- write:pets: modify pets in your account
83+
- read:pets: read your pets
84+
85+
<a id="api_key"></a>
86+
### api_key
87+
88+
- **Type**: API key
89+
- **API key parameter name**: api_key
90+
- **Location**: HTTP header
91+

0 commit comments

Comments
 (0)