mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 14:10:56 +00:00
typescript-angular: add serviceSuffix and serviceFileSuffix parameters suffix (#418)
This commit is contained in:
parent
be68ef502e
commit
dcc0c17a29
@ -34,6 +34,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
|
||||
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
|
||||
private static String CLASS_NAME_SUFFIX_PATTERN = "^[a-zA-Z0-9]*$";
|
||||
private static String FILE_NAME_SUFFIX_PATTERN = "^[a-zA-Z0-9.-]*$";
|
||||
|
||||
public static final String NPM_NAME = "npmName";
|
||||
public static final String NPM_VERSION = "npmVersion";
|
||||
@ -42,15 +44,19 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
public static final String WITH_INTERFACES = "withInterfaces";
|
||||
public static final String TAGGED_UNIONS = "taggedUnions";
|
||||
public static final String NG_VERSION = "ngVersion";
|
||||
public static final String PROVIDED_IN_ROOT ="providedInRoot";
|
||||
public static final String PROVIDED_IN_ROOT = "providedInRoot";
|
||||
public static final String SERVICE_SUFFIX = "serviceSuffix";
|
||||
public static final String SERVICE_FILE_SUFFIX = "serviceFileSuffix";
|
||||
public static final String MODEL_SUFFIX = "modelSuffix";
|
||||
public static final String MODEL_FILE_SUFFIX = "modelFileSuffix";
|
||||
|
||||
protected String npmName = null;
|
||||
protected String npmVersion = "1.0.0";
|
||||
protected String npmRepository = null;
|
||||
protected String serviceSuffix = "Service";
|
||||
protected String serviceFileSuffix = ".service";
|
||||
protected String modelSuffix = "";
|
||||
protected String modelFileSuffix = "";
|
||||
|
||||
private boolean taggedUnions = false;
|
||||
|
||||
@ -86,6 +92,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular. Default is '4.3'"));
|
||||
this.cliOptions.add(new CliOption(SERVICE_SUFFIX, "The suffix of the generated service. Default is 'Service'."));
|
||||
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 ''."));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -146,14 +154,14 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
}
|
||||
|
||||
if (ngVersion.atLeast("6.0.0")) {
|
||||
if (!additionalProperties.containsKey(PROVIDED_IN_ROOT)){
|
||||
additionalProperties.put(PROVIDED_IN_ROOT,true);
|
||||
}else {
|
||||
additionalProperties.put(PROVIDED_IN_ROOT,Boolean.valueOf(
|
||||
if (!additionalProperties.containsKey(PROVIDED_IN_ROOT)) {
|
||||
additionalProperties.put(PROVIDED_IN_ROOT, true);
|
||||
} else {
|
||||
additionalProperties.put(PROVIDED_IN_ROOT, Boolean.valueOf(
|
||||
(String) additionalProperties.get(PROVIDED_IN_ROOT)));
|
||||
}
|
||||
}else {
|
||||
additionalProperties.put(PROVIDED_IN_ROOT,false);
|
||||
} else {
|
||||
additionalProperties.put(PROVIDED_IN_ROOT, false);
|
||||
}
|
||||
|
||||
additionalProperties.put(NG_VERSION, ngVersion);
|
||||
@ -166,9 +174,19 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
}
|
||||
if (additionalProperties.containsKey(SERVICE_SUFFIX)) {
|
||||
serviceSuffix = additionalProperties.get(SERVICE_SUFFIX).toString();
|
||||
validateClassSuffixArgument("Service", serviceSuffix);
|
||||
}
|
||||
if (additionalProperties.containsKey(SERVICE_FILE_SUFFIX)) {
|
||||
serviceFileSuffix = additionalProperties.get(SERVICE_FILE_SUFFIX).toString();
|
||||
validateFileSuffixArgument("Service", serviceFileSuffix);
|
||||
}
|
||||
if (additionalProperties.containsKey(MODEL_SUFFIX)) {
|
||||
modelSuffix = additionalProperties.get(MODEL_SUFFIX).toString();
|
||||
validateClassSuffixArgument("Model", modelSuffix);
|
||||
}
|
||||
if (additionalProperties.containsKey(MODEL_FILE_SUFFIX)) {
|
||||
modelFileSuffix = additionalProperties.get(MODEL_FILE_SUFFIX).toString();
|
||||
validateFileSuffixArgument("Model", modelFileSuffix);
|
||||
}
|
||||
}
|
||||
|
||||
@ -364,12 +382,13 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
|
||||
/**
|
||||
* Finds and returns a path parameter of an operation by its name
|
||||
*
|
||||
* @param operation
|
||||
* @param parameterName
|
||||
* @return
|
||||
*/
|
||||
private CodegenParameter findPathParameterByName(CodegenOperation operation, String parameterName) {
|
||||
for(CodegenParameter param : operation.pathParams) {
|
||||
for (CodegenParameter param : operation.pathParams) {
|
||||
if (param.baseName.equals(parameterName)) {
|
||||
return param;
|
||||
}
|
||||
@ -380,14 +399,12 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
@Override
|
||||
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||
Map<String, Object> result = super.postProcessModels(objs);
|
||||
|
||||
return postProcessModelsEnum(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
|
||||
Map<String, Object> result = super.postProcessAllModels(objs);
|
||||
|
||||
for (Map.Entry<String, Object> entry : result.entrySet()) {
|
||||
Map<String, Object> inner = (Map<String, Object>) entry.getValue();
|
||||
List<Map<String, Object>> models = (List<Map<String, Object>>) inner.get("models");
|
||||
@ -416,8 +433,9 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
for (String im : imports) {
|
||||
if (!im.equals(cm.classname)) {
|
||||
HashMap<String, String> tsImport = new HashMap<>();
|
||||
// TVG: This is used as class name in the import statements of the model file
|
||||
tsImport.put("classname", im);
|
||||
tsImport.put("filename", toModelFilename(im));
|
||||
tsImport.put("filename", toModelFilename(removeModelSuffixIfNecessary(im)));
|
||||
tsImports.add(tsImport);
|
||||
}
|
||||
}
|
||||
@ -437,7 +455,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
if (name.length() == 0) {
|
||||
return "default.service";
|
||||
}
|
||||
return camelize(name, true) + serviceFileSuffix;
|
||||
return camelize(removeModelSuffixIfNecessary(name), true) + serviceFileSuffix;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -447,7 +465,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
|
||||
@Override
|
||||
public String toModelFilename(String name) {
|
||||
return camelize(toModelName(name), true);
|
||||
String modelName = toModelName(name);
|
||||
return camelize(removeModelSuffixIfNecessary(modelName), true) + modelFileSuffix;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -486,7 +505,56 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
|
||||
private String getModelnameFromModelFilename(String filename) {
|
||||
String name = filename.substring((modelPackage() + "/").length());
|
||||
return camelize(name);
|
||||
// Remove the file suffix and add the class suffix.
|
||||
// This is needed because the model file suffix might not be the same as
|
||||
// the model suffix.
|
||||
if (modelFileSuffix.length() > 0) {
|
||||
name = name.substring(0, name.length() - modelFileSuffix.length());
|
||||
}
|
||||
return camelize(name) + modelSuffix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelName(String name) {
|
||||
String modelName = super.toModelName(name);
|
||||
if (modelSuffix.length() == 0 || modelName.endsWith(modelSuffix)) {
|
||||
return modelName;
|
||||
}
|
||||
return modelName + modelSuffix;
|
||||
}
|
||||
|
||||
private String removeModelSuffixIfNecessary(String name) {
|
||||
if (modelSuffix.length() == 0 || !name.endsWith(modelSuffix)) {
|
||||
return name;
|
||||
}
|
||||
return name.substring(0, name.length() - modelSuffix.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that the given string value only contains '-', '.' and alpha numeric characters.
|
||||
* Throws an IllegalArgumentException, if the string contains any other characters.
|
||||
* @param argument The name of the argument being validated. This is only used for displaying an error message.
|
||||
* @param value The value that is being validated.
|
||||
*/
|
||||
private void validateFileSuffixArgument(String argument, String value) {
|
||||
if (!value.matches(FILE_NAME_SUFFIX_PATTERN)) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("%s file suffix only allows '.', '-' and alphanumeric characters.", argument)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that the given string value only contains alpha numeric characters.
|
||||
* Throws an IllegalArgumentException, if the string contains any other characters.
|
||||
* @param argument The name of the argument being validated. This is only used for displaying an error message.
|
||||
* @param value The value that is being validated.
|
||||
*/
|
||||
private void validateClassSuffixArgument(String argument, String value) {
|
||||
if (!value.matches(CLASS_NAME_SUFFIX_PATTERN)) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("%s class suffix only allows alphanumeric characters.", argument)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +37,8 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
|
||||
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
|
||||
public static String SERVICE_SUFFIX = "Service";
|
||||
public static String SERVICE_FILE_SUFFIX = ".service";
|
||||
public static String MODEL_SUFFIX = "";
|
||||
public static String MODEL_FILE_SUFFIX = "";
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
@ -60,6 +62,8 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
|
||||
.put(TypeScriptAngularClientCodegen.NG_VERSION, NG_VERSION)
|
||||
.put(TypeScriptAngularClientCodegen.SERVICE_SUFFIX, SERVICE_SUFFIX)
|
||||
.put(TypeScriptAngularClientCodegen.SERVICE_FILE_SUFFIX, SERVICE_FILE_SUFFIX)
|
||||
.put(TypeScriptAngularClientCodegen.MODEL_SUFFIX, MODEL_SUFFIX)
|
||||
.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)
|
||||
.build();
|
||||
|
Loading…
x
Reference in New Issue
Block a user