Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript-angular] Add fileNaming configuration property #767

Merged
merged 3 commits into from
Sep 4, 2018
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 @@ -762,7 +762,7 @@ private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, L
bundle.put("authMethods", authMethods);
bundle.put("hasAuthMethods", true);
}

List<CodegenServer> servers = config.fromServers(openAPI.getServers());
if (servers != null && !servers.isEmpty()) {
bundle.put("servers", servers);
Expand Down Expand Up @@ -1008,6 +1008,7 @@ private Map<String, Object> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;

Expand Down Expand Up @@ -95,6 +97,7 @@ public TypeScriptAngularClientCodegen() {
this.cliOptions.add(new CliOption(SERVICE_FILE_SUFFIX, "The suffix of the file of the generated service (service<suffix>.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<suffix>.ts). Default is ''."));
this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'camelCase', 'kebab-case'. Default is 'camelCase'."));
}

@Override
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -375,7 +381,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports");
for (Map<String, Object> im : imports) {
im.put("filename", im.get("import"));
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString()));
im.put("classname", im.get("classname"));
}

return operations;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 <tt>fileNaming</tt> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -66,6 +67,7 @@ public Map<String, String> 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();
}

Expand Down