Skip to content

Feature/1255 suffix+prefix for model name #2211

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

Merged
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 @@ -73,6 +73,12 @@ public class Generate implements Runnable {
@Option(name = {"--model-package"}, title = "model package", description = CodegenConstants.MODEL_PACKAGE_DESC)
private String modelPackage;

@Option(name = {"--model-name-prefix"}, title = "model name prefix", description = CodegenConstants.MODEL_NAME_PREFIX_DESC)
private String modelNamePrefix;

@Option(name = {"--model-name-suffix"}, title = "model name suffix", description = CodegenConstants.MODEL_NAME_SUFFIX_DESC)
private String modelNameSuffix;

@Option(name = {"--instantiation-types"}, title = "instantiation types", description = "sets instantiation type mappings in the format of type=instantiatedType,type=instantiatedType." +
"For example (in Java): array=ArrayList,map=HashMap. In other words array types will get instantiated as ArrayList in generated code.")
private String instantiationTypes;
Expand Down Expand Up @@ -156,6 +162,14 @@ public void run() {
configurator.setModelPackage(modelPackage);
}

if(isNotEmpty(modelNamePrefix)){
configurator.setModelNamePrefix(modelNamePrefix);
}

if(isNotEmpty(modelNameSuffix)){
configurator.setModelNameSuffix(modelNameSuffix);
}

if(isNotEmpty(invokerPackage)) {
configurator.setInvokerPackage(invokerPackage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ public class CodegenConstants {

public static enum MODEL_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case, original}

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. Default is the empty string.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you also aware that another way to add the default value is .defaultValue in CliOption (example)

In this case, I'm fine with both approaches as the default is an empty string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true for CliOption, but I don't think it would work the same for maven/airline cli options, e.g.:

 @Option(name = {"--model-name-suffix"}, 
         title = "model name suffix", 
         description = CodegenConstants.MODEL_NAME_SUFFIX_DESC)

Airline, for example, only has allowedValues.

edit: probably better in this case to just not say there's a default since it doesn't matter to the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better If the option is not given, nothing will be prepended. (and analogously for the suffix)?


public static final String MODEL_NAME_SUFFIX = "modelNameSuffix";
public static final String MODEL_NAME_SUFFIX_DESC = "Suffix that will be appended to all model names. Default is the empty string.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class DefaultCodegen {
protected Set<String> languageSpecificPrimitives = new HashSet<String>();
protected Map<String, String> importMapping = new HashMap<String, String>();
protected String modelPackage = "", apiPackage = "", fileSuffix;
protected String modelNamePrefix = "", modelNameSuffix = "";
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 @@ -107,6 +108,14 @@ public void processOpts() {
this.setEnsureUniqueParams(Boolean.valueOf(additionalProperties
.get(CodegenConstants.ENSURE_UNIQUE_PARAMS).toString()));
}

if(additionalProperties.containsKey(CodegenConstants.MODEL_NAME_PREFIX)){
this.setModelNamePrefix((String) additionalProperties.get(CodegenConstants.MODEL_NAME_PREFIX));
}

if(additionalProperties.containsKey(CodegenConstants.MODEL_NAME_SUFFIX)){
this.setModelNameSuffix((String) additionalProperties.get(CodegenConstants.MODEL_NAME_SUFFIX));
}
}

// override with any special post-processing for all models
Expand Down Expand Up @@ -283,6 +292,14 @@ public void setModelPackage(String modelPackage) {
this.modelPackage = modelPackage;
}

public void setModelNamePrefix(String modelNamePrefix){
this.modelNamePrefix = modelNamePrefix;
}

public void setModelNameSuffix(String modelNameSuffix){
this.modelNameSuffix = modelNameSuffix;
}

public void setApiPackage(String apiPackage) {
this.apiPackage = apiPackage;
}
Expand Down Expand Up @@ -826,8 +843,8 @@ public String toApiName(String name) {
* @param name the name of the model
* @return capitalized model name
*/
public String toModelName(String name) {
return initialCaps(name);
public String toModelName(final String name) {
return initialCaps(modelNamePrefix + name + modelNameSuffix);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class CodegenConfigurator {
private String apiPackage;
private String modelPackage;
private String invokerPackage;
private String modelNamePrefix;
private String modelNameSuffix;
private String groupId;
private String artifactId;
private String artifactVersion;
Expand Down Expand Up @@ -97,6 +99,24 @@ public CodegenConfigurator setModelPackage(String modelPackage) {
return this;
}

public String getModelNamePrefix() {
return modelNamePrefix;
}

public CodegenConfigurator setModelNamePrefix(String prefix) {
this.modelNamePrefix = prefix;
return this;
}

public String getModelNameSuffix() {
return modelNameSuffix;
}

public CodegenConfigurator setModelNameSuffix(String suffix) {
this.modelNameSuffix = suffix;
return this;
}

public boolean isVerbose() {
return verbose;
}
Expand Down Expand Up @@ -300,6 +320,8 @@ public ClientOptInput toClientOptInput() {
checkAndSetAdditionalProperty(artifactId, CodegenConstants.ARTIFACT_ID);
checkAndSetAdditionalProperty(artifactVersion, CodegenConstants.ARTIFACT_VERSION);
checkAndSetAdditionalProperty(templateDir, toAbsolutePathStr(templateDir), CodegenConstants.TEMPLATE_DIR);
checkAndSetAdditionalProperty(modelNamePrefix, CodegenConstants.MODEL_NAME_PREFIX);
checkAndSetAdditionalProperty(modelNameSuffix, CodegenConstants.MODEL_NAME_SUFFIX);

handleDynamicProperties(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public String getTypeDeclaration(Property p) {

@Override
public String toModelName(String name) {
name = sanitizeName(name);
name = sanitizeName(modelNamePrefix + name + modelNameSuffix);

// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,21 +376,21 @@ public String toParamName(String name) {
}

@Override
public String toModelName(String name) {
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
public String toModelName(final String name) {
final String sanitizedName = sanitizeName(modelNamePrefix + name + modelNameSuffix);

// camelize the model name
// phone_number => PhoneNumber
name = camelize(name);
final String camelizedName = camelize(sanitizedName);

// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
String modelName = "Object" + name;
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + modelName);
if (isReservedWord(camelizedName)) {
final String modelName = "Object" + camelizedName;
LOGGER.warn(camelizedName + " (reserved word) cannot be used as model name. Renamed to " + modelName);
return modelName;
}

return name;
return camelizedName;
}

@Override
Expand Down Expand Up @@ -486,7 +486,8 @@ public String getSwaggerType(Property p) {
String type = null;
if (typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
if (languageSpecificPrimitives.contains(type) || type.indexOf(".") >= 0) {
if (languageSpecificPrimitives.contains(type) || type.indexOf(".") >= 0 ||
type.equals("Map") || type.equals("List")) {
return type;
}
} else {
Expand Down