Skip to content

Commit 38bb732

Browse files
tomvangreenwing328
authored andcommitted
Typescript Angular 2: Make service suffix configurable (#341)
* Added serviceSuffix and serviceFileSuffix parameters to control the suffixes of generated class and file names * Updated TypeScriptAngularClientOptionsProvider to include the new serviceSuffix and serviceFileSuffix parameters * Fixed part in generator where hardcoded 'Service' suffix was used. * Made the . in the service file name part of the config setting * Updated cli message
1 parent a0bfe2b commit 38bb732

8 files changed

Lines changed: 25 additions & 10 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
4343
public static final String TAGGED_UNIONS = "taggedUnions";
4444
public static final String NG_VERSION = "ngVersion";
4545
public static final String PROVIDED_IN_ROOT ="providedInRoot";
46-
46+
public static final String SERVICE_SUFFIX = "serviceSuffix";
47+
public static final String SERVICE_FILE_SUFFIX = "serviceFileSuffix";
4748

4849
protected String npmName = null;
4950
protected String npmVersion = "1.0.0";
5051
protected String npmRepository = null;
52+
protected String serviceSuffix = "Service";
53+
protected String serviceFileSuffix = ".service";
5154

5255
private boolean taggedUnions = false;
5356

@@ -81,6 +84,8 @@ public TypeScriptAngularClientCodegen() {
8184
"Use this property to provide Injectables in root (it is only valid in angular version greater or equal to 6.0.0).",
8285
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
8386
this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular. Default is '4.3'"));
87+
this.cliOptions.add(new CliOption(SERVICE_SUFFIX, "The suffix of the generated service. Default is 'Service'."));
88+
this.cliOptions.add(new CliOption(SERVICE_FILE_SUFFIX, "The suffix of the file of the generated service (service<suffix>.ts). Default is '.service'."));
8489
}
8590

8691
@Override
@@ -159,6 +164,12 @@ public void processOpts() {
159164
if (!ngVersion.atLeast("4.3.0")) {
160165
supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts"));
161166
}
167+
if (additionalProperties.containsKey(SERVICE_SUFFIX)) {
168+
serviceSuffix = additionalProperties.get(SERVICE_SUFFIX).toString();
169+
}
170+
if (additionalProperties.containsKey(SERVICE_FILE_SUFFIX)) {
171+
serviceFileSuffix = additionalProperties.get(SERVICE_FILE_SUFFIX).toString();
172+
}
162173
}
163174

164175
private void addNpmPackageGeneration(SemVer ngVersion) {
@@ -418,15 +429,15 @@ public String toApiName(String name) {
418429
if (name.length() == 0) {
419430
return "DefaultService";
420431
}
421-
return initialCaps(name) + "Service";
432+
return initialCaps(name) + serviceSuffix;
422433
}
423434

424435
@Override
425436
public String toApiFilename(String name) {
426437
if (name.length() == 0) {
427438
return "default.service";
428439
}
429-
return camelize(name, true) + ".service";
440+
return camelize(name, true) + serviceFileSuffix;
430441
}
431442

432443
@Override
@@ -469,7 +480,7 @@ public void setNpmRepository(String npmRepository) {
469480
}
470481

471482
private String getApiFilenameFromClassname(String classname) {
472-
String name = classname.substring(0, classname.length() - "Service".length());
483+
String name = classname.substring(0, classname.length() - serviceSuffix.length());
473484
return toApiFilename(name);
474485
}
475486

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
3535
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
3636
public static final String NG_VERSION = "2";
3737
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
38+
public static String SERVICE_SUFFIX = "Service";
39+
public static String SERVICE_FILE_SUFFIX = ".service";
3840

3941
@Override
4042
public String getLanguage() {
@@ -56,6 +58,8 @@ public Map<String, String> createOptions() {
5658
.put(TypeScriptAngularClientCodegen.TAGGED_UNIONS, Boolean.FALSE.toString())
5759
.put(TypeScriptAngularClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY)
5860
.put(TypeScriptAngularClientCodegen.NG_VERSION, NG_VERSION)
61+
.put(TypeScriptAngularClientCodegen.SERVICE_SUFFIX, SERVICE_SUFFIX)
62+
.put(TypeScriptAngularClientCodegen.SERVICE_FILE_SUFFIX, SERVICE_FILE_SUFFIX)
5963
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
6064
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
6165
.build();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.1-SNAPSHOT
1+
3.0.2-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.1-SNAPSHOT
1+
3.0.2-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.1-SNAPSHOT
1+
3.0.2-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.1-SNAPSHOT
1+
3.0.2-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.1-SNAPSHOT
1+
3.0.2-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.1-SNAPSHOT
1+
3.0.2-SNAPSHOT

0 commit comments

Comments
 (0)