Skip to content

Commit

Permalink
Feature/api name suffix (OpenAPITools#3918)
Browse files Browse the repository at this point in the history
* added apiNameSuffix parameter to control the suffixes of API class/file/doc names

* added --api-name-suffix in readme
  • Loading branch information
bgong-mdsol authored and Jesse Michael committed Oct 3, 2019
1 parent 0fb7c9a commit 2c37400
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ NAME
SYNOPSIS
openapi-generator-cli generate
[(-a <authorization> | --auth <authorization>)]
[--api-name-suffix <api name suffix>]
[--api-package <api package>] [--artifact-id <artifact id>]
[--artifact-version <artifact version>]
[(-c <configuration file> | --config <configuration file>)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class Generate implements Runnable {
private String templateDir;

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

@Option(
Expand Down Expand Up @@ -109,6 +109,10 @@ public class Generate implements Runnable {
description = CodegenConstants.MODEL_PACKAGE_DESC)
private String modelPackage;

@Option(name = {"--api-name-suffix"}, title = "api name suffix",
description = CodegenConstants.API_NAME_SUFFIX_DESC)
private String apiNameSuffix;

@Option(name = {"--model-name-prefix"}, title = "model name prefix",
description = CodegenConstants.MODEL_NAME_PREFIX_DESC)
private String modelNamePrefix;
Expand Down Expand Up @@ -319,6 +323,10 @@ public void run() {
configurator.setModelPackage(modelPackage);
}

if (isNotEmpty(apiNameSuffix)) {
configurator.setApiNameSuffix(apiNameSuffix);
}

if (isNotEmpty(modelNamePrefix)) {
configurator.setModelNamePrefix(modelNamePrefix);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class GeneratorSettings implements Serializable {
private String modelPackage;
private String invokerPackage;
private String packageName;
private String apiNameSuffix;
private String modelNamePrefix;
private String modelNameSuffix;
private String groupId;
Expand Down Expand Up @@ -106,6 +107,21 @@ public String getPackageName() {
return packageName;
}

/**
* Gets a api name suffix for generated models. This name will be appended to a api name.
* <p>
* This option is often used to circumvent compilation issues where models match keywords.
* <p>
* Example:
* <p>
* Suffix <code>Gen</code> applied to <code>Object</code> results in a generated class named <code>ObjectGen</code>.
*
* @return the model name suffix
*/
public String getApiNameSuffix() {
return apiNameSuffix;
}

/**
* Gets a model name prefix for generated models. This name will be prefixed to a model name.
* <p>
Expand Down Expand Up @@ -325,6 +341,7 @@ private GeneratorSettings(Builder builder) {
modelPackage = builder.modelPackage;
invokerPackage = builder.invokerPackage;
packageName = builder.packageName;
apiNameSuffix = builder.apiNameSuffix;
modelNamePrefix = builder.modelNamePrefix;
modelNameSuffix = builder.modelNameSuffix;
groupId = builder.groupId;
Expand Down Expand Up @@ -366,6 +383,9 @@ private GeneratorSettings(Builder builder) {
if (isNotEmpty(artifactVersion)) {
additional.put("artifactVersion", artifactVersion);
}
if (isNotEmpty(apiNameSuffix)) {
additional.put("apiNameSuffix", apiNameSuffix);
}
if (isNotEmpty(modelNamePrefix)) {
additional.put("modelNamePrefix", modelNamePrefix);
}
Expand Down Expand Up @@ -433,6 +453,7 @@ public static Builder newBuilder(GeneratorSettings copy) {
builder.modelPackage = copy.getModelPackage();
builder.invokerPackage = copy.getInvokerPackage();
builder.packageName = copy.getPackageName();
builder.apiNameSuffix = copy.getApiNameSuffix();
builder.modelNamePrefix = copy.getModelNamePrefix();
builder.modelNameSuffix = copy.getModelNameSuffix();
builder.groupId = copy.getGroupId();
Expand Down Expand Up @@ -479,6 +500,7 @@ public static final class Builder {
private String modelPackage;
private String invokerPackage;
private String packageName;
private String apiNameSuffix;
private String modelNamePrefix;
private String modelNameSuffix;
private String groupId;
Expand Down Expand Up @@ -571,6 +593,17 @@ public Builder withPackageName(String packageName) {
return this;
}

/**
* Sets the {@code apiNameSuffix} and returns a reference to this Builder so that the methods can be chained together.
*
* @param apiNameSuffix the {@code apiNameSuffix} to set
* @return a reference to this Builder
*/
public Builder withApiNameSuffix(String apiNameSuffix) {
this.apiNameSuffix = apiNameSuffix;
return this;
}

/**
* Sets the {@code modelNamePrefix} and returns a reference to this Builder so that the methods can be chained together.
*
Expand Down Expand Up @@ -880,6 +913,7 @@ public String toString() {
", modelPackage='" + modelPackage + '\'' +
", invokerPackage='" + invokerPackage + '\'' +
", packageName='" + packageName + '\'' +
", apiNameSuffix='" + apiNameSuffix + '\'' +
", modelNamePrefix='" + modelNamePrefix + '\'' +
", modelNameSuffix='" + modelNameSuffix + '\'' +
", groupId='" + groupId + '\'' +
Expand Down Expand Up @@ -910,6 +944,7 @@ public boolean equals(Object o) {
Objects.equals(getModelPackage(), that.getModelPackage()) &&
Objects.equals(getInvokerPackage(), that.getInvokerPackage()) &&
Objects.equals(getPackageName(), that.getPackageName()) &&
Objects.equals(getApiNameSuffix(), that.getApiNameSuffix()) &&
Objects.equals(getModelNamePrefix(), that.getModelNamePrefix()) &&
Objects.equals(getModelNameSuffix(), that.getModelNameSuffix()) &&
Objects.equals(getGroupId(), that.getGroupId()) &&
Expand Down Expand Up @@ -937,6 +972,7 @@ public int hashCode() {
getModelPackage(),
getInvokerPackage(),
getPackageName(),
getApiNameSuffix(),
getModelNamePrefix(),
getModelNameSuffix(),
getGroupId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
// Codegen constants should define a description and provide proper input validation for the value of serializationLibrary
public static final String SERIALIZATION_LIBRARY = "serializationLibrary";

public static final String API_NAME_SUFFIX = "apiNameSuffix";
public static final String API_NAME_SUFFIX_DESC = "Suffix that will be appended to all api names.";

public static final String MODEL_NAME_PREFIX = "modelNamePrefix";
public static final String MODEL_NAME_PREFIX_DESC = "Prefix that will be prepended to all model names.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class DefaultCodegen implements CodegenConfig {
protected Map<String, String> importMapping = new HashMap<String, String>();
protected String modelPackage = "", apiPackage = "", fileSuffix;
protected String modelNamePrefix = "", modelNameSuffix = "";
protected String apiNameSuffix = "Api";
protected String testPackage = "";
protected Map<String, String> apiTemplateFiles = new HashMap<String, String>();
protected Map<String, String> modelTemplateFiles = new HashMap<String, String>();
Expand Down Expand Up @@ -180,6 +181,10 @@ public void processOpts() {
.get(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS).toString()));
}

if (additionalProperties.containsKey(CodegenConstants.API_NAME_SUFFIX)) {
this.setApiNameSuffix((String) additionalProperties.get(CodegenConstants.API_NAME_SUFFIX));
}

if (additionalProperties.containsKey(CodegenConstants.MODEL_NAME_PREFIX)) {
this.setModelNamePrefix((String) additionalProperties.get(CodegenConstants.MODEL_NAME_PREFIX));
}
Expand Down Expand Up @@ -779,6 +784,14 @@ public void setModelNameSuffix(String modelNameSuffix) {
this.modelNameSuffix = modelNameSuffix;
}

public String getApiNameSuffix() {
return apiNameSuffix;
}

public void setApiNameSuffix(String apiNameSuffix) {
this.apiNameSuffix = apiNameSuffix;
}

public void setApiPackage(String apiPackage) {
this.apiPackage = apiPackage;
}
Expand Down Expand Up @@ -1692,17 +1705,17 @@ public String toSetter(String name) {
}

/**
* Output the API (class) name (capitalized) ending with "Api"
* Output the API (class) name (capitalized) ending with the specified or default suffix
* Return DefaultApi if name is empty
*
* @param name the name of the Api
* @return capitalized Api name ending with "Api"
* @return capitalized Api name
*/
public String toApiName(String name) {
if (name.length() == 0) {
return "DefaultApi";
}
return camelize(name) + "Api";
return camelize(name + "_" + apiNameSuffix);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ public CodegenConfigurator setLogToStderr(boolean logToStderr) {
return this;
}

public CodegenConfigurator setApiNameSuffix(String suffix) {
generatorSettingsBuilder.withApiNameSuffix(suffix);
return this;
}

public CodegenConfigurator setModelNamePrefix(String prefix) {
generatorSettingsBuilder.withModelNamePrefix(prefix);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public String escapeUnsafeCharacters(String input) {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,10 @@ static Map<String, Object> jaxrsPostProcessOperations(Map<String, Object> objs)
@Override
public String toApiName(final String name) {
String computed = name;
if (computed.length() == 0) {
return "DefaultApi";
if (computed.length() > 0) {
computed = sanitizeName(computed);
}
computed = sanitizeName(computed);
return camelize(computed) + "Api";
return super.toApiName(computed);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public String toApiFilename(String name) {
name = name.replaceAll("-", "_");

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

@Override
Expand All @@ -541,19 +541,15 @@ public String toApiTestFilename(String name) {

@Override
public String toApiName(String name) {
if (name.length() == 0) {
return "DefaultApi";
}
// e.g. phone_number_api => PhoneNumberApi
return camelize(name) + "Api";
return super.toApiName(name);
}

@Override
public String toApiVarName(String name) {
if (name.length() == 0) {
return "default_api";
}
return underscore(name) + "_api";
return underscore(name+ "_" + apiNameSuffix);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public String toApiFilename(String name) {
name = name.replaceAll("-", "_"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.

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

@Override
Expand All @@ -407,11 +407,7 @@ public String toModelTestFilename(String name) {

@Override
public String toApiName(String name) {
if (name.length() == 0) {
return "DefaultApi";
}
// e.g. phone_number_api => PhoneNumberApi
return camelize(name) + "Api";
return super.toApiName(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -968,4 +969,19 @@ public void commonLambdasRegistrationTest() {
assertTrue(lambdas.get("indented_16") instanceof IndentedLambda, "Expecting IndentedLambda class");
}

@Test
public void convertApiNameWithEmptySuffix() {
DefaultCodegen codegen = new DefaultCodegen();
assertEquals(codegen.toApiName("Fake"), "FakeApi");
assertEquals(codegen.toApiName(""), "DefaultApi");
}

@Test
public void convertApiNameWithSuffix() {
DefaultCodegen codegen = new DefaultCodegen();
codegen.setApiNameSuffix("Test");
assertEquals(codegen.toApiName("Fake"), "FakeTest");
assertEquals(codegen.toApiName(""), "DefaultApi");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void shouldSetConfiglProperties() throws IOException {
.setGitHost("test.com")
.setGroupId("group")
.setHttpUserAgent("agent")
.setApiNameSuffix("api-suffix")
.setModelNamePrefix("model-prefix")
.setModelNameSuffix("model-suffix")
.setModelPackage("model-package")
Expand All @@ -100,6 +101,7 @@ public void shouldSetConfiglProperties() throws IOException {
want(props, CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, false);
want(props, CodegenConstants.ENSURE_UNIQUE_PARAMS, true);
want(props, CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, true);
want(props, CodegenConstants.API_NAME_SUFFIX, "api-suffix");
want(props, CodegenConstants.MODEL_NAME_PREFIX, "model-prefix");
want(props, CodegenConstants.MODEL_NAME_SUFFIX, "model-suffix");
want(props, CodegenConstants.REMOVE_OPERATION_ID_PREFIX, false);
Expand Down

0 comments on commit 2c37400

Please sign in to comment.