Skip to content
Permalink
Browse files

[Kotlin] client improvement / remove WildCards in client/server (#2862)

  • Loading branch information...
karismann authored and wing328 committed May 11, 2019
1 parent 9323cad commit ef26ce68d41ab7da3ce70dfa290022d7ba2112b4
Showing with 660 additions and 344 deletions.
  1. +3 −1 .gitignore
  2. +3 −3 docs/generators/kotlin-server.md
  3. +2 −2 docs/generators/kotlin-spring.md
  4. +4 −4 docs/generators/kotlin.md
  5. +1 −1 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java
  6. +6 −5 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java
  7. +12 −7 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinServerCodegen.java
  8. +4 −1 ...openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java
  9. +1 −1 modules/openapi-generator/src/main/resources/kotlin-client/README.mustache
  10. +12 −2 modules/openapi-generator/src/main/resources/kotlin-client/api.mustache
  11. +3 −3 modules/openapi-generator/src/main/resources/kotlin-client/build.gradle.mustache
  12. +51 −36 modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache
  13. +1 −1 modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/RequestConfig.kt.mustache
  14. +9 −2 modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/ApiKeyAuth.kt.mustache
  15. +22 −15 modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/AppMain.kt.mustache
  16. +6 −2 modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/Configuration.kt.mustache
  17. +3 −5 modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/Paths.kt.mustache
  18. +7 −2 modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor/api.mustache
  19. +14 −2 modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache
  20. +7 −2 modules/openapi-generator/src/main/resources/kotlin-spring/model.mustache
  21. +1 −1 samples/client/petstore/kotlin-string/README.md
  22. +3 −3 samples/client/petstore/kotlin-string/build.gradle
  23. +19 −9 samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/PetApi.kt
  24. +15 −5 samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt
  25. +19 −9 samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/apis/UserApi.kt
  26. +51 −36 ...client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt
  27. +1 −1 ...nt/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
  28. +1 −1 samples/client/petstore/kotlin-threetenbp/README.md
  29. +3 −3 samples/client/petstore/kotlin-threetenbp/build.gradle
  30. +19 −9 samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt
  31. +15 −5 samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt
  32. +19 −9 samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/UserApi.kt
  33. +51 −36 ...nt/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt
  34. +1 −1 ...etstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
  35. +1 −1 samples/client/petstore/kotlin/README.md
  36. +3 −3 samples/client/petstore/kotlin/build.gradle
  37. +19 −9 samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/PetApi.kt
  38. +15 −5 samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt
  39. +19 −9 samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/apis/UserApi.kt
  40. +51 −36 samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt
  41. +1 −1 ...es/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
  42. +17 −14 samples/client/petstore/kotlin/src/test/kotlin/org/openapitools/client/PetApiTest.kt
  43. +23 −10 samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/AppMain.kt
  44. +6 −2 samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/Configuration.kt
  45. +2 −3 samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/Paths.kt
  46. +7 −2 samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/PetApi.kt
  47. +7 −2 samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/StoreApi.kt
  48. +7 −2 samples/server/petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/apis/UserApi.kt
  49. +9 −2 .../petstore/kotlin-server/ktor/src/main/kotlin/org/openapitools/server/infrastructure/ApiKeyAuth.kt
  50. +14 −2 samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/PetApi.kt
  51. +14 −2 samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/StoreApi.kt
  52. +14 −2 samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/UserApi.kt
  53. +7 −2 samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/model/Category.kt
  54. +7 −2 samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/model/ModelApiResponse.kt
  55. +7 −2 samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/model/Order.kt
  56. +7 −2 samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/model/Pet.kt
  57. +7 −2 samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/model/Tag.kt
  58. +7 −2 samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/model/User.kt
@@ -175,12 +175,14 @@ samples/client/petstore/typescript-angular/tsd-debug.log
# aspnetcore
samples/server/petstore/aspnetcore/.vs/
effective.pom

# kotlin
samples/client/petstore/kotlin/src/main/kotlin/test/
samples/client/petstore/kotlin-threetenbp/build
samples/client/petstore/kotlin-string/build
samples/server/petstore/kotlin-server/ktor/build
samples/openapi3/client/petstore/kotlin/build
samples/server/petstore/kotlin-server/ktor/build
samples/server/petstore/kotlin-springboot/build
\?

# haskell
@@ -8,14 +8,14 @@ sidebar_label: kotlin-server
| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|sourceFolder|source folder for generated code| |src/main/kotlin|
|packageName|Generated artifact package name.| |org.openapitools|
|packageName|Generated artifact package name.| |org.openapitools.server|
|apiSuffix|suffix for api classes| |Api|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|artifactId|Generated artifact id (name of jar).| |null|
|artifactId|Generated artifact id (name of jar).| |kotlin-server|
|artifactVersion|Generated artifact's package version.| |1.0.0|
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |camelCase|
|parcelizeModels|toggle "@Parcelize" for generated models| |null|
|library|library template (sub-template) to use|<dl><dt>**ktor**</dt><dd>ktor framework</dd><dl>|ktor|
|library|library template (sub-template)|<dl><dt>**ktor**</dt><dd>ktor framework</dd><dl>|ktor|
|featureAutoHead|Automatically provide responses to HEAD requests for existing routes that have the GET verb defined.| |true|
|featureConditionalHeaders|Avoid sending content if client already has same content, by checking ETag or LastModified properties.| |false|
|featureHSTS|Avoid sending content if client already has same content, by checking ETag or LastModified properties.| |true|
@@ -11,7 +11,7 @@ sidebar_label: kotlin-spring
|packageName|Generated artifact package name.| |org.openapitools|
|apiSuffix|suffix for api classes| |Api|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|artifactId|Generated artifact id (name of jar).| |null|
|artifactId|Generated artifact id (name of jar).| |openapi-spring|
|artifactVersion|Generated artifact's package version.| |1.0.0|
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |camelCase|
|parcelizeModels|toggle &quot;@Parcelize&quot; for generated models| |null|
@@ -26,4 +26,4 @@ sidebar_label: kotlin-spring
|serviceInterface|generate service interfaces to go alongside controllers. In most cases this option would be used to update an existing project, so not to override implementations. Useful to help facilitate the generation gap pattern| |false|
|serviceImplementation|generate stub service implementations that extends service interfaces. If this is set to true service interfaces will also be generated| |false|
|useBeanValidation|Use BeanValidation API annotations to validate data types| |true|
|library|library template (sub-template) to use|<dl><dt>**spring-boot**</dt><dd>Spring-boot Server application.</dd><dl>|spring-boot|
|library|library template (sub-template)|<dl><dt>**spring-boot**</dt><dd>Spring-boot Server application.</dd><dl>|spring-boot|
@@ -8,12 +8,12 @@ sidebar_label: kotlin
| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|sourceFolder|source folder for generated code| |src/main/kotlin|
|packageName|Generated artifact package name.| |org.openapitools|
|packageName|Generated artifact package name.| |org.openapitools.client|
|apiSuffix|suffix for api classes| |Api|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|artifactId|Generated artifact id (name of jar).| |null|
|artifactId|Generated artifact id (name of jar).| |kotlin-client|
|artifactVersion|Generated artifact's package version.| |1.0.0|
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |camelCase|
|parcelizeModels|toggle &quot;@Parcelize&quot; for generated models| |null|
|dateLibrary|Option. Date library to use|<dl><dt>**string**</dt><dd>String</dd><dt>**java8**</dt><dd>Java 8 native JSR310</dd><dt>**threetenbp**</dt><dd>Threetenbp</dd><dl>|null|
|collectionType|Option. Collection type to use|<dl><dt>**array**</dt><dd>kotlin.Array</dd><dt>**list**</dt><dd>kotlin.collections.List</dd><dl>|null|
|dateLibrary|Option. Date library to use|<dl><dt>**string**</dt><dd>String</dd><dt>**java8**</dt><dd>Java 8 native JSR310</dd><dt>**threetenbp**</dt><dd>Threetenbp</dd><dl>|java8|
|collectionType|Option. Collection type to use|<dl><dt>**array**</dt><dd>kotlin.Array</dd><dt>**list**</dt><dd>kotlin.collections.List</dd><dl>|array|
@@ -102,7 +102,7 @@ public JavaClientCodegen() {
apiPackage = "org.openapitools.client.api";
modelPackage = "org.openapitools.client.model";

// clioOptions default redifinition need to be updated
// cliOptions default redefinition need to be updated
updateOption(CodegenConstants.INVOKER_PACKAGE, this.getInvokerPackage());
updateOption(CodegenConstants.ARTIFACT_ID, this.getArtifactId());
updateOption(CodegenConstants.API_PACKAGE, apiPackage);
@@ -21,8 +21,6 @@
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
@@ -32,7 +30,6 @@

public static final String DATE_LIBRARY = "dateLibrary";
public static final String COLLECTION_TYPE = "collectionType";
private static final Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class);

protected String dateLibrary = DateLibrary.JAVA8.value;
protected String collectionType = CollectionType.ARRAY.value;
@@ -69,6 +66,10 @@ public KotlinClientCodegen() {
artifactId = "kotlin-client";
packageName = "org.openapitools.client";

// cliOptions default redefinition need to be updated
updateOption(CodegenConstants.ARTIFACT_ID, this.artifactId);
updateOption(CodegenConstants.PACKAGE_NAME, this.packageName);

outputFolder = "generated-code" + File.separator + "kotlin-client";
modelTemplateFiles.put("model.mustache", ".kt");
apiTemplateFiles.put("api.mustache", ".kt");
@@ -78,21 +79,21 @@ public KotlinClientCodegen() {
apiPackage = packageName + ".apis";
modelPackage = packageName + ".models";

enumPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase;

CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
Map<String, String> dateOptions = new HashMap<>();
dateOptions.put(DateLibrary.THREETENBP.value, "Threetenbp");
dateOptions.put(DateLibrary.STRING.value, "String");
dateOptions.put(DateLibrary.JAVA8.value, "Java 8 native JSR310");
dateLibrary.setEnum(dateOptions);
dateLibrary.setDefault(this.dateLibrary);
cliOptions.add(dateLibrary);

CliOption collectionType = new CliOption(COLLECTION_TYPE, "Option. Collection type to use");
Map<String, String> collectionOptions = new HashMap<>();
collectionOptions.put(CollectionType.ARRAY.value, "kotlin.Array");
collectionOptions.put(CollectionType.LIST.value, "kotlin.collections.List");
collectionType.setEnum(collectionOptions);
collectionType.setDefault(this.collectionType);
cliOptions.add(collectionType);
}

@@ -62,17 +62,22 @@ public KotlinServerCodegen() {

artifactId = "kotlin-server";
packageName = "org.openapitools.server";

// cliOptions default redefinition need to be updated
updateOption(CodegenConstants.ARTIFACT_ID, this.artifactId);
updateOption(CodegenConstants.PACKAGE_NAME, this.packageName);

outputFolder = "generated-code" + File.separator + "kotlin-server";
modelTemplateFiles.put("model.mustache", ".kt");
apiTemplateFiles.put("api.mustache", ".kt");
embeddedTemplateDir = templateDir = "kotlin-server";
apiPackage = packageName + ".apis";
modelPackage = packageName + ".models";

supportedLibraries.put("ktor", "ktor framework");
supportedLibraries.put(Constants.KTOR, "ktor framework");

// TODO: Configurable server engine. Defaults to netty in build.gradle.
CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
CliOption library = new CliOption(CodegenConstants.LIBRARY, CodegenConstants.LIBRARY_DESC);
library.setDefault(DEFAULT_LIBRARY);
library.setEnum(supportedLibraries);

@@ -147,9 +152,9 @@ public void processOpts() {

// set default library to "ktor"
if (StringUtils.isEmpty(library)) {
this.setLibrary("ktor");
additionalProperties.put(CodegenConstants.LIBRARY, "ktor");
LOGGER.info("`library` option is empty. Default to 'ktor'.");
this.setLibrary(DEFAULT_LIBRARY);
additionalProperties.put(CodegenConstants.LIBRARY, DEFAULT_LIBRARY);
LOGGER.info("`library` option is empty. Default to " + DEFAULT_LIBRARY);
}

if (additionalProperties.containsKey(Constants.AUTOMATIC_HEAD_REQUESTS)) {
@@ -182,7 +187,7 @@ public void processOpts() {
additionalProperties.put(Constants.COMPRESSION, getCompressionFeatureEnabled());
}

Boolean generateApis = additionalProperties.containsKey(CodegenConstants.GENERATE_APIS) && (Boolean)additionalProperties.get(CodegenConstants.GENERATE_APIS);
boolean generateApis = additionalProperties.containsKey(CodegenConstants.GENERATE_APIS) && (Boolean)additionalProperties.get(CodegenConstants.GENERATE_APIS);
String packageFolder = (sourceFolder + File.separator + packageName).replace(".", File.separator);
String resourcesFolder = "src/main/resources"; // not sure this can be user configurable.

@@ -226,7 +231,7 @@ private void addMustacheLambdas(Map<String, Object> objs) {
if (objs.containsKey("lambda")) {
LOGGER.warn("A property named 'lambda' already exists. Mustache lambdas renamed from 'lambda' to '_lambda'. " +
"You'll likely need to use a custom template, " +
"see https://github.com/swagger-api/swagger-codegen#modifying-the-client-library-format. "); // TODO: update the URL
"see https://github.com/OpenAPITools/openapi-generator/blob/master/docs/templating.md. ");
objs.put("_lambda", lambdas);
} else {
objs.put("lambda", lambdas);
@@ -87,6 +87,9 @@ public KotlinSpringServerCodegen() {
apiPackage = "org.openapitools.api";
modelPackage = "org.openapitools.model";

// cliOptions default redefinition need to be updated
updateOption(CodegenConstants.ARTIFACT_ID, this.artifactId);

// Use lists instead of arrays
typeMapping.put("array", "List");
typeMapping.put("string", "String");
@@ -149,7 +152,7 @@ public KotlinSpringServerCodegen() {
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
setLibrary(SPRING_BOOT);

CliOption cliOpt = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
CliOption cliOpt = new CliOption(CodegenConstants.LIBRARY, CodegenConstants.LIBRARY_DESC);
cliOpt.setDefault(SPRING_BOOT);
cliOpt.setEnum(supportedLibraries);
cliOptions.add(cliOpt);
@@ -2,7 +2,7 @@

## Requires

* Kotlin 1.3.20
* Kotlin 1.3.31
* Gradle 4.9

## Build
@@ -4,7 +4,17 @@ package {{apiPackage}}
{{#imports}}import {{import}}
{{/imports}}

import {{packageName}}.infrastructure.*
import {{packageName}}.infrastructure.ApiClient
import {{packageName}}.infrastructure.ClientException
import {{packageName}}.infrastructure.ClientError
import {{packageName}}.infrastructure.ServerException
import {{packageName}}.infrastructure.ServerError
import {{packageName}}.infrastructure.MultiValueMap
import {{packageName}}.infrastructure.RequestConfig
import {{packageName}}.infrastructure.RequestMethod
import {{packageName}}.infrastructure.ResponseType
import {{packageName}}.infrastructure.Success
import {{packageName}}.infrastructure.toMultiValue
{{#threetenbp}}
import org.threeten.bp.LocalDateTime
{{/threetenbp}}
@@ -23,7 +33,7 @@ class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(base
fun {{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}} {
val localVariableBody: kotlin.Any? = {{#hasBodyParam}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}null{{/hasFormParams}}{{#hasFormParams}}mapOf({{#formParams}}"{{{baseName}}}" to "${{paramName}}"{{#hasMore}}, {{/hasMore}}{{/formParams}}){{/hasFormParams}}{{/hasBodyParam}}
val localVariableQuery: MultiValueMap = {{^hasQueryParams}}mapOf(){{/hasQueryParams}}{{#hasQueryParams}}mapOf({{#queryParams}}"{{paramName}}" to {{#isContainer}}toMultiValue({{paramName}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf("${{paramName}}"){{/isContainer}}{{#hasMore}}, {{/hasMore}}{{/queryParams}}){{/hasQueryParams}}
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf({{#hasFormParams}}"Content-Type" to "multipart/form-data"{{/hasFormParams}}{{^hasHeaderParams}}){{/hasHeaderParams}}{{#hasHeaderParams}}{{#hasFormParams}}, {{/hasFormParams}}{{#headerParams}}"{{baseName}}" to {{#isContainer}}{{paramName}}.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}{{paramName}}.toString(){{/isContainer}}{{#hasMore}}, {{/hasMore}}{{/headerParams}}){{/hasHeaderParams}}
val localVariableHeaders: MutableMap<String, String> = mutableMapOf({{#hasFormParams}}"Content-Type" to {{^consumes}}"multipart/form-data"{{/consumes}}{{#consumes.0}}"{{MediaType}}"{{/consumes.0}}{{/hasFormParams}}{{^hasHeaderParams}}){{/hasHeaderParams}}{{#hasHeaderParams}}{{#hasFormParams}}, {{/hasFormParams}}{{#headerParams}}"{{baseName}}" to {{#isContainer}}{{paramName}}.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}{{paramName}}.toString(){{/isContainer}}{{#hasMore}}, {{/hasMore}}{{/headerParams}}){{/hasHeaderParams}}
val localVariableConfig = RequestConfig(
RequestMethod.{{httpMethod}},
"{{path}}"{{#pathParams}}.replace("{"+"{{baseName}}"+"}", "${{paramName}}"){{/pathParams}},
@@ -7,7 +7,7 @@ wrapper {
}

buildscript {
ext.kotlin_version = '1.3.20'
ext.kotlin_version = '1.3.31'

repositories {
mavenCentral()
@@ -32,7 +32,7 @@ dependencies {
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "com.squareup.moshi:moshi-kotlin:1.5.0"
compile "com.squareup.moshi:moshi-adapters:1.5.0"
compile "com.squareup.okhttp3:okhttp:3.8.0"
compile "org.threeten:threetenbp:1.3.6"
compile "com.squareup.okhttp3:okhttp:3.14.0"
compile "org.threeten:threetenbp:1.3.8"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.1.0"
}
Oops, something went wrong.

0 comments on commit ef26ce6

Please sign in to comment.
You can’t perform that action at this time.