Swagr code-gen: Refactoring class names of classes used in code generation

This commit is contained in:
Deepak Michael 2011-07-22 11:35:00 +05:30
parent af04c24332
commit 63c7932a67
13 changed files with 192 additions and 268 deletions

View File

@ -17,7 +17,7 @@ public class $className$ extends $extends$ {
$fields:{ field | $fields:{ field |
//$field.description$ //$field.description$
private $field.attributeDefinition.returnType$ $field.attributeDefinition.name$ $field.attributeDefinition.initialization$; private $field.fieldDefinition.returnType$ $field.fieldDefinition.name$ $field.fieldDefinition.initialization$;
}$ }$
$fields:{ field | $fields:{ field |
@ -26,12 +26,12 @@ public class $className$ extends $extends$ {
@Required $endif$ @Required $endif$
$if(field.allowableValues)$ $if(field.allowableValues)$
@AllowableValues(value="$field.allowableValues$")$endif$ @AllowableValues(value="$field.allowableValues$")$endif$
public $field.attributeDefinition.returnType$ get$field.attributeDefinition.NameForMethod$() { public $field.fieldDefinition.returnType$ get$field.fieldDefinition.NameForMethod$() {
return $field.attributeDefinition.name$; return $field.fieldDefinition.name$;
} }
public void set$field.attributeDefinition.NameForMethod$($field.attributeDefinition.returnType$ $field.attributeDefinition.name$) { public void set$field.fieldDefinition.NameForMethod$($field.fieldDefinition.returnType$ $field.fieldDefinition.name$) {
this.$field.attributeDefinition.name$ = $field.attributeDefinition.name$; this.$field.fieldDefinition.name$ = $field.fieldDefinition.name$;
} }
}$ }$
} }

View File

