Skip to content

Commit

Permalink
Merge branch 'master' into issues/1054
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMario authored Apr 5, 2023
2 parents fa4c33b + 78ed26c commit 44ea4b2
Show file tree
Hide file tree
Showing 23 changed files with 173 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.swagger.codegen.v3.generators.kotlin;

import com.github.jknack.handlebars.helper.ConditionalHelpers;
import io.swagger.codegen.v3.CliOption;
import io.swagger.codegen.v3.CodegenConstants;
import io.swagger.codegen.v3.CodegenModel;
Expand All @@ -9,6 +10,7 @@
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.jknack.handlebars.helper.StringHelpers;
Expand Down Expand Up @@ -182,6 +184,7 @@ public AbstractKotlinCodegen() {
typeMapping.put("binary", "kotlin.Array<kotlin.Byte>");
typeMapping.put("Date", "java.time.LocalDate");
typeMapping.put("DateTime", "java.time.LocalDateTime");
typeMapping.put("ByteArray", "kotlin.ByteArray");

instantiationTypes.put("array", "arrayOf");
instantiationTypes.put("list", "arrayOf");
Expand Down Expand Up @@ -508,10 +511,16 @@ public String toVarName(String name) {
return super.toVarName(sanitizeKotlinSpecificNames(name));
}

@Override
public String toEnumName(CodegenProperty property) {
return StringUtils.capitalize(property.name);
}

@Override
public void addHandlebarHelpers(Handlebars handlebars) {
super.addHandlebarHelpers(handlebars);
handlebars.registerHelpers(StringHelpers.class);
handlebars.registerHelpers(ConditionalHelpers.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ public KotlinClientCodegen() {
cliOptions.add(dateLibrary);
}

@Override
public void addHandlebarHelpers(Handlebars handlebars) {
super.addHandlebarHelpers(handlebars);
handlebars.registerHelpers(ConditionalHelpers.class);
}

@Override
public String getDefaultTemplateDir() {
return "kotlin-client";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public PhpClientCodegen() {

instantiationTypes.put("array", "array");
instantiationTypes.put("map", "map");

// provide primitives to mustache template
List<String> sortedLanguageSpecificPrimitives= new ArrayList<String>(languageSpecificPrimitives);
Collections.sort(sortedLanguageSpecificPrimitives);
Expand Down Expand Up @@ -295,6 +295,12 @@ public void processOpts() {
if (additionalProperties.containsKey(VARIABLE_NAMING_CONVENTION)) {
this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION));
}
if (StringUtils.isBlank(composerVendorName) && additionalProperties.get(CodegenConstants.GIT_USER_ID) != null) {
additionalProperties.put(CodegenConstants.GIT_USER_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_USER_ID).toString()));
}
if (StringUtils.isBlank(composerProjectName) && additionalProperties.get(CodegenConstants.GIT_REPO_ID) != null) {
additionalProperties.put(CodegenConstants.GIT_REPO_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_REPO_ID).toString()));
}

additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));

