forked from loafle/openapi-generator-original
[Kotlin]Reducing code smells (#13703)
This commit is contained in:
parent
f864c6d226
commit
04e441bad2
@ -90,7 +90,7 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
|
|
||||||
// A cache of sanitized words. The sanitizeName() method is invoked many times with the same
|
// A cache of sanitized words. The sanitizeName() method is invoked many times with the same
|
||||||
// arguments, this cache is used to optimized performance.
|
// arguments, this cache is used to optimized performance.
|
||||||
private static Cache<SanitizeNameOptions, String> sanitizedNameCache;
|
private static final Cache<SanitizeNameOptions, String> sanitizedNameCache;
|
||||||
private static final String xSchemaTestExamplesKey = "x-schema-test-examples";
|
private static final String xSchemaTestExamplesKey = "x-schema-test-examples";
|
||||||
private static final String xSchemaTestExamplesRefPrefix = "#/components/x-schema-test-examples/";
|
private static final String xSchemaTestExamplesRefPrefix = "#/components/x-schema-test-examples/";
|
||||||
protected static Schema falseSchema;
|
protected static Schema falseSchema;
|
||||||
@ -161,9 +161,9 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
protected Set<String> reservedWords;
|
protected Set<String> reservedWords;
|
||||||
protected Set<String> languageSpecificPrimitives = new HashSet<>();
|
protected Set<String> languageSpecificPrimitives = new HashSet<>();
|
||||||
protected Map<String, String> importMapping = new HashMap<>();
|
protected Map<String, String> importMapping = new HashMap<>();
|
||||||
// a map to store the mappping between a schema and the new one
|
// a map to store the mapping between a schema and the new one
|
||||||
protected Map<String, String> schemaMapping = new HashMap<>();
|
protected Map<String, String> schemaMapping = new HashMap<>();
|
||||||
// a map to store the mappping between inline schema and the name provided by the user
|
// a map to store the mapping between inline schema and the name provided by the user
|
||||||
protected Map<String, String> inlineSchemaNameMapping = new HashMap<>();
|
protected Map<String, String> inlineSchemaNameMapping = new HashMap<>();
|
||||||
// a map to store the inline schema naming conventions
|
// a map to store the inline schema naming conventions
|
||||||
protected Map<String, String> inlineSchemaNameDefault = new HashMap<>();
|
protected Map<String, String> inlineSchemaNameDefault = new HashMap<>();
|
||||||
|
@ -60,12 +60,12 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
|
|
||||||
protected String sourceFolder = "src/main/kotlin";
|
protected String sourceFolder = "src/main/kotlin";
|
||||||
protected String testFolder = "src/test/kotlin";
|
protected String testFolder = "src/test/kotlin";
|
||||||
|
protected String resourcesFolder = "src/main/resources";
|
||||||
|
|
||||||
protected String apiDocPath = "docs/";
|
protected String apiDocPath = "docs/";
|
||||||
protected String modelDocPath = "docs/";
|
protected String modelDocPath = "docs/";
|
||||||
protected boolean parcelizeModels = false;
|
protected boolean parcelizeModels = false;
|
||||||
protected boolean serializableModel = false;
|
protected boolean serializableModel = false;
|
||||||
protected boolean needsDataClassBody = false;
|
|
||||||
|
|
||||||
protected boolean nonPublicApi = false;
|
protected boolean nonPublicApi = false;
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
// ref: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/
|
// ref: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/
|
||||||
protected Set<String> propertyAdditionalKeywords = new HashSet<>(Arrays.asList("entries", "keys", "size", "values"));
|
protected Set<String> propertyAdditionalKeywords = new HashSet<>(Arrays.asList("entries", "keys", "size", "values"));
|
||||||
|
|
||||||
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
|
private final Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
|
||||||
|
|
||||||
public AbstractKotlinCodegen() {
|
public AbstractKotlinCodegen() {
|
||||||
super();
|
super();
|
||||||
@ -578,14 +578,6 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
this.nonPublicApi = nonPublicApi;
|
this.nonPublicApi = nonPublicApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isNeedsDataClassBody() {
|
|
||||||
return needsDataClassBody;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNeedsDataClassBody(boolean needsDataClassBody) {
|
|
||||||
this.needsDataClassBody = needsDataClassBody;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the sanitized variable name for enum
|
* Return the sanitized variable name for enum
|
||||||
*
|
*
|
||||||
@ -673,9 +665,8 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
@Override
|
@Override
|
||||||
public String toModelName(final String name) {
|
public String toModelName(final String name) {
|
||||||
// memoization
|
// memoization
|
||||||
String origName = name;
|
if (schemaKeyToModelNameCache.containsKey(name)) {
|
||||||
if (schemaKeyToModelNameCache.containsKey(origName)) {
|
return schemaKeyToModelNameCache.get(name);
|
||||||
return schemaKeyToModelNameCache.get(origName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow for explicitly configured kotlin.* and java.* types
|
// Allow for explicitly configured kotlin.* and java.* types
|
||||||
@ -695,9 +686,8 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
}
|
}
|
||||||
|
|
||||||
String modifiedName = name.replaceAll("\\.", "");
|
String modifiedName = name.replaceAll("\\.", "");
|
||||||
String sanitizedName = sanitizeKotlinSpecificNames(modifiedName);
|
|
||||||
|
|
||||||
String nameWithPrefixSuffix = sanitizedName;
|
String nameWithPrefixSuffix = sanitizeKotlinSpecificNames(modifiedName);
|
||||||
if (!StringUtils.isEmpty(modelNamePrefix)) {
|
if (!StringUtils.isEmpty(modelNamePrefix)) {
|
||||||
// add '_' so that model name can be camelized correctly
|
// add '_' so that model name can be camelized correctly
|
||||||
nameWithPrefixSuffix = modelNamePrefix + "_" + nameWithPrefixSuffix;
|
nameWithPrefixSuffix = modelNamePrefix + "_" + nameWithPrefixSuffix;
|
||||||
@ -726,8 +716,8 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
return modelName;
|
return modelName;
|
||||||
}
|
}
|
||||||
|
|
||||||
schemaKeyToModelNameCache.put(origName, titleCase(modifiedName));
|
schemaKeyToModelNameCache.put(name, titleCase(modifiedName));
|
||||||
return schemaKeyToModelNameCache.get(origName);
|
return schemaKeyToModelNameCache.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -843,10 +833,9 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
@Override
|
@Override
|
||||||
protected boolean needToImport(String type) {
|
protected boolean needToImport(String type) {
|
||||||
// provides extra protection against improperly trying to import language primitives and java types
|
// provides extra protection against improperly trying to import language primitives and java types
|
||||||
boolean imports = !type.startsWith("kotlin.") && !type.startsWith("java.") &&
|
return !type.startsWith("kotlin.") && !type.startsWith("java.") &&
|
||||||
!defaultIncludes.contains(type) && !languageSpecificPrimitives.contains(type) &&
|
!defaultIncludes.contains(type) && !languageSpecificPrimitives.contains(type) &&
|
||||||
!type.contains(".");
|
!type.contains(".");
|
||||||
return imports;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -938,7 +927,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If name contains special chars -> replace them.
|
// If name contains special chars -> replace them.
|
||||||
if ((name.chars().anyMatch(character -> specialCharReplacements.keySet().contains(String.valueOf((char) character))))) {
|
if ((name.chars().anyMatch(character -> specialCharReplacements.containsKey(String.valueOf((char) character))))) {
|
||||||
List<String> allowedCharacters = new ArrayList<>();
|
List<String> allowedCharacters = new ArrayList<>();
|
||||||
allowedCharacters.add("_");
|
allowedCharacters.add("_");
|
||||||
allowedCharacters.add("$");
|
allowedCharacters.add("$");
|
||||||
@ -1017,15 +1006,17 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toDefaultValue(Schema schema) {
|
public String toDefaultValue(Schema schema) {
|
||||||
Schema p = ModelUtils.getReferencedSchema(this.openAPI, schema);
|
Schema<?> p = ModelUtils.getReferencedSchema(this.openAPI, schema);
|
||||||
if (ModelUtils.isBooleanSchema(p)) {
|
if (ModelUtils.isBooleanSchema(p)) {
|
||||||
if (p.getDefault() != null) {
|
if (p.getDefault() != null) {
|
||||||
return p.getDefault().toString();
|
return p.getDefault().toString();
|
||||||
}
|
}
|
||||||
} else if (ModelUtils.isDateSchema(p)) {
|
} else if (ModelUtils.isDateSchema(p)) {
|
||||||
// TODO
|
// TODO
|
||||||
|
return null;
|
||||||
} else if (ModelUtils.isDateTimeSchema(p)) {
|
} else if (ModelUtils.isDateTimeSchema(p)) {
|
||||||
// TODO
|
// TODO
|
||||||
|
return null;
|
||||||
} else if (ModelUtils.isNumberSchema(p)) {
|
} else if (ModelUtils.isNumberSchema(p)) {
|
||||||
if (p.getDefault() != null) {
|
if (p.getDefault() != null) {
|
||||||
return fixNumberValue(p.getDefault().toString(), p);
|
return fixNumberValue(p.getDefault().toString(), p);
|
||||||
@ -1071,7 +1062,6 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1082,7 +1072,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void updateModelForObject(CodegenModel m, Schema schema) {
|
protected void updateModelForObject(CodegenModel m, Schema schema) {
|
||||||
/**
|
/*
|
||||||
* we have a custom version of this function so we only set isMap to true if
|
* we have a custom version of this function so we only set isMap to true if
|
||||||
* ModelUtils.isMapSchema
|
* ModelUtils.isMapSchema
|
||||||
* In other generators, isMap is true for all type object schemas
|
* In other generators, isMap is true for all type object schemas
|
||||||
|
@ -131,7 +131,7 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
|
|||||||
|
|
||||||
cliOptions.add(CliOption.newBoolean(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files. This option is currently supported only when using jaxrs-spec library.").defaultValue(String.valueOf(interfaceOnly)));
|
cliOptions.add(CliOption.newBoolean(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files. This option is currently supported only when using jaxrs-spec library.").defaultValue(String.valueOf(interfaceOnly)));
|
||||||
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations. This option is currently supported only when using jaxrs-spec library.", useBeanValidation));
|
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations. This option is currently supported only when using jaxrs-spec library.", useBeanValidation));
|
||||||
cliOptions.add(CliOption.newBoolean(USE_COROUTINES, "Whether to use the Coroutines. This option is currently supported only when using jaxrs-spec library."));
|
cliOptions.add(CliOption.newBoolean(USE_COROUTINES, "Whether to use the Coroutines. This option is currently supported only when using jaxrs-spec library.", useCoroutines));
|
||||||
cliOptions.add(CliOption.newBoolean(RETURN_RESPONSE, "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.").defaultValue(String.valueOf(returnResponse)));
|
cliOptions.add(CliOption.newBoolean(RETURN_RESPONSE, "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.").defaultValue(String.valueOf(returnResponse)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,7 +293,6 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
|
|||||||
|
|
||||||
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 packageFolder = (sourceFolder + File.separator + packageName).replace(".", File.separator);
|
||||||
String resourcesFolder = "src/main/resources"; // not sure this can be user configurable.
|
|
||||||
|
|
||||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||||
|
|
||||||
|
@ -214,7 +214,6 @@ public class KotlinServerDeprecatedCodegen extends AbstractKotlinCodegen {
|
|||||||
|
|
||||||
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 packageFolder = (sourceFolder + File.separator + packageName).replace(".", File.separator);
|
||||||
String resourcesFolder = "src/main/resources"; // not sure this can be user configurable.
|
|
||||||
|
|
||||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||||
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
|
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
|
||||||
|
@ -75,7 +75,6 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
|||||||
private String invokerPackage;
|
private String invokerPackage;
|
||||||
private String serverPort = "8080";
|
private String serverPort = "8080";
|
||||||
private String title = "OpenAPI Kotlin Spring";
|
private String title = "OpenAPI Kotlin Spring";
|
||||||
private String resourceFolder = "src/main/resources";
|
|
||||||
private boolean useBeanValidation = true;
|
private boolean useBeanValidation = true;
|
||||||
private boolean exceptionHandler = true;
|
private boolean exceptionHandler = true;
|
||||||
private boolean gradleBuildFile = true;
|
private boolean gradleBuildFile = true;
|
||||||
@ -234,14 +233,6 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
|||||||
getDocumentationProvider().equals(DocumentationProvider.SOURCE);
|
getDocumentationProvider().equals(DocumentationProvider.SOURCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getResourceFolder() {
|
|
||||||
return this.resourceFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setResourceFolder(String resourceFolder) {
|
|
||||||
this.resourceFolder = resourceFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getBasePackage() {
|
public String getBasePackage() {
|
||||||
return this.basePackage;
|
return this.basePackage;
|
||||||
}
|
}
|
||||||
@ -336,10 +327,6 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
|||||||
this.reactive = reactive;
|
this.reactive = reactive;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBeanQualifiers() {
|
|
||||||
return beanQualifiers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBeanQualifiers(boolean beanQualifiers) {
|
public void setBeanQualifiers(boolean beanQualifiers) {
|
||||||
this.beanQualifiers = beanQualifiers;
|
this.beanQualifiers = beanQualifiers;
|
||||||
}
|
}
|
||||||
@ -569,16 +556,16 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
|||||||
(sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator),
|
(sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator),
|
||||||
"HomeController.kt"));
|
"HomeController.kt"));
|
||||||
supportingFiles.add(new SupportingFile("openapi.mustache",
|
supportingFiles.add(new SupportingFile("openapi.mustache",
|
||||||
("src/main/resources").replace("/", java.io.File.separator), "openapi.yaml"));
|
resourcesFolder.replace("/", java.io.File.separator), "openapi.yaml"));
|
||||||
}
|
}
|
||||||
|
|
||||||
supportingFiles.add(new SupportingFile("application.mustache", resourceFolder, "application.yaml"));
|
supportingFiles.add(new SupportingFile("application.mustache", resourcesFolder, "application.yaml"));
|
||||||
supportingFiles.add(new SupportingFile("springBootApplication.mustache",
|
supportingFiles.add(new SupportingFile("springBootApplication.mustache",
|
||||||
sanitizeDirectory(sourceFolder + File.separator + basePackage), "Application.kt"));
|
sanitizeDirectory(sourceFolder + File.separator + basePackage), "Application.kt"));
|
||||||
|
|
||||||
if (useSwaggerUI && selectedDocumentationProviderRequiresSwaggerUiBootstrap()) {
|
if (useSwaggerUI && selectedDocumentationProviderRequiresSwaggerUiBootstrap()) {
|
||||||
supportingFiles.add(new SupportingFile("swagger-ui.mustache",
|
supportingFiles.add(new SupportingFile("swagger-ui.mustache",
|
||||||
"src/main/resources/static", "swagger-ui.html"));
|
resourcesFolder + "/static", "swagger-ui.html"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user