diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index 71e901054226..5dbd78b11208 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -762,7 +762,7 @@ private Map buildSupportFileBundle(List allOperations, L bundle.put("authMethods", authMethods); bundle.put("hasAuthMethods", true); } - + List servers = config.fromServers(openAPI.getServers()); if (servers != null && !servers.isEmpty()) { bundle.put("servers", servers); @@ -1008,6 +1008,7 @@ private Map processOperations(CodegenConfig config, String tag, } if (mapping != null) { im.put("import", mapping); + im.put("classname", nextImport); if (!imports.contains(im)) { // avoid duplicates imports.add(im); } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java index 4ae97530cc46..17ad3c37e781 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java @@ -50,6 +50,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode public static final String SERVICE_FILE_SUFFIX = "serviceFileSuffix"; public static final String MODEL_SUFFIX = "modelSuffix"; public static final String MODEL_FILE_SUFFIX = "modelFileSuffix"; + public static final String FILE_NAMING = "fileNaming"; protected String npmName = null; protected String npmVersion = "1.0.0"; @@ -58,6 +59,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode protected String serviceFileSuffix = ".service"; protected String modelSuffix = ""; protected String modelFileSuffix = ""; + protected String fileNaming = "camelCase"; private boolean taggedUnions = false; @@ -95,6 +97,7 @@ public TypeScriptAngularClientCodegen() { this.cliOptions.add(new CliOption(SERVICE_FILE_SUFFIX, "The suffix of the file of the generated service (service.ts). Default is '.service'.")); this.cliOptions.add(new CliOption(MODEL_SUFFIX, "The suffix of the generated model. Default is ''.")); this.cliOptions.add(new CliOption(MODEL_FILE_SUFFIX, "The suffix of the file of the generated model (model.ts). Default is ''.")); + this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'camelCase', 'kebab-case'. Default is 'camelCase'.")); } @Override @@ -189,6 +192,9 @@ public void processOpts() { modelFileSuffix = additionalProperties.get(MODEL_FILE_SUFFIX).toString(); validateFileSuffixArgument("Model", modelFileSuffix); } + if (additionalProperties.containsKey(FILE_NAMING)) { + this.setFileNaming(additionalProperties.get(FILE_NAMING).toString()); + } } private void addNpmPackageGeneration(SemVer ngVersion) { @@ -375,7 +381,7 @@ public Map postProcessOperationsWithModels(Map o List> imports = (List>) operations.get("imports"); for (Map im : imports) { im.put("filename", im.get("import")); - im.put("classname", getModelnameFromModelFilename(im.get("filename").toString())); + im.put("classname", im.get("classname")); } return operations; @@ -456,7 +462,7 @@ public String toApiFilename(String name) { if (name.length() == 0) { return "default.service"; } - return org.openapitools.codegen.utils.StringUtils.camelize(removeModelSuffixIfNecessary(name), true) + serviceFileSuffix; + return this.convertUsingFileNamingConvention(name) + serviceFileSuffix; } @Override @@ -466,8 +472,7 @@ public String toApiImport(String name) { @Override public String toModelFilename(String name) { - String modelName = toModelName(name); - return org.openapitools.codegen.utils.StringUtils.camelize(removeModelSuffixIfNecessary(modelName), true) + modelFileSuffix; + return this.convertUsingFileNamingConvention(name) + modelFileSuffix; } @Override @@ -558,4 +563,32 @@ private void validateClassSuffixArgument(String argument, String value) { ); } } + + /** + * Set the file naming type. + * @param fileNaming the file naming to use + */ + private void setFileNaming(String fileNaming) { + if ("camelCase".equals(fileNaming) || "kebab-case".equals(fileNaming)) { + this.fileNaming = fileNaming; + } else { + throw new IllegalArgumentException("Invalid file naming '" + + fileNaming + "'. Must be 'camelCase' or 'kebab-case'"); + } + } + + /** + * Converts the original name according to the current fileNaming strategy. + * @param originalName the original name to transform + * @return the transformed name + */ + private String convertUsingFileNamingConvention(String originalName) { + String name = this.removeModelSuffixIfNecessary(originalName); + if ("kebab-case".equals(fileNaming)) { + name = dashize(underscore(name)); + } else { + name = camelize(name, true); + } + return name; + } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java index d4a1e718589a..c809a156ea5c 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java @@ -35,6 +35,7 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider { public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; public static final String NG_VERSION = "2"; public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; + public static final String FILE_NAMING_VALUE = "camelCase"; public static String SERVICE_SUFFIX = "Service"; public static String SERVICE_FILE_SUFFIX = ".service"; public static String MODEL_SUFFIX = ""; @@ -66,6 +67,7 @@ public Map createOptions() { .put(TypeScriptAngularClientCodegen.MODEL_FILE_SUFFIX, MODEL_FILE_SUFFIX) .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE) + .put(TypeScriptAngularClientCodegen.FILE_NAMING, FILE_NAMING_VALUE) .build(); }