Expand Down
24 changes: 22 additions & 2 deletions src/main/resources/handlebars/kotlin-client/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,29 @@ class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(base
{{#or hasFormParams hasBodyParam}}
val localVariableBody: kotlin.Any? = {{^isForm}}{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}{{/isForm}}{{#isForm}}{{#hasFormParams}}mapOf({{#formParams}}"{{{baseName}}}" to "${{paramName}}"{{#hasMore}}, {{/hasMore}}{{/formParams}}){{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}}{{/isForm}}
{{/or}}
{{#hasQueryParams}}val localVariableQuery: MultiValueMap = mapOf({{#queryParams}}"{{baseName}}" to {{#isContainer}}toMultiValue({{paramName}}{{^required}}!!{{/required}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf("${{paramName}}"){{/isContainer}}{{#hasMore}}, {{/hasMore}}{{/queryParams}}){{/hasQueryParams}}
{{#hasQueryParams}}
val localVariableQuery: MultiValueMap = mutableMapOf<kotlin.String, kotlin.collections.List<kotlin.String>>().apply {
{{#queryParams}}
{{^required}}
if ({{{paramName}}} != null) {
put("{{baseName}}", {{#isContainer}}toMultiValue({{{paramName}}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{#isDateTime}}parseDateToQueryString({{{paramName}}}){{/isDateTime}}{{#isDate}}parseDateToQueryString({{{paramName}}}){{/isDate}}{{^isDateTime}}{{^isDate}}{{{paramName}}}.toString(){{/isDate}}{{/isDateTime}}){{/isContainer}})
}
{{/required}}
{{#required}}
put("{{baseName}}", {{#isContainer}}toMultiValue({{{paramName}}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{#isDateTime}}parseDateToQueryString({{{paramName}}}){{/isDateTime}}{{#isDate}}parseDateToQueryString({{{paramName}}}){{/isDate}}{{^isDateTime}}{{^isDate}}{{{paramName}}}.toString(){{/isDate}}{{/isDateTime}}){{/isContainer}})
{{/required}}
{{/queryParams}}
}
{{/hasQueryParams}}
{{#or hasFormParams hasHeaderParams}}
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}}{{^required}}.toString(){{/required}}{{/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}})
{{#headerParams}}
{{{paramName}}}{{^required}}?{{/required}}.apply {
localVariableHeaders["{{baseName}}"] = {{#isContainer}}this.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}this.toString(){{/isContainer}}
}
{{/headerParams}}
{{^hasFormParams}}{{#hasConsumes}}{{#consumes}}localVariableHeaders["Content-Type"] = "{{{mediaType}}}"
{{/consumes}}{{/hasConsumes}}{{/hasFormParams}}{{#hasProduces}}localVariableHeaders["Accept"] = "{{#produces}}{{{mediaType}}}{{^@last}}, {{/@last}}{{/produces}}"{{/hasProduces}}
{{/or}}
val localVariableConfig = RequestConfig(
RequestMethod.{{httpMethod}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ group '{{groupId}}'
version '{{artifactVersion}}'

wrapper {
gradleVersion = '5.3'
gradleVersion = '7.5'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}

buildscript {
ext.kotlin_version = '1.4.30'
ext.kotlin_version = '1.8.0'
repositories {
maven { url "https://repo1.maven.org/maven2" }
Expand All @@ -24,10 +24,10 @@ repositories {
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "com.squareup.moshi:moshi-kotlin:1.11.0"
compile "com.squareup.moshi:moshi-adapters:1.11.0"
compile "com.squareup.okhttp3:okhttp:4.9.0"
testCompile "io.kotlintest:kotlintest:2.0.7"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.11.0"
implementation "com.squareup.moshi:moshi-adapters:1.11.0"
implementation "com.squareup.okhttp3:okhttp:4.9.0"
testImplementation "io.kotlintest:kotlintest:2.0.7"
}
16 changes: 9 additions & 7 deletions src/main/resources/handlebars/kotlin-client/data_class.mustache
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
/**
* {{{description}}}
{{#allVars}}
{{#vars}}
* @param {{name}} {{{description}}}
{{/allVars}}
{{/vars}}
*/
{{#hasVars}}data {{/hasVars}}class {{classname}} (

{{#allVars}}
{{#vars}}
{{#required}}
{{>data_class_req_var}}{{^@last}},{{/@last}}
{{/required}}
{{^required}}
{{>data_class_opt_var}}{{^@last}},{{/@last}}
{{/required}}
{{/allVars}}
{{/vars}}
) {
{{#allVars}}
{{#vars}}
{{#baseItems this}}
{{#isEnum}}
/**
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^@last}},{{/@last}}{{/enumVars}}{{/allowableValues}}
*/
enum class {{nameInCamelCase}}(val value: {{datatype}}{{#isNullable}}?{{/isNullable}}){
enum class {{{datatypeWithEnum}}}(val value: {{{datatype}}}{{#isNullable}}?{{/isNullable}}){
{{#allowableValues}}{{#enumVars}}
{{&name}}({{#value}}{{{value}}}{{/value}}{{^value}}null{{/value}}){{^@last}},{{/@last}}{{#@last}};{{/@last}}
{{/enumVars}}{{/allowableValues}}
}
{{/isEnum}}
{{/allVars}}
{{/baseItems}}
{{/vars}}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#description}}
/* {{{description}}} */
{{/description}}
val {{{name}}}: {{#is this 'enum'}}{{classname}}.{{nameInCamelCase}}{{/is}}{{#isNot this 'enum'}}{{{datatype}}}{{/isNot}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
val {{{name}}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#description}}
/* {{{description}}} */
{{/description}}
val {{{name}}}: {{#is this 'enum'}}{{classname}}.{{nameInCamelCase}}{{/is}}{{#isNot this 'enum'}}{{{datatype}}}{{/isNot}}
val {{{name}}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}}
32 changes: 20 additions & 12 deletions src/main/resources/handlebars/kotlin-server/data_class.mustache
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
/**
* {{{description}}}
{{~#vars}}
{{#vars}}
* @param {{name}} {{{description}}}
{{~/vars~}}
{{/vars}}
*/
data class {{classname}} (
{{~#requiredVars~}}
{{>data_class_req_var}}{{^@last}},{{/@last}}
{{~/requiredVars~}}
{{#has this 'required'}}{{#has this 'optional'}},{{/has}}{{/has}}
{{~#optionalVars}}{{>data_class_opt_var}}{{^@last}},{{/@last}}{{/optionalVars}}
{{#hasVars}}data {{/hasVars}}class {{classname}} (

{{#vars}}
{{#required}}
{{>data_class_req_var}}{{^@last}},{{/@last}}
{{/required}}
{{^required}}
{{>data_class_opt_var}}{{^@last}},{{/@last}}
{{/required}}
{{/vars}}
) {
{{#has this 'enums'}}{{#vars}}{{#is this 'enum'}}
{{#vars}}
{{#baseItems this}}
{{#isEnum}}
/**
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^@last}},{{/@last}}{{/enumVars}}{{/allowableValues}}
*/
enum class {{nameInCamelCase}}(val value: {{datatype}}){
enum class {{{datatypeWithEnum}}}(val value: {{{datatype}}}{{#isNullable}}?{{/isNullable}}){
{{#allowableValues}}{{#enumVars}}
{{&name}}({{{value}}}){{^@last}},{{/@last}}{{#@last}};{{/@last}}
{{&name}}({{#value}}{{{value}}}{{/value}}{{^value}}null{{/value}}){{^@last}},{{/@last}}{{#@last}};{{/@last}}
{{/enumVars}}{{/allowableValues}}
}
{{/is}}{{/vars}}{{/has}}
{{/isEnum}}
{{/baseItems}}
{{/vars}}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#description}}
/* {{{description}}} */
{{~/description}}
val {{{name}}}: {{#is this 'enum'}}{{classname}}.{{nameInCamelCase}}{{/is}}{{#isNot this 'enum'}}{{{datatype}}}{{/isNot}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
{{/description}}
val {{{name}}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#description}}
/* {{{description}}} */
{{/description}}
val {{{name}}}: {{#is this 'enum'}}{{classname}}.{{nameInCamelCase}}{{/is}}{{#isNot this 'enum'}}{{{datatype}}}{{/isNot}}
val {{{name}}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import io.ktor.util.KtorExperimentalAPI
{{#hasAuthMethods}}
import io.ktor.auth.Authentication
import io.ktor.auth.oauth
import io.ktor.auth.basic
import io.ktor.auth.UserIdPrincipal
import io.ktor.metrics.dropwizard.DropwizardMetrics
import io.swagger.server.infrastructure.ApiKeyCredential
import io.swagger.server.infrastructure.ApiPrincipal
Expand Down Expand Up @@ -88,13 +90,14 @@ fun Application.main() {
{{#authMethods}}
{{#isBasic}}
basic("{{{name}}}") {
validate { credentials ->
// TODO: "Apply your basic authentication functionality."
// Accessible in-method via call.principal<UserIdPrincipal>()
if (credentials.name == "Swagger" && "Codegen" == credentials.password) {
UserIdPrincipal(credentials.name)
} else {
null
validate { credentials ->
// TODO: "Apply your basic authentication functionality."
// Accessible in-method via call.principal<UserIdPrincipal>()
if (credentials.name == "Swagger" && "Codegen" == credentials.password) {
UserIdPrincipal(credentials.name)
} else {
null
}
}
}
{{/isBasic}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{{#authMethods}}
{{#@first}}
{{#isBasic}}
{{#or isBasic isBearer}}
val principal = call.authentication.principal<UserIdPrincipal>()
{{/isBasic}}
{{/or}}
{{#isApiKey}}
val principal = call.authentication.principal<ApiPrincipal>()
{{/isApiKey}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.ktor.locations.delete
import io.ktor.locations.get
import io.ktor.locations.post
import io.ktor.locations.put
import io.ktor.locations.patch
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.Route
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ group '{{groupId}}'
version '{{artifactVersion}}'

wrapper {
gradleVersion = '5.3'
gradleVersion = '7.5'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}

buildscript {
ext.kotlin_version = '1.4.30'
ext.ktor_version = '1.5.1'
ext.shadow_version = '5.2.0'
ext.kotlin_version = '1.8.0'
ext.ktor_version = "1.5.4"
ext.shadow_version = "6.1.0"
repositories {
maven {
url "https://repo1.maven.org/maven2"
}
maven {
url "https://plugins.gradle.org/m2/"
}
Expand Down Expand Up @@ -40,12 +43,6 @@ compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

kotlin {
experimental {
coroutines "enable"
}
}

shadowJar {
baseName = '{{artifactId}}'
classifier = null
Expand All @@ -59,13 +56,13 @@ repositories {
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "io.ktor:ktor-server-netty:$ktor_version"
compile "io.ktor:ktor-metrics:$ktor_version"
compile "io.ktor:ktor-locations:$ktor_version"
compile "io.ktor:ktor-gson:$ktor_version"
compile "io.ktor:ktor-client-core:$ktor_version"
compile "io.ktor:ktor-client-apache:$ktor_version"
compile "ch.qos.logback:logback-classic:1.2.9"
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "io.ktor:ktor-server-netty:$ktor_version"
implementation "io.ktor:ktor-metrics:$ktor_version"
implementation "io.ktor:ktor-locations:$ktor_version"
implementation "io.ktor:ktor-gson:$ktor_version"
implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-apache:$ktor_version"
implementation "ch.qos.logback:logback-classic:1.2.9"
testImplementation group: 'junit', name: 'junit', version: '4.12'
}
Loading

0 comments on commit 44ea4b2

Please sign in to comment.