Swagr code-gen: Refactoring method names in mapper and generator interfaces

This commit is contained in:
Deepak Michael 2011-07-23 07:57:46 +05:30
parent 63c7932a67
commit 5ec7c8b40e
10 changed files with 39 additions and 39 deletions

View File

@ -333,20 +333,20 @@ public class DriverCodeGenerator {
model.setFields(modelFields); model.setFields(modelFields);
for(String className : generatedClassNames){ for(String className : generatedClassNames){
ModelField aParam = new ModelField(); ModelField aParam = new ModelField();
aParam.setName(config.getNameGenerator().convertToMethodNameFormat(className)+"List"); aParam.setName(config.getNameGenerator().applyMethodNamingPolicy(className)+"List");
aParam.setParamType(config.getDataTypeMapper().getListReturnType(className)); aParam.setParamType(config.getDataTypeMapper().getListReturnTypeSignature(className));
modelFields.add(aParam); modelFields.add(aParam);
} }
//add missing class from models //add missing class from models
ModelField aParam = new ModelField(); ModelField aParam = new ModelField();
aParam.setName("StringValueList"); aParam.setName("StringValueList");
aParam.setParamType(config.getDataTypeMapper().getListReturnType("StringValue")); aParam.setParamType(config.getDataTypeMapper().getListReturnTypeSignature("StringValue"));
modelFields.add(aParam); modelFields.add(aParam);
List<String> imports = new ArrayList<String>(); List<String> imports = new ArrayList<String>();
imports.addAll(this.config.getDefaultModelImports()); imports.addAll(this.config.getDefaultModelImports());
imports.addAll(this.config.getDataTypeMapper().getListImports()); imports.addAll(this.config.getDataTypeMapper().getListImportPackages());
for(ModelField param : model.getFields()){ for(ModelField param : model.getFields()){
for(String importDef : param.getFieldDefinition(config.getDataTypeMapper()).getImportDefinitions()){ for(String importDef : param.getFieldDefinition(config.getDataTypeMapper()).getImportDefinitions()){
if(!imports.contains(importDef)){ if(!imports.contains(importDef)){

View File

@ -56,7 +56,7 @@ public class MethodArgument {
} }
public void setInputModelClassArgument(String inputModelClass, CodeGenConfig config) { public void setInputModelClassArgument(String inputModelClass, CodeGenConfig config) {
this.inputModelClassArgument = config.getNameGenerator().convertToMethodNameFormat(inputModelClass); this.inputModelClassArgument = config.getNameGenerator().applyMethodNamingPolicy(inputModelClass);
if(name != null) { if(name != null) {
methodNameFromModelClass = config.getNameGenerator().createGetterMethodName(inputModelClassArgument, name); methodNameFromModelClass = config.getNameGenerator().createGetterMethodName(inputModelClassArgument, name);
} }

View File

@ -39,7 +39,7 @@ public interface DataTypeMapper {
* @param typeClass of class that list object contains. * @param typeClass of class that list object contains.
* @return * @return
*/ */
public String getListReturnType(String typeClass); public String getListReturnTypeSignature(String typeClass);
/** /**
* Signature that should be used when returning map of given object type. * Signature that should be used when returning map of given object type.
@ -48,7 +48,7 @@ public interface DataTypeMapper {
* @param typeClass of class that list object contains. * @param typeClass of class that list object contains.
* @return * @return
*/ */
public String getMapReturnType(String typeClass); public String getMapReturnTypeSignature(String typeClass);
/** /**
* Initialization need for list objects. Example. If it is java list the initialization will look as * Initialization need for list objects. Example. If it is java list the initialization will look as
@ -60,7 +60,7 @@ public interface DataTypeMapper {
* @param typeClass * @param typeClass
* @return * @return
*/ */
public String getListInitialization(String typeClass); public String generateListInitialization(String typeClass);
/** /**
* Initialization need for map objects. Example. If it is java list the initialization will look as * Initialization need for map objects. Example. If it is java list the initialization will look as
@ -72,7 +72,7 @@ public interface DataTypeMapper {
* @param typeClass * @param typeClass
* @return * @return
*/ */
public String getMapInitialization(String typeClass); public String generateMapInitialization(String typeClass);
/** /**
* Gets list of imports that needs to be included when used objects of type List. * Gets list of imports that needs to be included when used objects of type List.
@ -86,7 +86,7 @@ public interface DataTypeMapper {
* </Code> * </Code>
* @return * @return
*/ */
public List<String> getListImports(); public List<String> getListImportPackages();
/** /**
* Gets list of imports that needs to be included when used objects of type Map. * Gets list of imports that needs to be included when used objects of type Map.
@ -100,7 +100,7 @@ public interface DataTypeMapper {
* </Code> * </Code>
* @return * @return
*/ */
public List<String> getMapImports(); public List<String> getMapImportPackages();
/** /**
* Gets list of imports that needs to be included when used objects of type Date. * Gets list of imports that needs to be included when used objects of type Date.

View File

@ -29,7 +29,7 @@ public interface ServiceAndMethodNameGenerator {
* @param input * @param input
* @return * @return
*/ */
public String convertToClassNameFormat(String input); public String applyClassNamingPolicy(String input);
/** /**
* Transform the input string into method naming convention format. * Transform the input string into method naming convention format.
@ -39,7 +39,7 @@ public interface ServiceAndMethodNameGenerator {
* @param input * @param input
* @return * @return
*/ */
public String convertToMethodNameFormat(String input); public String applyMethodNamingPolicy(String input);
/** /**
* Generates the name of service based on resource path. * Generates the name of service based on resource path.

View File

@ -74,38 +74,38 @@ public class JavaDataTypeMapper implements DataTypeMapper {
return primitiveValueMap.get(type); return primitiveValueMap.get(type);
} }
}else{ }else{
return nameGenerator.convertToClassNameFormat(type); return nameGenerator.applyClassNamingPolicy(type);
} }
} }
public String getListReturnType(String typeClass) { public String getListReturnTypeSignature(String typeClass) {
return "List<"+nameGenerator.convertToClassNameFormat(typeClass)+">"; return "List<"+nameGenerator.applyClassNamingPolicy(typeClass)+">";
} }
public String getReturnTypeForVoidMethods() { public String getReturnTypeForVoidMethods() {
return "void"; return "void";
} }
public String getMapReturnType(String typeClass) { public String getMapReturnTypeSignature(String typeClass) {
return "Map<"+nameGenerator.convertToClassNameFormat(typeClass)+">"; return "Map<"+nameGenerator.applyClassNamingPolicy(typeClass)+">";
} }
public String getListInitialization(String typeClass) { public String generateListInitialization(String typeClass) {
return " new ArrayList<"+nameGenerator.convertToClassNameFormat(typeClass)+">()"; return " new ArrayList<"+nameGenerator.applyClassNamingPolicy(typeClass)+">()";
} }
public String getMapInitialization(String typeClass) { public String generateMapInitialization(String typeClass) {
return " new HashMap<"+nameGenerator.convertToClassNameFormat(typeClass)+">()"; return " new HashMap<"+nameGenerator.applyClassNamingPolicy(typeClass)+">()";
} }
public List<String> getListImports() { public List<String> getListImportPackages() {
List<String> imports = new ArrayList<String>(); List<String> imports = new ArrayList<String>();
imports.add("java.util.List"); imports.add("java.util.List");
imports.add("java.util.ArrayList"); imports.add("java.util.ArrayList");
return imports; return imports;
} }
public List<String> getMapImports() { public List<String> getMapImportPackages() {
List<String> imports = new ArrayList<String>(); List<String> imports = new ArrayList<String>();
imports.add("java.util.Map"); imports.add("java.util.Map");
imports.add("java.util.HashMap"); imports.add("java.util.HashMap");

View File

@ -25,7 +25,7 @@ public class JavaServiceAndMethodNameGenerator implements ServiceAndMethodNameGe
* @param input * @param input
* @return * @return
*/ */
public String convertToClassNameFormat(String input) { public String applyClassNamingPolicy(String input) {
if(input != null && input.length() > 0) { if(input != null && input.length() > 0) {
return input.substring(0,1).toUpperCase() + input.substring(1); return input.substring(0,1).toUpperCase() + input.substring(1);
}else{ }else{
@ -39,7 +39,7 @@ public class JavaServiceAndMethodNameGenerator implements ServiceAndMethodNameGe
* @param input * @param input
* @return * @return
*/ */
public String convertToMethodNameFormat(String input) { public String applyMethodNamingPolicy(String input) {
if(input != null && input.length() > 0) { if(input != null && input.length() > 0) {
return input.substring(0,1).toLowerCase() + input.substring(1); return input.substring(0,1).toLowerCase() + input.substring(1);
}else{ }else{
@ -52,12 +52,12 @@ public class JavaServiceAndMethodNameGenerator implements ServiceAndMethodNameGe
int index = resourcePath.indexOf("."); int index = resourcePath.indexOf(".");
if(index >= 0) { if(index >= 0) {
String resourceName = resourcePath.substring(1,index); String resourceName = resourcePath.substring(1,index);
className = convertToClassNameFormat(resourceName); className = applyClassNamingPolicy(resourceName);
}else{ }else{
String[] paths = resourcePath.split("/"); String[] paths = resourcePath.split("/");
for(String path : paths) { for(String path : paths) {
if(path != null && path.length() > 0) { if(path != null && path.length() > 0) {
className = convertToClassNameFormat(path); className = applyClassNamingPolicy(path);
break; break;
} }
} }
@ -92,7 +92,7 @@ public class JavaServiceAndMethodNameGenerator implements ServiceAndMethodNameGe
if(pathElement != null && pathElement.length() > 0) { if(pathElement != null && pathElement.length() > 0) {
int position = pathElement.indexOf("{"); int position = pathElement.indexOf("{");
if(position < 0) { if(position < 0) {
inputobjectName = inputobjectName + convertToClassNameFormat(pathElement) + Model.INPUT_OBJECT_SUFFIX; inputobjectName = inputobjectName + applyClassNamingPolicy(pathElement) + Model.INPUT_OBJECT_SUFFIX;
} }
} }
} }
@ -111,7 +111,7 @@ public class JavaServiceAndMethodNameGenerator implements ServiceAndMethodNameGe
* @return * @return
*/ */
public String createGetterMethodName(String className, String attributeName) { public String createGetterMethodName(String className, String attributeName) {
return className+".get"+convertToClassNameFormat(attributeName)+"()"; return className+".get"+ applyClassNamingPolicy(attributeName)+"()";
} }
} }

View File

@ -52,7 +52,7 @@ public class ApiPropertyListWrapper implements Serializable
if(propertyDefn.getItems().getAdditionalProperties().get("$ref") != null) { if(propertyDefn.getItems().getAdditionalProperties().get("$ref") != null) {
arrayItemType = (String) propertyDefn.getItems().getAdditionalProperties().get("$ref"); arrayItemType = (String) propertyDefn.getItems().getAdditionalProperties().get("$ref");
} }
field.setParamType("List[" + config.getNameGenerator().convertToClassNameFormat(arrayItemType) + "]"); field.setParamType("List[" + config.getNameGenerator().applyClassNamingPolicy(arrayItemType) + "]");
} }
field.setDefaultValue(propertyDefn.getDefaultValue()); field.setDefaultValue(propertyDefn.getDefaultValue());
field.setInternalDescription(propertyDefn.getNotes()); field.setInternalDescription(propertyDefn.getNotes());

View File

@ -271,7 +271,7 @@ public class EndpointOperation {
MethodArgument anArgument = new MethodArgument(); MethodArgument anArgument = new MethodArgument();
anArgument.setDataType(inputobjectName); anArgument.setDataType(inputobjectName);
anArgument.setName(config.getNameGenerator().convertToMethodNameFormat(inputobjectName)); anArgument.setName(config.getNameGenerator().applyMethodNamingPolicy(inputobjectName));
arguments.add(anArgument); arguments.add(anArgument);
method.setArguments(arguments); method.setArguments(arguments);
method.setInputModel(modelforMethodInput); method.setInputModel(modelforMethodInput);

View File

@ -148,12 +148,12 @@ public class ModelField {
fieldDefinition.getImportDefinitions().add("java.util.Date"); fieldDefinition.getImportDefinitions().add("java.util.Date");
} }
if(type.startsWith("List[")){ if(type.startsWith("List[")){
fieldDefinition.getImportDefinitions().addAll(dataTypeMapper.getListImports()); fieldDefinition.getImportDefinitions().addAll(dataTypeMapper.getListImportPackages());
String entryType = type.substring(5, type.length()-1); String entryType = type.substring(5, type.length()-1);
entryType = dataTypeMapper.getObjectType(entryType, true); entryType = dataTypeMapper.getObjectType(entryType, true);
String returnType = dataTypeMapper.getListReturnType(entryType); String returnType = dataTypeMapper.getListReturnTypeSignature(entryType);
fieldDefinition.setReturnType(returnType); fieldDefinition.setReturnType(returnType);
fieldDefinition.setInitialization(" = " + dataTypeMapper.getListInitialization(entryType)); fieldDefinition.setInitialization(" = " + dataTypeMapper.generateListInitialization(entryType));
if(this.getWrapperName() != null){ if(this.getWrapperName() != null){
fieldDefinition.setName(this.getWrapperName()); fieldDefinition.setName(this.getWrapperName());
}else{ }else{
@ -161,16 +161,16 @@ public class ModelField {
} }
}else if (type.startsWith("Map[")) { }else if (type.startsWith("Map[")) {
fieldDefinition.getImportDefinitions().addAll(dataTypeMapper.getMapImports()); fieldDefinition.getImportDefinitions().addAll(dataTypeMapper.getMapImportPackages());
String keyClass, entryClass = ""; String keyClass, entryClass = "";
String entryType = type.substring(4, type.length()-1); String entryType = type.substring(4, type.length()-1);
keyClass = entryType.substring(0, entryType.indexOf(",") ); keyClass = entryType.substring(0, entryType.indexOf(",") );
entryClass = entryType.substring(entryType.indexOf(",") + 1, entryType.length()); entryClass = entryType.substring(entryType.indexOf(",") + 1, entryType.length());
//entryType = dataTypeMapper.getObjectType(entryType, true); //entryType = dataTypeMapper.getObjectType(entryType, true);
entryType = dataTypeMapper.getObjectType(keyClass, true) + "," + dataTypeMapper.getObjectType(entryClass, true); entryType = dataTypeMapper.getObjectType(keyClass, true) + "," + dataTypeMapper.getObjectType(entryClass, true);
String returnType = dataTypeMapper.getMapReturnType(entryType); String returnType = dataTypeMapper.getMapReturnTypeSignature(entryType);
fieldDefinition.setReturnType(returnType); fieldDefinition.setReturnType(returnType);
fieldDefinition.setInitialization("= " + dataTypeMapper.getMapInitialization(entryType)); fieldDefinition.setInitialization("= " + dataTypeMapper.generateMapInitialization(entryType));
if(this.getWrapperName() != null){ if(this.getWrapperName() != null){
fieldDefinition.setName(this.getWrapperName()); fieldDefinition.setName(this.getWrapperName());
}else{ }else{

View File

@ -195,7 +195,7 @@ public class TestCaseExecutor {
if(method.getName().startsWith("get")){ if(method.getName().startsWith("get")){
String methodName = method.getName(); String methodName = method.getName();
String fieldName = methodName.substring(3); String fieldName = methodName.substring(3);
fieldName = config.getNameGenerator().convertToMethodNameFormat(fieldName); fieldName = config.getNameGenerator().applyMethodNamingPolicy(fieldName);
Object value = inputDefinitions.get(fieldName); Object value = inputDefinitions.get(fieldName);
BeanUtils.setProperty(object, fieldName, value); BeanUtils.setProperty(object, fieldName, value);
} }