Skip to content

Commit

Permalink
Added Kotlin Vert.x client (OpenAPITools#13594)
Browse files Browse the repository at this point in the history
* Initial version of Kotlin Vert.x client

* Initial version of Kotlin Vert.x client

* Initial version of Kotlin Vert.x client

* Fix for parseDateToQueryString issue in vert.x kotlin client

* Moved common methods from api to ApiClient in kotlin vert.x client

* Fixed issue with absolute URLs

* bearer auth for oauth

* empty request headers fix

* missing import and typo

* added uri template dependency

* added api abstractions to client generator

* added full import form infrastructure

* removed fail on unknown properties to response body parsing

* fixed error response parsing

* replace vertx client name to more unique

* multiline content type

* optional responses added to template

* additional annotations for kotlin client

* Added additionalModelTypeAnnotations parameter support to AbstractKotlinCodegen

* Updated samples and documents

* Fixed issues with gson and moshi serializers with kotlin-jvm-vertx client

* Added sample configs for kotlin-jvm-vertx clients with gson, jackson and moshi

* Added samples for kotlin-jvm-vertx clients with gson, jackson and moshi

* Included kotlin-jvm-vertx samples to test build

* Updated samples

Co-authored-by: Katja Danilova <[email protected]>
  • Loading branch information
anttileppa and kaeline authored Oct 19, 2022
1 parent 042e250 commit 11c43c3
Show file tree
Hide file tree
Showing 289 changed files with 15,132 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/samples-kotlin-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ jobs:
- samples/client/petstore/kotlin-bigdecimal-default-okhttp4
- samples/client/petstore/kotlin-jvm-ktor-jackson
- samples/client/petstore/kotlin-jvm-ktor-gson
- samples/client/petstore/kotlin-jvm-ktor-gson
- samples/client/petstore/kotlin-jvm-vertx-gson
- samples/client/petstore/kotlin-jvm-vertx-jackson
- samples/client/petstore/kotlin-jvm-vertx-jackson-coroutines
- samples/client/petstore/kotlin-jvm-vertx-moshi
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
Expand Down
8 changes: 8 additions & 0 deletions bin/configs/kotlin-jvm-vertx-gson.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-jvm-vertx-gson
library: jvm-vertx
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-petstore-jvm-vertx
serializationLibrary: "gson"
9 changes: 9 additions & 0 deletions bin/configs/kotlin-jvm-vertx-jackson-coroutines.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-jvm-vertx-jackson-coroutines
library: jvm-vertx
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-petstore-jvm-vertx
serializationLibrary: "jackson"
useCoroutines: "true"
8 changes: 8 additions & 0 deletions bin/configs/kotlin-jvm-vertx-jackson.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-jvm-vertx-jackson
library: jvm-vertx
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-petstore-jvm-vertx
serializationLibrary: "jackson"
8 changes: 8 additions & 0 deletions bin/configs/kotlin-jvm-vertx-moshi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-jvm-vertx-moshi
library: jvm-vertx
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-petstore-jvm-vertx
serializationLibrary: "moshi"
1 change: 1 addition & 0 deletions docs/generators/kotlin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl

| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
|apiSuffix|suffix for api classes| |Api|
|artifactId|Generated artifact id (name of jar).| |kotlin-server|
|artifactVersion|Generated artifact's package version.| |1.0.0|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/kotlin-spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl

| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
|annotationLibrary|Select the complementary documentation annotation library.|<dl><dt>**none**</dt><dd>Do not annotate Model and Api with complementary annotations.</dd><dt>**swagger1**</dt><dd>Annotate Model and Api using the Swagger Annotations 1.x library.</dd><dt>**swagger2**</dt><dd>Annotate Model and Api using the Swagger Annotations 2.x library.</dd></dl>|swagger2|
|apiPackage|api package for generated code| |org.openapitools.api|
|apiSuffix|suffix for api classes| |Api|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/kotlin-vertx.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl

| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
|apiSuffix|suffix for api classes| |Api|
|artifactId|Generated artifact id (name of jar).| |null|
|artifactVersion|Generated artifact's package version.| |1.0.0|
Expand Down
3 changes: 2 additions & 1 deletion docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl

| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
|apiSuffix|suffix for api classes| |Api|
|artifactId|Generated artifact id (name of jar).| |kotlin-client|
|artifactVersion|Generated artifact's package version.| |1.0.0|
Expand All @@ -27,7 +28,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|generateRoomModels|Generate Android Room database models in addition to API models (JVM Volley library only)| |false|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|idea|Add IntellJ Idea plugin and mark Kotlin main and test folders as source folders.| |false|
|library|Library template (sub-template) to use|<dl><dt>**jvm-ktor**</dt><dd>Platform: Java Virtual Machine. HTTP client: Ktor 1.6.7. JSON processing: Gson, Jackson (default).</dd><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.6.7. JSON processing: Kotlinx Serialization: 1.2.1.</dd><dt>**jvm-volley**</dt><dd>Platform: JVM for Android. HTTP client: Volley 1.2.1. JSON processing: gson 2.8.9</dd></dl>|jvm-okhttp4|
|library|Library template (sub-template) to use|<dl><dt>**jvm-ktor**</dt><dd>Platform: Java Virtual Machine. HTTP client: Ktor 1.6.7. JSON processing: Gson, Jackson (default).</dd><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.6.7. JSON processing: Kotlinx Serialization: 1.2.1.</dd><dt>**jvm-volley**</dt><dd>Platform: JVM for Android. HTTP client: Volley 1.2.1. JSON processing: gson 2.8.9</dd><dt>**jvm-vertx**</dt><dd>Platform: Java Virtual Machine. HTTP client: Vert.x Web Client. JSON processing: Moshi, Gson or Jackson.</dd></dl>|jvm-okhttp4|
|modelMutable|Create mutable models| |false|
|moshiCodeGen|Whether to enable codegen with the Moshi library. Refer to the [official Moshi doc](https://github.com/square/moshi#codegen) for more info.| |false|
|omitGradlePluginVersions|Whether to declare Gradle plugin versions in build files.| |false|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/ktorm-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|addSurrogateKey|Adds the surrogate key for all models that don't already have a primary key (named by the above convention)| |false|
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
|artifactId|Generated artifact id (name of jar).| |ktorm|
|artifactVersion|Generated artifact's package version.| |1.0.0|
|defaultDatabaseName|Default database name for all queries| |sqlite.db|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public enum SERIALIZATION_LIBRARY_TYPE {moshi, gson, jackson, kotlinx_serializat

public static final String MODEL_MUTABLE = "modelMutable";
public static final String MODEL_MUTABLE_DESC = "Create mutable models";
public static final String ADDITIONAL_MODEL_TYPE_ANNOTATIONS = "additionalModelTypeAnnotations";

private final Logger LOGGER = LoggerFactory.getLogger(AbstractKotlinCodegen.class);

Expand Down Expand Up @@ -77,6 +78,7 @@ public enum SERIALIZATION_LIBRARY_TYPE {moshi, gson, jackson, kotlinx_serializat
protected Set<String> propertyAdditionalKeywords = new HashSet<>(Arrays.asList("entries", "keys", "size", "values"));

private final Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
protected List<String> additionalModelTypeAnnotations = new LinkedList<>();

public AbstractKotlinCodegen() {
super();
Expand Down Expand Up @@ -262,6 +264,7 @@ public AbstractKotlinCodegen() {
cliOptions.add(new CliOption(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG_DESC));

cliOptions.add(CliOption.newBoolean(MODEL_MUTABLE, MODEL_MUTABLE_DESC, false));
cliOptions.add(CliOption.newString(ADDITIONAL_MODEL_TYPE_ANNOTATIONS, "Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)"));
}

@Override
Expand Down Expand Up @@ -398,6 +401,21 @@ public String modelFileFolder() {
return outputFolder + File.separator + sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar);
}

@Override
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
objs = super.postProcessAllModels(objs);
objs = super.updateAllModels(objs);

if (!additionalModelTypeAnnotations.isEmpty()) {
for (String modelName : objs.keySet()) {
Map<String, Object> models = (Map<String, Object>) objs.get(modelName);
models.put(ADDITIONAL_MODEL_TYPE_ANNOTATIONS, additionalModelTypeAnnotations);
}
}

return objs;
}

@Override
public ModelsMap postProcessModels(ModelsMap objs) {
objs = super.postProcessModelsEnum(objs);
Expand Down Expand Up @@ -506,6 +524,11 @@ public void processOpts() {
additionalProperties.put(CodegenConstants.NON_PUBLIC_API, nonPublicApi);
}

if (additionalProperties.containsKey(ADDITIONAL_MODEL_TYPE_ANNOTATIONS)) {
String additionalAnnotationsList = additionalProperties.get(ADDITIONAL_MODEL_TYPE_ANNOTATIONS).toString();
this.setAdditionalModelTypeAnnotations(Arrays.asList(additionalAnnotationsList.trim().split("\\s*(;|\\r?\\n)\\s*")));
}

additionalProperties.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, getSortParamsByRequiredFlag());
additionalProperties.put(CodegenConstants.SORT_MODEL_PROPERTIES_BY_REQUIRED_FLAG, getSortModelPropertiesByRequiredFlag());

Expand Down Expand Up @@ -1095,4 +1118,12 @@ protected void updateModelForObject(CodegenModel m, Schema schema) {
// process 'additionalProperties'
setAddProps(schema, m);
}

public List<String> getAdditionalModelTypeAnnotations() {
return additionalModelTypeAnnotations;
}

public void setAdditionalModelTypeAnnotations(final List<String> additionalModelTypeAnnotations) {
this.additionalModelTypeAnnotations = additionalModelTypeAnnotations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
protected static final String JVM_RETROFIT2 = "jvm-retrofit2";
protected static final String MULTIPLATFORM = "multiplatform";
protected static final String JVM_VOLLEY = "jvm-volley";
protected static final String JVM_VERTX = "jvm-vertx";

public static final String USE_RX_JAVA = "useRxJava";
public static final String USE_RX_JAVA2 = "useRxJava2";
Expand Down Expand Up @@ -215,6 +216,7 @@ public KotlinClientCodegen() {
supportedLibraries.put(JVM_RETROFIT2, "Platform: Java Virtual Machine. HTTP client: Retrofit 2.6.2.");
supportedLibraries.put(MULTIPLATFORM, "Platform: Kotlin multiplatform. HTTP client: Ktor 1.6.7. JSON processing: Kotlinx Serialization: 1.2.1.");
supportedLibraries.put(JVM_VOLLEY, "Platform: JVM for Android. HTTP client: Volley 1.2.1. JSON processing: gson 2.8.9");
supportedLibraries.put(JVM_VERTX, "Platform: Java Virtual Machine. HTTP client: Vert.x Web Client. JSON processing: Moshi, Gson or Jackson.");

CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "Library template (sub-template) to use");
libraryOption.setEnum(supportedLibraries);
Expand Down Expand Up @@ -441,6 +443,9 @@ public void processOpts() {
case MULTIPLATFORM:
processMultiplatformLibrary(infrastructureFolder);
break;
case JVM_VERTX:
processJVMVertXLibrary(infrastructureFolder);
break;
default:
break;
}
Expand Down Expand Up @@ -666,6 +671,22 @@ private void processJVMKtorLibrary(final String infrastructureFolder) {
supportingFiles.add(new SupportingFile("auth/OAuth.kt.mustache", authFolder, "OAuth.kt"));
}

/**
* Process Vert.x client options
*
* @param infrastructureFolder infrastructure destination folder
*/
private void processJVMVertXLibrary(final String infrastructureFolder) {
supportingFiles.add(new SupportingFile("infrastructure/ApiAbstractions.kt.mustache", infrastructureFolder, "ApiAbstractions.kt"));
supportingFiles.add(new SupportingFile("infrastructure/ApiClient.kt.mustache", infrastructureFolder, "ApiClient.kt"));
supportingFiles.add(new SupportingFile("infrastructure/Errors.kt.mustache", infrastructureFolder, "Errors.kt"));
supportingFiles.add(new SupportingFile("infrastructure/ApiResponse.kt.mustache", infrastructureFolder, "ApiResponse.kt"));
addSupportingSerializerAdapters(infrastructureFolder);

additionalProperties.put(JVM, true);
additionalProperties.put(JVM_VERTX, true);
}

private void processJVMOkHttpLibrary(final String infrastructureFolder) {
commonJvmMultiplatformSupportingFiles(infrastructureFolder);
addSupportingSerializerAdapters(infrastructureFolder);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{#additionalModelTypeAnnotations}}{{{.}}}
{{/additionalModelTypeAnnotations}}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ buildscript {
{{#useRxJava3}}
ext.rxJava3Version = '3.0.12'
{{/useRxJava3}}
{{#jvm-vertx}}
ext.vertx_version = "4.3.3"
{{/jvm-vertx}}

repositories {
maven { url "https://repo1.maven.org/maven2" }
Expand Down Expand Up @@ -149,6 +152,15 @@ dependencies {
implementation "com.squareup.retrofit2:converter-scalars:$retrofitVersion"
{{/jvm-retrofit2}}
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
{{#jvm-vertx}}
implementation "io.vertx:vertx-web-client:$vertx_version"
implementation "io.vertx:vertx-core:$vertx_version"
implementation "io.vertx:vertx-lang-kotlin:$vertx_version"
implementation "io.vertx:vertx-uri-template:$vertx_version"
{{#useCoroutines}}
implementation "io.vertx:vertx-lang-kotlin-coroutines:$vertx_version"
{{/useCoroutines}}
{{/jvm-vertx}}
}
{{#kotlinx_serialization}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {{packageName}}.infrastructure.ITransformForStorage
{{#isDeprecated}}
@Deprecated(message = "This schema is deprecated.")
{{/isDeprecated}}
{{>additionalModelTypeAnnotations}}
{{#nonPublicApi}}internal {{/nonPublicApi}}{{#discriminator}}interface{{/discriminator}}{{^discriminator}}data class{{/discriminator}} {{classname}}{{^discriminator}} (

{{#allVars}}
Expand Down
Loading

0 comments on commit 11c43c3

Please sign in to comment.