Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class DefaultCodegen implements CodegenConfig {

// A cache of sanitized words. The sanitizeName() method is invoked many times with the same
// 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 xSchemaTestExamplesRefPrefix = "#/components/x-schema-test-examples/";
protected static Schema falseSchema;
Expand Down Expand Up @@ -161,9 +161,9 @@ public class DefaultCodegen implements CodegenConfig {
protected Set<String> reservedWords;
protected Set<String> languageSpecificPrimitives = new HashSet<>();
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<>();
// 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<>();
// a map to store the inline schema naming conventions
protected Map<String, String> inlineSchemaNameDefault = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public enum SERIALIZATION_LIBRARY_TYPE {moshi, gson, jackson, kotlinx_serializat

protected String sourceFolder = "src/main/kotlin";
protected String testFolder = "src/test/kotlin";
protected String resourcesFolder = "src/main/resources";

protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
protected boolean parcelizeModels = false;
protected boolean serializableModel = false;
protected boolean needsDataClassBody = false;

protected boolean nonPublicApi = false;

Expand All @@ -76,7 +76,7 @@ public enum SERIALIZATION_LIBRARY_TYPE {moshi, gson, jackson, kotlinx_serializat
// ref: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/
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() {
super();
Expand Down Expand Up @@ -578,14 +578,6 @@ public void setNonPublicApi(boolean nonPublicApi) {
this.nonPublicApi = nonPublicApi;
}

public boolean isNeedsDataClassBody() {
return needsDataClassBody;
}

public void setNeedsDataClassBody(boolean needsDataClassBody) {
this.needsDataClassBody = needsDataClassBody;
}

/**
* Return the sanitized variable name for enum
*
Expand Down Expand Up @@ -673,9 +665,8 @@ public String toModelImport(String name) {
@Override
public String toModelName(final String name) {
// memoization
String origName = name;
if (schemaKeyToModelNameCache.containsKey(origName)) {
return schemaKeyToModelNameCache.get(origName);
if (schemaKeyToModelNameCache.containsKey(name)) {
return schemaKeyToModelNameCache.get(name);
}

// Allow for explicitly configured kotlin.* and java.* types
Expand All @@ -695,9 +686,8 @@ public String toModelName(final String name) {
}

String modifiedName = name.replaceAll("\\.", "");
String sanitizedName = sanitizeKotlinSpecificNames(modifiedName);

String nameWithPrefixSuffix = sanitizedName;
String nameWithPrefixSuffix = sanitizeKotlinSpecificNames(modifiedName);
if (!StringUtils.isEmpty(modelNamePrefix)) {
// add '_' so that model name can be camelized correctly
nameWithPrefixSuffix = modelNamePrefix + "_" + nameWithPrefixSuffix;
Expand Down Expand Up @@ -726,8 +716,8 @@ public String toModelName(final String name) {
return modelName;
}

schemaKeyToModelNameCache.put(origName, titleCase(modifiedName));
return schemaKeyToModelNameCache.get(origName);
schemaKeyToModelNameCache.put(name, titleCase(modifiedName));
return schemaKeyToModelNameCache.get(name);
}

/**
Expand Down Expand Up @@ -843,10 +833,9 @@ protected boolean isReservedWord(String word) {
@Override
protected boolean needToImport(String type) {
// 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) &&
!type.contains(".");
return imports;
}

@Override
Expand Down Expand Up @@ -938,7 +927,7 @@ protected String toVariableName(String name) {
}

// 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<>();
allowedCharacters.add("_");
allowedCharacters.add("$");
Expand Down Expand Up @@ -1017,15 +1006,17 @@ private String fixNumberValue(String number, Schema p) {

@Override
public String toDefaultValue(Schema schema) {
Schema p = ModelUtils.getReferencedSchema(this.openAPI, schema);
Schema<?> p = ModelUtils.getReferencedSchema(this.openAPI, schema);
if (ModelUtils.isBooleanSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
}
} else if (ModelUtils.isDateSchema(p)) {
// TODO
return null;
} else if (ModelUtils.isDateTimeSchema(p)) {
// TODO
return null;
} else if (ModelUtils.isNumberSchema(p)) {
if (p.getDefault() != null) {
return fixNumberValue(p.getDefault().toString(), p);
Expand Down Expand Up @@ -1071,7 +1062,6 @@ public String toDefaultValue(Schema schema) {
}
return null;
}

return null;
}

Expand All @@ -1082,7 +1072,7 @@ public GeneratorLanguage generatorLanguage() {

@Override
protected void updateModelForObject(CodegenModel m, Schema schema) {
/**
/*
* we have a custom version of this function so we only set isMap to true if
* ModelUtils.isMapSchema
* In other generators, isMap is true for all type object schemas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public KotlinServerCodegen() {

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_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)));
}

Expand Down Expand Up @@ -293,7 +293,6 @@ public void processOpts() {

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.

supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public void processOpts() {

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.

supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
private String invokerPackage;
private String serverPort = "8080";
private String title = "OpenAPI Kotlin Spring";
private String resourceFolder = "src/main/resources";
private boolean useBeanValidation = true;
private boolean exceptionHandler = true;
private boolean gradleBuildFile = true;
Expand Down Expand Up @@ -234,14 +233,6 @@ private boolean selectedDocumentationProviderRequiresSwaggerUiBootstrap() {
getDocumentationProvider().equals(DocumentationProvider.SOURCE);
}

public String getResourceFolder() {
return this.resourceFolder;
}

public void setResourceFolder(String resourceFolder) {
this.resourceFolder = resourceFolder;
}

public String getBasePackage() {
return this.basePackage;
}
Expand Down Expand Up @@ -336,10 +327,6 @@ public void setReactive(boolean reactive) {
this.reactive = reactive;
}

public boolean isBeanQualifiers() {
return beanQualifiers;
}

public void setBeanQualifiers(boolean beanQualifiers) {
this.beanQualifiers = beanQualifiers;
}
Expand Down Expand Up @@ -569,16 +556,16 @@ public void processOpts() {
(sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator),
"HomeController.kt"));
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",
sanitizeDirectory(sourceFolder + File.separator + basePackage), "Application.kt"));

if (useSwaggerUI && selectedDocumentationProviderRequiresSwaggerUiBootstrap()) {
supportingFiles.add(new SupportingFile("swagger-ui.mustache",
"src/main/resources/static", "swagger-ui.html"));
resourcesFolder + "/static", "swagger-ui.html"));
}
}
}
Expand Down