Skip to content

Commit ebc9e29

Browse files
bgong-mdsolwing328
authored andcommitted
Feature/api name suffix (#3918)
* added apiNameSuffix parameter to control the suffixes of API class/file/doc names * added --api-name-suffix in readme
1 parent 7c7fa68 commit ebc9e29

12 files changed

Lines changed: 97 additions & 22 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ NAME
455455
SYNOPSIS
456456
openapi-generator-cli generate
457457
[(-a <authorization> | --auth <authorization>)]
458+
[--api-name-suffix <api name suffix>]
458459
[--api-package <api package>] [--artifact-id <artifact id>]
459460
[--artifact-version <artifact version>]
460461
[(-c <configuration file> | --config <configuration file>)]

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class Generate implements Runnable {
6565
private String templateDir;
6666

6767
@Option(name = {"-e", "--engine"}, title = "templating engine",
68-
description = "templating engine: \"mustache\" (default) or \"handlebars\" (beta)")
68+
description = "templating engine: \"mustache\" (default) or \"handlebars\" (beta)")
6969
private String templatingEngine;
7070

7171
@Option(
@@ -109,6 +109,10 @@ public class Generate implements Runnable {
109109
description = CodegenConstants.MODEL_PACKAGE_DESC)
110110
private String modelPackage;
111111

112+
@Option(name = {"--api-name-suffix"}, title = "api name suffix",
113+
description = CodegenConstants.API_NAME_SUFFIX_DESC)
114+
private String apiNameSuffix;
115+
112116
@Option(name = {"--model-name-prefix"}, title = "model name prefix",
113117
description = CodegenConstants.MODEL_NAME_PREFIX_DESC)
114118
private String modelNamePrefix;
@@ -319,6 +323,10 @@ public void run() {
319323
configurator.setModelPackage(modelPackage);
320324
}
321325

326+
if (isNotEmpty(apiNameSuffix)) {
327+
configurator.setApiNameSuffix(apiNameSuffix);
328+
}
329+
322330
if (isNotEmpty(modelNamePrefix)) {
323331
configurator.setModelNamePrefix(modelNamePrefix);
324332
}

modules/openapi-generator-core/src/main/java/org/openapitools/codegen/config/GeneratorSettings.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public final class GeneratorSettings implements Serializable {
4040
private String modelPackage;
4141
private String invokerPackage;
4242
private String packageName;
43+
private String apiNameSuffix;
4344
private String modelNamePrefix;
4445
private String modelNameSuffix;
4546
private String groupId;
@@ -106,6 +107,21 @@ public String getPackageName() {
106107
return packageName;
107108
}
108109

110+
/**
111+
* Gets a api name suffix for generated models. This name will be appended to a api name.
112+
* <p>
113+
* This option is often used to circumvent compilation issues where models match keywords.
114+
* <p>
115+
* Example:
116+
* <p>
117+
* Suffix <code>Gen</code> applied to <code>Object</code> results in a generated class named <code>ObjectGen</code>.
118+
*
119+
* @return the model name suffix
120+
*/
121+
public String getApiNameSuffix() {
122+
return apiNameSuffix;
123+
}
124+
109125
/**
110126
* Gets a model name prefix for generated models. This name will be prefixed to a model name.
111127
* <p>
@@ -325,6 +341,7 @@ private GeneratorSettings(Builder builder) {
325341
modelPackage = builder.modelPackage;
326342
invokerPackage = builder.invokerPackage;
327343
packageName = builder.packageName;
344+
apiNameSuffix = builder.apiNameSuffix;
328345
modelNamePrefix = builder.modelNamePrefix;
329346
modelNameSuffix = builder.modelNameSuffix;
330347
groupId = builder.groupId;
@@ -366,6 +383,9 @@ private GeneratorSettings(Builder builder) {
366383
if (isNotEmpty(artifactVersion)) {
367384
additional.put("artifactVersion", artifactVersion);
368385
}
386+
if (isNotEmpty(apiNameSuffix)) {
387+
additional.put("apiNameSuffix", apiNameSuffix);
388+
}
369389
if (isNotEmpty(modelNamePrefix)) {
370390
additional.put("modelNamePrefix", modelNamePrefix);
371391
}
@@ -433,6 +453,7 @@ public static Builder newBuilder(GeneratorSettings copy) {
433453
builder.modelPackage = copy.getModelPackage();
434454
builder.invokerPackage = copy.getInvokerPackage();
435455
builder.packageName = copy.getPackageName();
456+
builder.apiNameSuffix = copy.getApiNameSuffix();
436457
builder.modelNamePrefix = copy.getModelNamePrefix();
437458
builder.modelNameSuffix = copy.getModelNameSuffix();
438459
builder.groupId = copy.getGroupId();
@@ -479,6 +500,7 @@ public static final class Builder {
479500
private String modelPackage;
480501
private String invokerPackage;
481502
private String packageName;
503+
private String apiNameSuffix;
482504
private String modelNamePrefix;
483505
private String modelNameSuffix;
484506
private String groupId;
@@ -571,6 +593,17 @@ public Builder withPackageName(String packageName) {
571593
return this;
572594
}
573595

596+
/**
597+
* Sets the {@code apiNameSuffix} and returns a reference to this Builder so that the methods can be chained together.
598+
*
599+
* @param apiNameSuffix the {@code apiNameSuffix} to set
600+
* @return a reference to this Builder
601+
*/
602+
public Builder withApiNameSuffix(String apiNameSuffix) {
603+
this.apiNameSuffix = apiNameSuffix;
604+
return this;
605+
}
606+
574607
/**
575608
* Sets the {@code modelNamePrefix} and returns a reference to this Builder so that the methods can be chained together.
576609
*
@@ -880,6 +913,7 @@ public String toString() {
880913
", modelPackage='" + modelPackage + '\'' +
881914
", invokerPackage='" + invokerPackage + '\'' +
882915
", packageName='" + packageName + '\'' +
916+
", apiNameSuffix='" + apiNameSuffix + '\'' +
883917
", modelNamePrefix='" + modelNamePrefix + '\'' +
884918
", modelNameSuffix='" + modelNameSuffix + '\'' +
885919
", groupId='" + groupId + '\'' +
@@ -910,6 +944,7 @@ public boolean equals(Object o) {
910944
Objects.equals(getModelPackage(), that.getModelPackage()) &&
911945
Objects.equals(getInvokerPackage(), that.getInvokerPackage()) &&
912946
Objects.equals(getPackageName(), that.getPackageName()) &&
947+
Objects.equals(getApiNameSuffix(), that.getApiNameSuffix()) &&
913948
Objects.equals(getModelNamePrefix(), that.getModelNamePrefix()) &&
914949
Objects.equals(getModelNameSuffix(), that.getModelNameSuffix()) &&
915950
Objects.equals(getGroupId(), that.getGroupId()) &&
@@ -937,6 +972,7 @@ public int hashCode() {
937972
getModelPackage(),
938973
getInvokerPackage(),
939974
getPackageName(),
975+
getApiNameSuffix(),
940976
getModelNamePrefix(),
941977
getModelNameSuffix(),
942978
getGroupId(),

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
206206
// Codegen constants should define a description and provide proper input validation for the value of serializationLibrary
207207
public static final String SERIALIZATION_LIBRARY = "serializationLibrary";
208208

209+
public static final String API_NAME_SUFFIX = "apiNameSuffix";
210+
public static final String API_NAME_SUFFIX_DESC = "Suffix that will be appended to all api names.";
211+
209212
public static final String MODEL_NAME_PREFIX = "modelNamePrefix";
210213
public static final String MODEL_NAME_PREFIX_DESC = "Prefix that will be prepended to all model names.";
211214

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class DefaultCodegen implements CodegenConfig {
8585
protected Map<String, String> importMapping = new HashMap<String, String>();
8686
protected String modelPackage = "", apiPackage = "", fileSuffix;
8787
protected String modelNamePrefix = "", modelNameSuffix = "";
88+
protected String apiNameSuffix = "Api";
8889
protected String testPackage = "";
8990
protected Map<String, String> apiTemplateFiles = new HashMap<String, String>();
9091
protected Map<String, String> modelTemplateFiles = new HashMap<String, String>();
@@ -180,6 +181,10 @@ public void processOpts() {
180181
.get(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS).toString()));
181182
}
182183

184+
if (additionalProperties.containsKey(CodegenConstants.API_NAME_SUFFIX)) {
185+
this.setApiNameSuffix((String) additionalProperties.get(CodegenConstants.API_NAME_SUFFIX));
186+
}
187+
183188
if (additionalProperties.containsKey(CodegenConstants.MODEL_NAME_PREFIX)) {
184189
this.setModelNamePrefix((String) additionalProperties.get(CodegenConstants.MODEL_NAME_PREFIX));
185190
}
@@ -779,6 +784,14 @@ public void setModelNameSuffix(String modelNameSuffix) {
779784
this.modelNameSuffix = modelNameSuffix;
780785
}
781786

787+
public String getApiNameSuffix() {
788+
return apiNameSuffix;
789+
}
790+
791+
public void setApiNameSuffix(String apiNameSuffix) {
792+
this.apiNameSuffix = apiNameSuffix;
793+
}
794+
782795
public void setApiPackage(String apiPackage) {
783796
this.apiPackage = apiPackage;
784797
}
@@ -1692,17 +1705,17 @@ public String toSetter(String name) {
16921705
}
16931706

16941707
/**
1695-
* Output the API (class) name (capitalized) ending with "Api"
1708+
* Output the API (class) name (capitalized) ending with the specified or default suffix
16961709
* Return DefaultApi if name is empty
16971710
*
16981711
* @param name the name of the Api
1699-
* @return capitalized Api name ending with "Api"
1712+
* @return capitalized Api name
17001713
*/
17011714
public String toApiName(String name) {
17021715
if (name.length() == 0) {
17031716
return "DefaultApi";
17041717
}
1705-
return camelize(name) + "Api";
1718+
return camelize(name + "_" + apiNameSuffix);
17061719
}
17071720

17081721
/**

modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ public CodegenConfigurator setLogToStderr(boolean logToStderr) {
287287
return this;
288288
}
289289

290+
public CodegenConfigurator setApiNameSuffix(String suffix) {
291+
generatorSettingsBuilder.withApiNameSuffix(suffix);
292+
return this;
293+
}
294+
290295
public CodegenConfigurator setModelNamePrefix(String prefix) {
291296
generatorSettingsBuilder.withModelNamePrefix(prefix);
292297
return this;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public String escapeUnsafeCharacters(String input) {
151151

152152
@Override
153153
public String toApiName(String type) {
154-
return sanitizeName(modelNamePrefix + Character.toUpperCase(type.charAt(0)) + type.substring(1) + "Api");
154+
return sanitizeName(modelNamePrefix + super.toApiName(type));
155155
}
156156

157157
@Override

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,10 @@ static Map<String, Object> jaxrsPostProcessOperations(Map<String, Object> objs)
244244
@Override
245245
public String toApiName(final String name) {
246246
String computed = name;
247-
if (computed.length() == 0) {
248-
return "DefaultApi";
247+
if (computed.length() > 0) {
248+
computed = sanitizeName(computed);
249249
}
250-
computed = sanitizeName(computed);
251-
return camelize(computed) + "Api";
250+
return super.toApiName(computed);
252251
}
253252

254253
@Override

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public String toApiFilename(String name) {
531531
name = name.replaceAll("-", "_");
532532

533533
// e.g. PhoneNumberApi.py => phone_number_api.py
534-
return underscore(name) + "_api";
534+
return underscore(name+ "_" + apiNameSuffix);
535535
}
536536

537537
@Override
@@ -541,19 +541,15 @@ public String toApiTestFilename(String name) {
541541

542542
@Override
543543
public String toApiName(String name) {
544-
if (name.length() == 0) {
545-
return "DefaultApi";
546-
}
547-
// e.g. phone_number_api => PhoneNumberApi
548-
return camelize(name) + "Api";
544+
return super.toApiName(name);
549545
}
550546

551547
@Override
552548
public String toApiVarName(String name) {
553549
if (name.length() == 0) {
554550
return "default_api";
555551
}
556-
return underscore(name) + "_api";
552+
return underscore(name+ "_" + apiNameSuffix);
557553
}
558554

559555
@Override

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public String toApiFilename(String name) {
387387
name = name.replaceAll("-", "_"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
388388

389389
// e.g. PhoneNumberApi.rb => phone_number_api.rb
390-
return underscore(name) + "_api";
390+
return underscore(name + "_" + apiNameSuffix);
391391
}
392392

393393
@Override
@@ -407,11 +407,7 @@ public String toModelTestFilename(String name) {
407407

408408
@Override
409409
public String toApiName(String name) {
410-
if (name.length() == 0) {
411-
return "DefaultApi";
412-
}
413-
// e.g. phone_number_api => PhoneNumberApi
414-
return camelize(name) + "Api";
410+
return super.toApiName(name);
415411
}
416412

417413
@Override

0 commit comments

Comments
 (0)