@ -19,7 +19,6 @@ import javax.xml.stream.XMLStreamReader;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* User: ramesh * User: ramesh
@ -59,7 +58,7 @@ public class DriverCodeGenerator {
List<Resource> resources = this.readResourceDocumentation(baseUrl); List<Resource> resources = this.readResourceDocumentation(baseUrl);
StringTemplateGroup aTemplateGroup = new StringTemplateGroup("templates",config.getTemplateLocation()); StringTemplateGroup aTemplateGroup = new StringTemplateGroup("templates",config.getTemplateLocation());
if(resources.size() > 0) { if(resources.size() > 0) {
generateVersionHelper(resources.get(0).getVersion(), aTemplateGroup); generateVersionHelper(resources.get(0).getApiVersion(), aTemplateGroup);
} }
generateModelClasses(resources, aTemplateGroup); generateModelClasses(resources, aTemplateGroup);
generateModelClassesForInput(resources, aTemplateGroup); generateModelClassesForInput(resources, aTemplateGroup);
@ -137,7 +136,7 @@ public class DriverCodeGenerator {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.getDeserializationConfig().set(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.getDeserializationConfig().set(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Resource aResourceDoc = deserializeResource(response, mapper); Resource aResourceDoc = deserializeResource(response, mapper);
aResourceDoc.setVersion(version); aResourceDoc.setApiVersion(version);
resourceDocs.add(aResourceDoc); resourceDocs.add(aResourceDoc);
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
@ -196,24 +195,8 @@ public class DriverCodeGenerator {
* @throws IOException * @throws IOException
*/ */
private Resource deserializeResource(String response, ObjectMapper mapper) throws IOException { private Resource deserializeResource(String response, ObjectMapper mapper) throws IOException {
Resource resource; Resource resource = mapper.readValue(response, Resource.class);
resource.generateModelsFromWrapper(this.config);
ApiResource apiResource = mapper.readValue(response, ApiResource.class);
resource = new Resource();
Model model;
List<Model> models = new ArrayList<Model>();
String modelName;
ApiModelDefn modelDefn;
if (apiResource.getModels() != null) {
for (Map.Entry<String, ApiModelDefn> entry : apiResource.getModels().getModelList().entrySet()) {
modelName = entry.getKey();
modelDefn = entry.getValue();
model = modelDefn.toModel(modelName, this.config);
models.add( model );
}
}
resource.setModels( models );
resource.setEndPoints( apiResource.getEndPoints() );
return resource; return resource;
} }
@ -241,8 +224,8 @@ public class DriverCodeGenerator {
if(!generatedClassNames.contains(model.getName()) && !config.getCodeGenOverridingRules().isModelIgnored(model.getName())){ if(!generatedClassNames.contains(model.getName()) && !config.getCodeGenOverridingRules().isModelIgnored(model.getName())){
List<String> imports = new ArrayList<String>(); List<String> imports = new ArrayList<String>();
imports.addAll(this.config.getDefaultModelImports()); imports.addAll(this.config.getDefaultModelImports());
for(Parameter param : model.getFields()){ for(ModelField param : model.getFields()){
for(String importDef : param.getAttributeDefinition(config.getDataTypeMapper()).getImportDefinitions()){ for(String importDef : param.getFieldDefinition(config.getDataTypeMapper()).getImportDefinitions()){
if(!imports.contains(importDef)){ if(!imports.contains(importDef)){
imports.add(importDef); imports.add(importDef);
} }
@ -275,15 +258,15 @@ public class DriverCodeGenerator {
for(Endpoint endpoint : resource.getEndPoints()){ for(Endpoint endpoint : resource.getEndPoints()){
if(endpoint.getOperations() != null) { if(endpoint.getOperations() != null) {
for(EndpointOperation operation : endpoint.getOperations()){ for(EndpointOperation operation : endpoint.getOperations()){
Method method = operation.generateMethod(endpoint, resource, config); ResourceMethod method = operation.generateMethod(endpoint, resource, config);
if(method.getInputModel() != null) { if(method.getInputModel() != null) {
Model model = method.getInputModel(); Model model = method.getInputModel();
if(model != null){ if(model != null){
if(!generatedClasses.contains(model.getName())) { if(!generatedClasses.contains(model.getName())) {
List<String> imports = new ArrayList<String>(); List<String> imports = new ArrayList<String>();
imports.addAll(this.config.getDefaultModelImports()); imports.addAll(this.config.getDefaultModelImports());
for(Parameter param : model.getFields()){ for(ModelField param : model.getFields()){
for(String importDef : param.getAttributeDefinition(config.getDataTypeMapper()).getImportDefinitions()){ for(String importDef : param.getFieldDefinition(config.getDataTypeMapper()).getImportDefinitions()){
if(!imports.contains(importDef)){ if(!imports.contains(importDef)){
imports.add(importDef); imports.add(importDef);
} }
@ -316,14 +299,14 @@ public class DriverCodeGenerator {
private void generateAPIClasses(List<Resource> resources, StringTemplateGroup templateGroup) { private void generateAPIClasses(List<Resource> resources, StringTemplateGroup templateGroup) {
for(Resource resource : resources) { for(Resource resource : resources) {
List<Method> methods = new ArrayList<Method>(); List<ResourceMethod> methods = new ArrayList<ResourceMethod>();
List<String> imports = new ArrayList<String>(); List<String> imports = new ArrayList<String>();
imports.addAll(this.config.getDefaultServiceImports()); imports.addAll(this.config.getDefaultServiceImports());
methods = resource.generateMethods(resource, config); methods = resource.generateMethods(resource, config);
StringTemplate template = templateGroup.getInstanceOf(API_OBJECT_TEMPLATE); StringTemplate template = templateGroup.getInstanceOf(API_OBJECT_TEMPLATE);
String className = resource.generateClassName(config); String className = resource.generateClassName(config);
List<Method> filteredMethods = new ArrayList<Method>(); List<ResourceMethod> filteredMethods = new ArrayList<ResourceMethod>();
for(Method method:methods){ for(ResourceMethod method:methods){
if(!config.getCodeGenOverridingRules().isMethodIgnored(className, method.getName())){ if(!config.getCodeGenOverridingRules().isMethodIgnored(className, method.getName())){
filteredMethods.add(method); filteredMethods.add(method);
} }
@ -346,26 +329,26 @@ public class DriverCodeGenerator {
Model model = new Model(); Model model = new Model();
model.setName("TestData"); model.setName("TestData");
model.setDescription("Class used to store all the test data. This should not be used for any development"); model.setDescription("Class used to store all the test data. This should not be used for any development");
List<Parameter> parameters = new ArrayList<Parameter>(); List<ModelField> modelFields = new ArrayList<ModelField>();
model.setFields(parameters); model.setFields(modelFields);
for(String className : generatedClassNames){ for(String className : generatedClassNames){
Parameter aParam = new Parameter(); ModelField aParam = new ModelField();
aParam.setName(config.getNameGenerator().convertToMethodNameFormat(className)+"List"); aParam.setName(config.getNameGenerator().convertToMethodNameFormat(className)+"List");
aParam.setParamType(config.getDataTypeMapper().getListReturnType(className)); aParam.setParamType(config.getDataTypeMapper().getListReturnType(className));
parameters.add(aParam); modelFields.add(aParam);
} }
//add missing class from models //add missing class from models
Parameter aParam = new Parameter(); ModelField aParam = new ModelField();
aParam.setName("StringValueList"); aParam.setName("StringValueList");
aParam.setParamType(config.getDataTypeMapper().getListReturnType("StringValue")); aParam.setParamType(config.getDataTypeMapper().getListReturnType("StringValue"));
parameters.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().getListImports());
for(Parameter param : model.getFields()){ for(ModelField param : model.getFields()){
for(String importDef : param.getAttributeDefinition(config.getDataTypeMapper()).getImportDefinitions()){ for(String importDef : param.getFieldDefinition(config.getDataTypeMapper()).getImportDefinitions()){
if(!imports.contains(importDef)){ if(!imports.contains(importDef)){
imports.add(importDef); imports.add(importDef);
} }

View File

@ -3,7 +3,7 @@ package com.wordnik.codegen;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class AttributeDefinition { public class FieldDefinition {
private String returnType; private String returnType;

View File

@ -2,7 +2,7 @@ package com.wordnik.codegen;
import com.wordnik.codegen.config.CodeGenConfig; import com.wordnik.codegen.config.CodeGenConfig;
public class Argument { public class MethodArgument {
public static String ARGUMENT_STRING = "String"; public static String ARGUMENT_STRING = "String";
public static String ARGUMENT_INTEGER = "int"; public static String ARGUMENT_INTEGER = "int";

View File

@ -4,15 +4,15 @@ import com.wordnik.codegen.resource.Model;
import java.util.List; import java.util.List;
public class Method { public class ResourceMethod {
private String description; private String description;
private List<Argument> arguments; private List<MethodArgument> arguments;
private List<Argument> queryParameters; private List<MethodArgument> queryParameters;
private List<Argument> pathParameters; private List<MethodArgument> pathParameters;
private String returnValue; private String returnValue;
@ -45,27 +45,27 @@ public class Method {
this.description = description; this.description = description;
} }
public List<Argument> getArguments() { public List<MethodArgument> getArguments() {
return arguments; return arguments;
} }
public void setArguments(List<Argument> arguments) { public void setArguments(List<MethodArgument> arguments) {
this.arguments = arguments; this.arguments = arguments;
} }
public List<Argument> getQueryParameters() { public List<MethodArgument> getQueryParameters() {
return queryParameters; return queryParameters;
} }
public void setQueryParameters(List<Argument> queryParameters) { public void setQueryParameters(List<MethodArgument> queryParameters) {
this.queryParameters = queryParameters; this.queryParameters = queryParameters;
} }
public List<Argument> getPathParameters() { public List<MethodArgument> getPathParameters() {
return pathParameters; return pathParameters;
} }
public void setPathParameters(List<Argument> pathParameters) { public void setPathParameters(List<MethodArgument> pathParameters) {
this.pathParameters = pathParameters; this.pathParameters = pathParameters;
} }

View File

@ -1,12 +1,10 @@
package com.wordnik.codegen.resource; package com.wordnik.codegen.resource;
import com.wordnik.codegen.config.CodeGenConfig; import com.wordnik.codegen.config.CodeGenConfig;
import com.wordnik.codegen.config.DataTypeMapper;
import org.codehaus.jackson.annotate.JsonAnyGetter; import org.codehaus.jackson.annotate.JsonAnyGetter;
import org.codehaus.jackson.annotate.JsonAnySetter; import org.codehaus.jackson.annotate.JsonAnySetter;
import org.codehaus.jackson.map.annotate.JsonSerialize; import org.codehaus.jackson.map.annotate.JsonSerialize;
import javax.annotation.Generated;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -29,9 +27,9 @@ public class ApiPropertyListWrapper implements Serializable
this.propertyList.put(name, value); this.propertyList.put(name, value);
} }
public List<Parameter> toFieldList(CodeGenConfig config) { public List<ModelField> toFieldList(CodeGenConfig config) {
List<Parameter> fields = new ArrayList<Parameter>(); List<ModelField> fields = new ArrayList<ModelField>();
Parameter field; ModelField field;
String propertyName; String propertyName;
ApiPropertyDefn propertyDefn; ApiPropertyDefn propertyDefn;
@ -39,7 +37,7 @@ public class ApiPropertyListWrapper implements Serializable
propertyName = propertyDefnEntry.getKey(); propertyName = propertyDefnEntry.getKey();
propertyDefn = propertyDefnEntry.getValue(); propertyDefn = propertyDefnEntry.getValue();
field = new Parameter(); field = new ModelField();
field.setName(propertyName); field.setName(propertyName);
//TODO - need to handle this via the nameGenerator which will do this in case the propertyName is a key word in the language //TODO - need to handle this via the nameGenerator which will do this in case the propertyName is a key word in the language
if(propertyName.equals("enum") || propertyName.equals("default")){ if(propertyName.equals("enum") || propertyName.equals("default")){

View File

@ -1,107 +0,0 @@
package com.wordnik.codegen.resource;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import javax.annotation.Generated;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonPropertyOrder({
"apiVersion",
"swagrVersion",
"basePath",
"models",
"id"
})
public class ApiResource implements Serializable
{
@JsonProperty("apiVersion")
private String apiVersion;
@JsonProperty("swagrVersion")
private String swagrVersion;
@JsonProperty("basePath")
private String basePath;
@JsonProperty("models")
private ApiModelListWrapper models;
@JsonProperty("apis")
private List<Endpoint> endPoints;
@JsonProperty("id")
private Object id;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonCreator
public ApiResource(@JsonProperty("models") ApiModelListWrapper models, @JsonProperty("apis") List<Endpoint> endPoints)
{
this.models = models;
this.endPoints = endPoints;
}
@JsonProperty("apiVersion")
public String getApiVersion() {
return apiVersion;
}
@JsonProperty("apiVersion")
public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}
@JsonProperty("swagrVersion")
public String getSwagrVersion() {
return swagrVersion;
}
@JsonProperty("swagrVersion")
public void setSwagrVersion(String swagrVersion) {
this.swagrVersion = swagrVersion;
}
@JsonProperty("basePath")
public String getBasePath() {
return basePath;
}
@JsonProperty("basePath")
public void setBasePath(String basePath) {
this.basePath = basePath;
}
@JsonProperty("models")
public ApiModelListWrapper getModels() {
return models;
}
@JsonProperty("models")
public void setModels(ApiModelListWrapper models) {
this.models = models;
}
@JsonProperty("apis")
public List<Endpoint> getEndPoints() {
return endPoints;
}
@JsonProperty("apis")
public void setEndPoints(List<Endpoint> endPoints) {
this.endPoints = endPoints;
}
@JsonProperty("id")
public Object getId() {
return id;
}
@JsonProperty("id")
public void setId(Object id) {
this.id = id;
}
}

View File

@ -1,6 +1,6 @@
package com.wordnik.codegen.resource; package com.wordnik.codegen.resource;
import com.wordnik.codegen.Method; import com.wordnik.codegen.ResourceMethod;
import com.wordnik.codegen.config.CodeGenConfig; import com.wordnik.codegen.config.CodeGenConfig;
import java.util.ArrayList; import java.util.ArrayList;
@ -23,7 +23,7 @@ public class Endpoint {
private List<EndpointOperation> operations; private List<EndpointOperation> operations;
private List<Method> methods; private List<ResourceMethod> methods;
private List<ErrorResponse> errorResponses; private List<ErrorResponse> errorResponses;
@ -81,9 +81,9 @@ public class Endpoint {
} }
} }
public List<Method> generateMethods(Resource resource, CodeGenConfig config) { public List<ResourceMethod> generateMethods(Resource resource, CodeGenConfig config) {
if(methods == null){ if(methods == null){
methods = new ArrayList<Method>(); methods = new ArrayList<ResourceMethod>();
if(getOperations() != null) { if(getOperations() != null) {
for(EndpointOperation operation: getOperations()) { for(EndpointOperation operation: getOperations()) {
if(!operation.isDeprecated() && areModelsAvailable(operation.getParameters(), resource, config)) { if(!operation.isDeprecated() && areModelsAvailable(operation.getParameters(), resource, config)) {
@ -95,18 +95,18 @@ public class Endpoint {
return methods; return methods;
} }
private boolean areModelsAvailable(List<Parameter> parameters, Resource resource, CodeGenConfig config) { private boolean areModelsAvailable(List<ModelField> modelFields, Resource resource, CodeGenConfig config) {
Boolean isParamSetAvailable = true; Boolean isParamSetAvailable = true;
if(parameters == null) return true; if(modelFields == null) return true;
for(Parameter parameter: parameters){ for(ModelField modelField : modelFields){
if (parameter.getParamType().equalsIgnoreCase(EndpointOperation.PARAM_TYPE_BODY) ){ if (modelField.getParamType().equalsIgnoreCase(EndpointOperation.PARAM_TYPE_BODY) ){
isParamSetAvailable = false; isParamSetAvailable = false;
for(Model model : resource.getModels()){ for(Model model : resource.getModels()){
if(config.getDataTypeMapper().isPrimitiveType(parameter.getDataType())){ if(config.getDataTypeMapper().isPrimitiveType(modelField.getDataType())){
isParamSetAvailable = true; isParamSetAvailable = true;
break; break;
} }
if(model.getName().equalsIgnoreCase(parameter.getDataType())){ if(model.getName().equalsIgnoreCase(modelField.getDataType())){
isParamSetAvailable = true; isParamSetAvailable = true;
break; break;
} }

View File

@ -1,12 +1,11 @@
package com.wordnik.codegen.resource; package com.wordnik.codegen.resource;
import com.wordnik.codegen.Argument; import com.wordnik.codegen.MethodArgument;
import com.wordnik.codegen.Method; import com.wordnik.codegen.ResourceMethod;
import com.wordnik.codegen.config.CodeGenConfig; import com.wordnik.codegen.config.CodeGenConfig;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.StringTokenizer;
/** /**
* User: ramesh * User: ramesh
@ -38,11 +37,11 @@ public class EndpointOperation {
private String responseClass; private String responseClass;
private List<Parameter> parameters; private List<ModelField> parameters;
private boolean deprecated; private boolean deprecated;
private Method method; private ResourceMethod method;
private List<String> tags; private List<String> tags;
@ -103,11 +102,11 @@ public class EndpointOperation {
this.getResponse().add(response); this.getResponse().add(response);
} }
public List<Parameter> getParameters() { public List<ModelField> getParameters() {
return parameters; return parameters;
} }
public void setParameters(List<Parameter> parameters) { public void setParameters(List<ModelField> parameters) {
this.parameters = parameters; this.parameters = parameters;
} }
@ -145,9 +144,9 @@ public class EndpointOperation {
} }
public Method generateMethod(Endpoint endPoint, Resource resource, CodeGenConfig config) { public ResourceMethod generateMethod(Endpoint endPoint, Resource resource, CodeGenConfig config) {
if(method == null){ if(method == null){
method = new Method(); method = new ResourceMethod();
//add method description //add method description
method.setDescription(this.getSummary() + "\n " + getNotes()); method.setDescription(this.getSummary() + "\n " + getNotes());
@ -199,49 +198,49 @@ public class EndpointOperation {
*/ */
List<String> argNames = new ArrayList<String>(); List<String> argNames = new ArrayList<String>();
if(this.getParameters() != null) { if(this.getParameters() != null) {
List<Argument> arguments = new ArrayList<Argument>(); List<MethodArgument> arguments = new ArrayList<MethodArgument>();
List<Argument> queryParams= new ArrayList<Argument>(); List<MethodArgument> queryParams= new ArrayList<MethodArgument>();
List<Argument> pathParams= new ArrayList<Argument>(); List<MethodArgument> pathParams= new ArrayList<MethodArgument>();
method.setArguments(arguments); method.setArguments(arguments);
method.setQueryParameters(queryParams); method.setQueryParameters(queryParams);
method.setPathParameters(pathParams); method.setPathParameters(pathParams);
for(Parameter parameter: this.getParameters()){ for(ModelField modelField : this.getParameters()){
if(!argNames.contains(parameter.getName())) { if(!argNames.contains(modelField.getName())) {
argNames.add(parameter.getName()); argNames.add(modelField.getName());
Argument anArgument = new Argument(); MethodArgument anArgument = new MethodArgument();
anArgument.setAllowedValues(parameter.getAllowedValuesString()); anArgument.setAllowedValues(modelField.getAllowedValuesString());
//check if arguments has auth token //check if arguments has auth token
if(parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) && if(modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) &&
parameter.getName().equals(AUTH_TOKEN_PARAM_NAME)){ modelField.getName().equals(AUTH_TOKEN_PARAM_NAME)){
method.setAuthToken(true); method.setAuthToken(true);
anArgument.setName(AUTH_TOKEN_ARGUMENT_NAME); anArgument.setName(AUTH_TOKEN_ARGUMENT_NAME);
anArgument.setDataType(Argument.ARGUMENT_STRING); anArgument.setDataType(MethodArgument.ARGUMENT_STRING);
anArgument.setDescription(parameter.getDescription()); anArgument.setDescription(modelField.getDescription());
arguments.add(anArgument); arguments.add(anArgument);
}else if(parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) && }else if(modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) &&
parameter.getName().equals(API_KEY_PARAM_NAME)){ modelField.getName().equals(API_KEY_PARAM_NAME)){
//do nothing for API key parameter as all calls will automatically add API KEY to the http headers //do nothing for API key parameter as all calls will automatically add API KEY to the http headers
}else if (parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_PATH) && }else if (modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_PATH) &&
!parameter.getName().equalsIgnoreCase(FORMAT_PARAM_NAME)) { !modelField.getName().equalsIgnoreCase(FORMAT_PARAM_NAME)) {
anArgument.setName(parameter.getName()); anArgument.setName(modelField.getName());
anArgument.setDataType(Argument.ARGUMENT_STRING); anArgument.setDataType(MethodArgument.ARGUMENT_STRING);
anArgument.setDescription(parameter.getDescription()); anArgument.setDescription(modelField.getDescription());
arguments.add(anArgument); arguments.add(anArgument);
pathParams.add(anArgument); pathParams.add(anArgument);
}else if (parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_QUERY)) { }else if (modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_QUERY)) {
anArgument.setName(parameter.getName()); anArgument.setName(modelField.getName());
anArgument.setDataType(Argument.ARGUMENT_STRING); anArgument.setDataType(MethodArgument.ARGUMENT_STRING);
anArgument.setDescription(parameter.getDescription()); anArgument.setDescription(modelField.getDescription());
queryParams.add(anArgument); queryParams.add(anArgument);
arguments.add(anArgument); arguments.add(anArgument);
}else if (parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_BODY)) { }else if (modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_BODY)) {
if(parameter.getName() == null) { if(modelField.getName() == null) {
parameter.setName("postObject"); modelField.setName("postObject");
} }
anArgument.setName(parameter.getName()); anArgument.setName(modelField.getName());
anArgument.setDataType(config.getDataTypeMapper().getReturnValueType(parameter.getDataType())); anArgument.setDataType(config.getDataTypeMapper().getReturnValueType(modelField.getDataType()));
anArgument.setDescription(parameter.getDescription()); anArgument.setDescription(modelField.getDescription());
arguments.add(anArgument); arguments.add(anArgument);
method.setPostObject(true); method.setPostObject(true);
} }
@ -252,25 +251,25 @@ public class EndpointOperation {
//check for number of arguments, if we have more than 4 then send the arguments as input object //check for number of arguments, if we have more than 4 then send the arguments as input object
if(method.getArguments() != null && method.getArguments().size() > 4){ if(method.getArguments() != null && method.getArguments().size() > 4){
List<Argument> arguments = new ArrayList<Argument>(); List<MethodArgument> arguments = new ArrayList<MethodArgument>();
Model modelforMethodInput = new Model(); Model modelforMethodInput = new Model();
modelforMethodInput.setName(inputobjectName); modelforMethodInput.setName(inputobjectName);
List<Parameter> fields = new ArrayList<Parameter>(); List<ModelField> fields = new ArrayList<ModelField>();
for(Argument argument: method.getArguments()){ for(MethodArgument argument: method.getArguments()){
if(!argument.getName().equals("postObject") && !argument.getName().equals("authToken")){ if(!argument.getName().equals("postObject") && !argument.getName().equals("authToken")){
Parameter aParameter = new Parameter(); ModelField aModelField = new ModelField();
aParameter.setAllowedValues(argument.getAllowedValues()); aModelField.setAllowedValues(argument.getAllowedValues());
aParameter.setDescription(argument.getDescription()); aModelField.setDescription(argument.getDescription());
aParameter.setName(argument.getName()); aModelField.setName(argument.getName());
aParameter.setParamType(argument.getDataType()); aModelField.setParamType(argument.getDataType());
fields.add(aParameter); fields.add(aModelField);
}else{ }else{
arguments.add(argument); arguments.add(argument);
} }
} }
modelforMethodInput.setFields(fields); modelforMethodInput.setFields(fields);
Argument anArgument = new Argument(); MethodArgument anArgument = new MethodArgument();
anArgument.setDataType(inputobjectName); anArgument.setDataType(inputobjectName);
anArgument.setName(config.getNameGenerator().convertToMethodNameFormat(inputobjectName)); anArgument.setName(config.getNameGenerator().convertToMethodNameFormat(inputobjectName));
arguments.add(anArgument); arguments.add(anArgument);
@ -281,7 +280,7 @@ public class EndpointOperation {
List<String> argumentDefinitions = new ArrayList<String>(); List<String> argumentDefinitions = new ArrayList<String>();
List<String> argumentNames = new ArrayList<String>(); List<String> argumentNames = new ArrayList<String>();
if (method.getArguments() != null && method.getArguments().size() > 0) { if (method.getArguments() != null && method.getArguments().size() > 0) {
for(Argument arg: method.getArguments()) { for(MethodArgument arg: method.getArguments()) {
if(!arg.getName().equalsIgnoreCase(FORMAT_PARAM_NAME)){ if(!arg.getName().equalsIgnoreCase(FORMAT_PARAM_NAME)){
argumentDefinitions.add(arg.getDataType() + " " + arg.getName()); argumentDefinitions.add(arg.getDataType() + " " + arg.getName());
argumentNames.add(arg.getName()); argumentNames.add(arg.getName());

View File

@ -17,7 +17,7 @@ public class Model {
private String description; private String description;
private List<Parameter> fields; private List<ModelField> fields;
public String getName() { public String getName() {
return name; return name;
@ -27,11 +27,11 @@ public class Model {
this.name = name; this.name = name;
} }
public List<Parameter> getFields() { public List<ModelField> getFields() {
return fields; return fields;
} }
public void setFields(List<Parameter> fields) { public void setFields(List<ModelField> fields) {
this.fields = fields; this.fields = fields;
} }

View File

@ -1,6 +1,6 @@
package com.wordnik.codegen.resource; package com.wordnik.codegen.resource;
import com.wordnik.codegen.AttributeDefinition; import com.wordnik.codegen.FieldDefinition;
import com.wordnik.codegen.config.DataTypeMapper; import com.wordnik.codegen.config.DataTypeMapper;
import java.util.ArrayList; import java.util.ArrayList;
@ -12,7 +12,7 @@ import java.util.StringTokenizer;
* Date: 3/31/11 * Date: 3/31/11
* Time: 7:57 AM * Time: 7:57 AM
*/ */
public class Parameter { public class ModelField {
private String name; private String name;
@ -34,7 +34,7 @@ public class Parameter {
private String paramAccess; private String paramAccess;
private AttributeDefinition attributeDefinition; private FieldDefinition fieldDefinition;
public String getName() { public String getName() {
return name; return name;
@ -136,32 +136,32 @@ public class Parameter {
this.dataType = dataType; this.dataType = dataType;
} }
public AttributeDefinition getAttributeDefinition(){ public FieldDefinition getFieldDefinition(){
return attributeDefinition; return fieldDefinition;
} }
public AttributeDefinition getAttributeDefinition(DataTypeMapper dataTypeMapper) { public FieldDefinition getFieldDefinition(DataTypeMapper dataTypeMapper) {
if(attributeDefinition == null) { if(fieldDefinition == null) {
attributeDefinition = new AttributeDefinition(); fieldDefinition = new FieldDefinition();
String type = paramType.trim(); String type = paramType.trim();
if(type.contains("date")||type.contains("Date") ){ if(type.contains("date")||type.contains("Date") ){
attributeDefinition.getImportDefinitions().add("java.util.Date"); fieldDefinition.getImportDefinitions().add("java.util.Date");
} }
if(type.startsWith("List[")){ if(type.startsWith("List[")){
attributeDefinition.getImportDefinitions().addAll(dataTypeMapper.getListImports()); fieldDefinition.getImportDefinitions().addAll(dataTypeMapper.getListImports());
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.getListReturnType(entryType);
attributeDefinition.setReturnType(returnType); fieldDefinition.setReturnType(returnType);
attributeDefinition.setInitialization(" = " + dataTypeMapper.getListInitialization(entryType)); fieldDefinition.setInitialization(" = " + dataTypeMapper.getListInitialization(entryType));
if(this.getWrapperName() != null){ if(this.getWrapperName() != null){
attributeDefinition.setName(this.getWrapperName()); fieldDefinition.setName(this.getWrapperName());
}else{ }else{
attributeDefinition.setName(this.getName()); fieldDefinition.setName(this.getName());
} }
}else if (type.startsWith("Map[")) { }else if (type.startsWith("Map[")) {
attributeDefinition.getImportDefinitions().addAll(dataTypeMapper.getMapImports()); fieldDefinition.getImportDefinitions().addAll(dataTypeMapper.getMapImports());
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(",") );
@ -169,20 +169,20 @@ public class Parameter {
//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.getMapReturnType(entryType);
attributeDefinition.setReturnType(returnType); fieldDefinition.setReturnType(returnType);
attributeDefinition.setInitialization("= " + dataTypeMapper.getMapInitialization(entryType)); fieldDefinition.setInitialization("= " + dataTypeMapper.getMapInitialization(entryType));
if(this.getWrapperName() != null){ if(this.getWrapperName() != null){
attributeDefinition.setName(this.getWrapperName()); fieldDefinition.setName(this.getWrapperName());
}else{ }else{
attributeDefinition.setName(this.getName()); fieldDefinition.setName(this.getName());
} }
}else{ }else{
attributeDefinition.setReturnType(dataTypeMapper.getObjectType(type, false)); fieldDefinition.setReturnType(dataTypeMapper.getObjectType(type, false));
attributeDefinition.setName(this.getName()); fieldDefinition.setName(this.getName());
} }
} }
return attributeDefinition; return fieldDefinition;
} }
} }

View File

@ -1,10 +1,13 @@
package com.wordnik.codegen.resource; package com.wordnik.codegen.resource;
import com.wordnik.codegen.Method; import com.wordnik.codegen.ResourceMethod;
import com.wordnik.codegen.config.CodeGenConfig; import com.wordnik.codegen.config.CodeGenConfig;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* User: ramesh * User: ramesh
@ -13,39 +16,74 @@ import java.util.List;
*/ */
public class Resource { public class Resource {
private String version; private String apiVersion;
@JsonProperty("swagrVersion")
private String swagrVersion;
@JsonProperty("apis")
private List<Endpoint> endPoints = new ArrayList<Endpoint>(); private List<Endpoint> endPoints = new ArrayList<Endpoint>();
@JsonProperty("models")
private ApiModelListWrapper modelListWrapper;
private List<Model> models = new ArrayList<Model>(); private List<Model> models = new ArrayList<Model>();
private String generatedClassName; private String generatedClassName;
private List<Method> methods; private List<ResourceMethod> methods;
@JsonCreator
public Resource() {//@JsonProperty("models") ApiModelListWrapper modelListWrapper, @JsonProperty("apis") List<Endpoint> endPoints)
}
public String getVersion() { public String getApiVersion() {
return version; return apiVersion;
} }
public void setVersion(String version) { public void setApiVersion(String apiVersion) {
this.version = version; this.apiVersion = apiVersion;
} }
@JsonProperty("swagrVersion")
public String getSwagrVersion() {
return swagrVersion;
}
@JsonProperty("swagrVersion")
public void setSwagrVersion(String swagrVersion) {
this.swagrVersion = swagrVersion;
}
@JsonProperty("apis")
public List<Endpoint> getEndPoints() { public List<Endpoint> getEndPoints() {
return endPoints; return endPoints;
} }
@JsonProperty("apis")
public void setEndPoints(List<Endpoint> endPoints) { public void setEndPoints(List<Endpoint> endPoints) {
this.endPoints = endPoints; this.endPoints = endPoints;
} }
@JsonProperty("models")
public ApiModelListWrapper getModelListWrapper() {
return modelListWrapper;
}
@JsonProperty("models")
public void setModelListWrapper(ApiModelListWrapper modelListWrapper) {
this.modelListWrapper = modelListWrapper;
}
public List<Model> getModels() { public List<Model> getModels() {
return models; return models;
} }
public void setModels(List<Model> models) { /*public void setModels(List<Model> models) {
this.models = models; this.models = models;
} }*/
public String generateClassName(CodeGenConfig config) { public String generateClassName(CodeGenConfig config) {
if (generatedClassName == null) { if (generatedClassName == null) {
@ -55,9 +93,9 @@ public class Resource {
return generatedClassName; return generatedClassName;
} }
public List<Method> generateMethods(Resource resource, CodeGenConfig config) { public List<ResourceMethod> generateMethods(Resource resource, CodeGenConfig config) {
if(methods == null){ if(methods == null){
methods = new ArrayList<Method>(); methods = new ArrayList<ResourceMethod>();
if(getEndPoints() != null) { if(getEndPoints() != null) {
for(Endpoint endpoint: getEndPoints()){ for(Endpoint endpoint: getEndPoints()){
methods.addAll(endpoint.generateMethods(resource, config)); methods.addAll(endpoint.generateMethods(resource, config));
@ -66,5 +104,18 @@ public class Resource {
} }
return methods; return methods;
} }
public void generateModelsFromWrapper(CodeGenConfig config) {
String modelName;
ApiModelDefn modelDefn;
if (modelListWrapper != null) {
for (Map.Entry<String, ApiModelDefn> entry : modelListWrapper.getModelList().entrySet()) {
modelName = entry.getKey();
modelDefn = entry.getValue();
models.add (modelDefn.toModel(modelName, config) );
}
}
}
} }

View File

@ -17,7 +17,7 @@ public class $className$ extends $extends$ {
$fields:{ field | $fields:{ field |
//$field.description$ //$field.description$
private $field.attributeDefinition.returnType$ $field.attributeDefinition.name$ $field.attributeDefinition.initialization$; private $field.fieldDefinition.returnType$ $field.fieldDefinition.name$ $field.fieldDefinition.initialization$;
}$ }$
$fields:{ field | $fields:{ field |
@ -26,12 +26,12 @@ public class $className$ extends $extends$ {
@Required $endif$ @Required $endif$
$if(field.allowableValues)$ $if(field.allowableValues)$
@AllowableValues(value="$field.allowableValues$")$endif$ @AllowableValues(value="$field.allowableValues$")$endif$
public $field.attributeDefinition.returnType$ get$field.attributeDefinition.NameForMethod$() { public $field.fieldDefinition.returnType$ get$field.fieldDefinition.NameForMethod$() {
return $field.attributeDefinition.name$; return $field.fieldDefinition.name$;
} }
public void set$field.attributeDefinition.NameForMethod$($field.attributeDefinition.returnType$ $field.attributeDefinition.name$) { public void set$field.fieldDefinition.NameForMethod$($field.fieldDefinition.returnType$ $field.fieldDefinition.name$) {
this.$field.attributeDefinition.name$ = $field.attributeDefinition.name$; this.$field.fieldDefinition.name$ = $field.fieldDefinition.name$;
} }
}$ }$
} }