forked from loafle/openapi-generator-original
Merge pull request #2211 from ePaul/feature/1255-suffix+prefix-for-model-name
Feature/1255 suffix+prefix for model name
This commit is contained in:
commit
ce83a90331
@ -73,6 +73,12 @@ public class Generate implements Runnable {
|
|||||||
@Option(name = {"--model-package"}, title = "model package", description = CodegenConstants.MODEL_PACKAGE_DESC)
|
@Option(name = {"--model-package"}, title = "model package", description = CodegenConstants.MODEL_PACKAGE_DESC)
|
||||||
private String modelPackage;
|
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." +
|
@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.")
|
"For example (in Java): array=ArrayList,map=HashMap. In other words array types will get instantiated as ArrayList in generated code.")
|
||||||
private String instantiationTypes;
|
private String instantiationTypes;
|
||||||
@ -156,6 +162,14 @@ public class Generate implements Runnable {
|
|||||||
configurator.setModelPackage(modelPackage);
|
configurator.setModelPackage(modelPackage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(isNotEmpty(modelNamePrefix)){
|
||||||
|
configurator.setModelNamePrefix(modelNamePrefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isNotEmpty(modelNameSuffix)){
|
||||||
|
configurator.setModelNameSuffix(modelNameSuffix);
|
||||||
|
}
|
||||||
|
|
||||||
if(isNotEmpty(invokerPackage)) {
|
if(isNotEmpty(invokerPackage)) {
|
||||||
configurator.setInvokerPackage(invokerPackage);
|
configurator.setInvokerPackage(invokerPackage);
|
||||||
}
|
}
|
||||||
|
@ -82,4 +82,9 @@ public class CodegenConstants {
|
|||||||
|
|
||||||
public static enum MODEL_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case, original}
|
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.";
|
||||||
|
|
||||||
|
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.";
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@ public class DefaultCodegen {
|
|||||||
protected Set<String> languageSpecificPrimitives = new HashSet<String>();
|
protected Set<String> languageSpecificPrimitives = new HashSet<String>();
|
||||||
protected Map<String, String> importMapping = new HashMap<String, String>();
|
protected Map<String, String> importMapping = new HashMap<String, String>();
|
||||||
protected String modelPackage = "", apiPackage = "", fileSuffix;
|
protected String modelPackage = "", apiPackage = "", fileSuffix;
|
||||||
|
protected String modelNamePrefix = "", modelNameSuffix = "";
|
||||||
protected String testPackage = "";
|
protected String testPackage = "";
|
||||||
protected Map<String, String> apiTemplateFiles = new HashMap<String, String>();
|
protected Map<String, String> apiTemplateFiles = new HashMap<String, String>();
|
||||||
protected Map<String, String> modelTemplateFiles = new HashMap<String, String>();
|
protected Map<String, String> modelTemplateFiles = new HashMap<String, String>();
|
||||||
@ -107,6 +108,14 @@ public class DefaultCodegen {
|
|||||||
this.setEnsureUniqueParams(Boolean.valueOf(additionalProperties
|
this.setEnsureUniqueParams(Boolean.valueOf(additionalProperties
|
||||||
.get(CodegenConstants.ENSURE_UNIQUE_PARAMS).toString()));
|
.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
|
// override with any special post-processing for all models
|
||||||
@ -283,6 +292,14 @@ public class DefaultCodegen {
|
|||||||
this.modelPackage = 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) {
|
public void setApiPackage(String apiPackage) {
|
||||||
this.apiPackage = apiPackage;
|
this.apiPackage = apiPackage;
|
||||||
}
|
}
|
||||||
@ -826,8 +843,8 @@ public class DefaultCodegen {
|
|||||||
* @param name the name of the model
|
* @param name the name of the model
|
||||||
* @return capitalized model name
|
* @return capitalized model name
|
||||||
*/
|
*/
|
||||||
public String toModelName(String name) {
|
public String toModelName(final String name) {
|
||||||
return initialCaps(name);
|
return initialCaps(modelNamePrefix + name + modelNameSuffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,6 +48,8 @@ public class CodegenConfigurator {
|
|||||||
private String apiPackage;
|
private String apiPackage;
|
||||||
private String modelPackage;
|
private String modelPackage;
|
||||||
private String invokerPackage;
|
private String invokerPackage;
|
||||||
|
private String modelNamePrefix;
|
||||||
|
private String modelNameSuffix;
|
||||||
private String groupId;
|
private String groupId;
|
||||||
private String artifactId;
|
private String artifactId;
|
||||||
private String artifactVersion;
|
private String artifactVersion;
|
||||||
@ -97,6 +99,24 @@ public class CodegenConfigurator {
|
|||||||
return this;
|
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() {
|
public boolean isVerbose() {
|
||||||
return verbose;
|
return verbose;
|
||||||
}
|
}
|
||||||
@ -300,6 +320,8 @@ public class CodegenConfigurator {
|
|||||||
checkAndSetAdditionalProperty(artifactId, CodegenConstants.ARTIFACT_ID);
|
checkAndSetAdditionalProperty(artifactId, CodegenConstants.ARTIFACT_ID);
|
||||||
checkAndSetAdditionalProperty(artifactVersion, CodegenConstants.ARTIFACT_VERSION);
|
checkAndSetAdditionalProperty(artifactVersion, CodegenConstants.ARTIFACT_VERSION);
|
||||||
checkAndSetAdditionalProperty(templateDir, toAbsolutePathStr(templateDir), CodegenConstants.TEMPLATE_DIR);
|
checkAndSetAdditionalProperty(templateDir, toAbsolutePathStr(templateDir), CodegenConstants.TEMPLATE_DIR);
|
||||||
|
checkAndSetAdditionalProperty(modelNamePrefix, CodegenConstants.MODEL_NAME_PREFIX);
|
||||||
|
checkAndSetAdditionalProperty(modelNameSuffix, CodegenConstants.MODEL_NAME_SUFFIX);
|
||||||
|
|
||||||
handleDynamicProperties(config);
|
handleDynamicProperties(config);
|
||||||
|
|
||||||
|
@ -468,7 +468,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toModelName(String name) {
|
public String toModelName(String name) {
|
||||||
name = sanitizeName(name);
|
name = sanitizeName(modelNamePrefix + name + modelNameSuffix);
|
||||||
|
|
||||||
// model name cannot use reserved keyword, e.g. return
|
// model name cannot use reserved keyword, e.g. return
|
||||||
if (isReservedWord(name)) {
|
if (isReservedWord(name)) {
|
||||||
|
@ -376,21 +376,21 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toModelName(String name) {
|
public String toModelName(final String name) {
|
||||||
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
final String sanitizedName = sanitizeName(modelNamePrefix + name + modelNameSuffix);
|
||||||
|
|
||||||
// camelize the model name
|
// camelize the model name
|
||||||
// phone_number => PhoneNumber
|
// phone_number => PhoneNumber
|
||||||
name = camelize(name);
|
final String camelizedName = camelize(sanitizedName);
|
||||||
|
|
||||||
// model name cannot use reserved keyword, e.g. return
|
// model name cannot use reserved keyword, e.g. return
|
||||||
if (isReservedWord(name)) {
|
if (isReservedWord(camelizedName)) {
|
||||||
String modelName = "Object" + name;
|
final String modelName = "Object" + camelizedName;
|
||||||
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + modelName);
|
LOGGER.warn(camelizedName + " (reserved word) cannot be used as model name. Renamed to " + modelName);
|
||||||
return modelName;
|
return modelName;
|
||||||
}
|
}
|
||||||
|
|
||||||
return name;
|
return camelizedName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -486,7 +486,8 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
String type = null;
|
String type = null;
|
||||||
if (typeMapping.containsKey(swaggerType)) {
|
if (typeMapping.containsKey(swaggerType)) {
|
||||||
type = typeMapping.get(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;
|
return type;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user