[Kotlin] minor bug fixes (#10071)

* fix http basic auth, deprecate option, escape special property name

* update doc
This commit is contained in:
William Cheng 2021-08-02 18:04:38 +08:00 committed by GitHub
parent e39e4bcd6e
commit ec49dc3fed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 34 deletions

View File

@ -27,8 +27,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sourceFolder|source folder for generated code| |src/main/kotlin|
|supportAndroidApiLevel25AndBelow|[WARNING] This flag will generate code that has a known security vulnerability. It uses `kotlin.io.createTempFile` instead of `java.nio.file.Files.createTempFile` in oder to support Android API level 25 and bellow. For more info, please check the following links https://github.com/OpenAPITools/openapi-generator/security/advisories/GHSA-23x4-m842-fmwf, https://github.com/OpenAPITools/openapi-generator/pull/9284| |false|
|useCoroutines|Whether to use the Coroutines adapter with the retrofit2 library.| |false|
|useRxJava|Whether to use the RxJava adapter with the retrofit2 library.| |false|
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library.| |false|
|useRxJava|Whether to use the RxJava adapter with the retrofit2 library. IMPORTANT: this option has been deprecated. Please use `useRxJava3` instead.| |false|
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: this option has been deprecated. Please use `useRxJava3` instead.| |false|
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library.| |false|
## IMPORT MAPPING

View File

@ -23,12 +23,7 @@ import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -73,6 +68,10 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
protected CodegenConstants.ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase;
protected SERIALIZATION_LIBRARY_TYPE serializationLibrary = SERIALIZATION_LIBRARY_TYPE.moshi;
// model classes cannot use the same property names defined in HashMap
// ref: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/
protected Set<String> propertyAdditionalKeywords = new HashSet<>(Arrays.asList("entries", "keys", "size", "values"));
public AbstractKotlinCodegen() {
super();
@ -856,11 +855,20 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
}
// should be the same as variable name
return toVarName(name);
return toVariableName(name);
}
@Override
public String toVarName(String name) {
name = toVariableName(name);
if (propertyAdditionalKeywords.contains(name)) {
return camelize("property_" + name, true);
} else {
return name;
}
}
protected String toVariableName(String name) {
// sanitize name
name = sanitizeName(name, "\\W-[\\$]");
name = sanitizeKotlinSpecificNames(name);
@ -984,7 +992,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
itemsSchema.setDefault(element.asText());
defaultContent.append(toDefaultValue(itemsSchema)).append(",");
});
defaultContent.deleteCharAt(defaultContent.length()-1); // remove trailing comma
defaultContent.deleteCharAt(defaultContent.length() - 1); // remove trailing comma
return arrInstantiationType + "Of(" + defaultContent + ")";
}
} else if (ModelUtils.isStringSchema(p)) {

View File

@ -17,21 +17,12 @@
package org.openapitools.codegen.languages;
import static java.util.Collections.sort;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.utils.ProcessUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openapitools.codegen.utils.ProcessUtils;
import java.io.File;
import java.util.HashMap;
@ -40,6 +31,8 @@ import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.Collections.sort;
public class KotlinClientCodegen extends AbstractKotlinCodegen {
private final Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class);
@ -204,8 +197,8 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
requestDateConverter.setDefault(this.requestDateConverter);
cliOptions.add(requestDateConverter);
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA, "Whether to use the RxJava adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA2, "Whether to use the RxJava2 adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA, "Whether to use the RxJava adapter with the retrofit2 library. IMPORTANT: this option has been deprecated. Please use `useRxJava3` instead."));
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA2, "Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: this option has been deprecated. Please use `useRxJava3` instead."));
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA3, "Whether to use the RxJava3 adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(USE_COROUTINES, "Whether to use the Coroutines adapter with the retrofit2 library."));
@ -378,7 +371,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
additionalProperties.put("isList", true);
}
if(usesRetrofit2Library()) {
if (usesRetrofit2Library()) {
if (ProcessUtils.hasOAuthMethods(openAPI)) {
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.kt.mustache", authFolder, "ApiKeyAuth.kt"));
supportingFiles.add(new SupportingFile("auth/OAuth.kt.mustache", authFolder, "OAuth.kt"));
@ -386,11 +379,11 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.kt.mustache", authFolder, "OAuthOkHttpClient.kt"));
}
if(ProcessUtils.hasHttpBearerMethods(openAPI)) {
if (ProcessUtils.hasHttpBearerMethods(openAPI)) {
supportingFiles.add(new SupportingFile("auth/HttpBearerAuth.kt.mustache", authFolder, "HttpBearerAuth.kt"));
}
if(ProcessUtils.hasHttpBasicMethods(openAPI)) {
if (ProcessUtils.hasHttpBasicMethods(openAPI)) {
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.kt.mustache", authFolder, "HttpBasicAuth.kt"));
}
}
@ -591,7 +584,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
private void commonSupportingFiles() {
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
if(getLibrary().equals(MULTIPLATFORM)) {
if (getLibrary().equals(MULTIPLATFORM)) {
supportingFiles.add(new SupportingFile("build.gradle.kts.mustache", "", "build.gradle.kts"));
supportingFiles.add(new SupportingFile("settings.gradle.kts.mustache", "", "settings.gradle.kts"));
} else {
@ -677,7 +670,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
if (one.isPathParam && another.isQueryParam) {
return -1;
}
if (one.isQueryParam && another.isPathParam){
if (one.isQueryParam && another.isPathParam) {
return 1;
}

View File

@ -3,11 +3,6 @@ package {{packageName}}.infrastructure
{{#supportAndroidApiLevel25AndBelow}}
import android.os.Build
{{/supportAndroidApiLevel25AndBelow}}
{{#hasAuthMethods}}
{{#isBasicBasic}}
import okhttp3.Credentials
{{/isBasicBasic}}
{{/hasAuthMethods}}
import okhttp3.OkHttpClient
import okhttp3.RequestBody
{{#jvm-okhttp3}}
@ -252,7 +247,7 @@ import com.squareup.moshi.adapter
if (requestConfig.headers[Authorization].isNullOrEmpty()) {
username?.let { username ->
password?.let { password ->
requestConfig.headers[Authorization] = Credentials.basic(username, password)
requestConfig.headers[Authorization] = okhttp3.Credentials.basic(username, password)
}
}
}