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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,12 +1,10 @@
package com.wordnik.codegen.resource;
import com.wordnik.codegen.config.CodeGenConfig;
import com.wordnik.codegen.config.DataTypeMapper;
import org.codehaus.jackson.annotate.JsonAnyGetter;
import org.codehaus.jackson.annotate.JsonAnySetter;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import javax.annotation.Generated;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
@ -29,9 +27,9 @@ public class ApiPropertyListWrapper implements Serializable
this.propertyList.put(name, value);
}
public List<Parameter> toFieldList(CodeGenConfig config) {
List<Parameter> fields = new ArrayList<Parameter>();
Parameter field;
public List<ModelField> toFieldList(CodeGenConfig config) {
List<ModelField> fields = new ArrayList<ModelField>();
ModelField field;
String propertyName;
ApiPropertyDefn propertyDefn;
@ -39,7 +37,7 @@ public class ApiPropertyListWrapper implements Serializable
propertyName = propertyDefnEntry.getKey();
propertyDefn = propertyDefnEntry.getValue();
field = new Parameter();
field = new ModelField();
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
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;
import com.wordnik.codegen.Method;
import com.wordnik.codegen.ResourceMethod;
import com.wordnik.codegen.config.CodeGenConfig;
import java.util.ArrayList;
@ -23,7 +23,7 @@ public class Endpoint {
private List<EndpointOperation> operations;
private List<Method> methods;
private List<ResourceMethod> methods;
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){
methods = new ArrayList<Method>();
methods = new ArrayList<ResourceMethod>();
if(getOperations() != null) {
for(EndpointOperation operation: getOperations()) {
if(!operation.isDeprecated() && areModelsAvailable(operation.getParameters(), resource, config)) {
@ -95,18 +95,18 @@ public class Endpoint {
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;
if(parameters == null) return true;
for(Parameter parameter: parameters){
if (parameter.getParamType().equalsIgnoreCase(EndpointOperation.PARAM_TYPE_BODY) ){
if(modelFields == null) return true;
for(ModelField modelField : modelFields){
if (modelField.getParamType().equalsIgnoreCase(EndpointOperation.PARAM_TYPE_BODY) ){
isParamSetAvailable = false;
for(Model model : resource.getModels()){
if(config.getDataTypeMapper().isPrimitiveType(parameter.getDataType())){
if(config.getDataTypeMapper().isPrimitiveType(modelField.getDataType())){
isParamSetAvailable = true;
break;
}
if(model.getName().equalsIgnoreCase(parameter.getDataType())){
if(model.getName().equalsIgnoreCase(modelField.getDataType())){
isParamSetAvailable = true;
break;
}

View File

@ -1,12 +1,11 @@
package com.wordnik.codegen.resource;
import com.wordnik.codegen.Argument;
import com.wordnik.codegen.Method;
import com.wordnik.codegen.MethodArgument;
import com.wordnik.codegen.ResourceMethod;
import com.wordnik.codegen.config.CodeGenConfig;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* User: ramesh
@ -38,11 +37,11 @@ public class EndpointOperation {
private String responseClass;
private List<Parameter> parameters;
private List<ModelField> parameters;
private boolean deprecated;
private Method method;
private ResourceMethod method;
private List<String> tags;
@ -103,11 +102,11 @@ public class EndpointOperation {
this.getResponse().add(response);
}
public List<Parameter> getParameters() {
public List<ModelField> getParameters() {
return parameters;
}
public void setParameters(List<Parameter> parameters) {
public void setParameters(List<ModelField> 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){
method = new Method();
method = new ResourceMethod();
//add method description
method.setDescription(this.getSummary() + "\n " + getNotes());
@ -199,49 +198,49 @@ public class EndpointOperation {
*/
List<String> argNames = new ArrayList<String>();
if(this.getParameters() != null) {
List<Argument> arguments = new ArrayList<Argument>();
List<Argument> queryParams= new ArrayList<Argument>();
List<Argument> pathParams= new ArrayList<Argument>();
List<MethodArgument> arguments = new ArrayList<MethodArgument>();
List<MethodArgument> queryParams= new ArrayList<MethodArgument>();
List<MethodArgument> pathParams= new ArrayList<MethodArgument>();
method.setArguments(arguments);
method.setQueryParameters(queryParams);
method.setPathParameters(pathParams);
for(Parameter parameter: this.getParameters()){
if(!argNames.contains(parameter.getName())) {
argNames.add(parameter.getName());
Argument anArgument = new Argument();
anArgument.setAllowedValues(parameter.getAllowedValuesString());
for(ModelField modelField : this.getParameters()){
if(!argNames.contains(modelField.getName())) {
argNames.add(modelField.getName());
MethodArgument anArgument = new MethodArgument();
anArgument.setAllowedValues(modelField.getAllowedValuesString());
//check if arguments has auth token
if(parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) &&
parameter.getName().equals(AUTH_TOKEN_PARAM_NAME)){
if(modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) &&
modelField.getName().equals(AUTH_TOKEN_PARAM_NAME)){
method.setAuthToken(true);
anArgument.setName(AUTH_TOKEN_ARGUMENT_NAME);
anArgument.setDataType(Argument.ARGUMENT_STRING);
anArgument.setDescription(parameter.getDescription());
anArgument.setDataType(MethodArgument.ARGUMENT_STRING);
anArgument.setDescription(modelField.getDescription());
arguments.add(anArgument);
}else if(parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) &&
parameter.getName().equals(API_KEY_PARAM_NAME)){
}else if(modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) &&
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
}else if (parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_PATH) &&
!parameter.getName().equalsIgnoreCase(FORMAT_PARAM_NAME)) {
anArgument.setName(parameter.getName());
anArgument.setDataType(Argument.ARGUMENT_STRING);
anArgument.setDescription(parameter.getDescription());
}else if (modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_PATH) &&
!modelField.getName().equalsIgnoreCase(FORMAT_PARAM_NAME)) {
anArgument.setName(modelField.getName());
anArgument.setDataType(MethodArgument.ARGUMENT_STRING);
anArgument.setDescription(modelField.getDescription());
arguments.add(anArgument);
pathParams.add(anArgument);
}else if (parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_QUERY)) {
anArgument.setName(parameter.getName());
anArgument.setDataType(Argument.ARGUMENT_STRING);
anArgument.setDescription(parameter.getDescription());
}else if (modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_QUERY)) {
anArgument.setName(modelField.getName());
anArgument.setDataType(MethodArgument.ARGUMENT_STRING);
anArgument.setDescription(modelField.getDescription());
queryParams.add(anArgument);
arguments.add(anArgument);
}else if (parameter.getParamType().equalsIgnoreCase(PARAM_TYPE_BODY)) {
if(parameter.getName() == null) {
parameter.setName("postObject");
}else if (modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_BODY)) {
if(modelField.getName() == null) {
modelField.setName("postObject");
}
anArgument.setName(parameter.getName());
anArgument.setDataType(config.getDataTypeMapper().getReturnValueType(parameter.getDataType()));
anArgument.setDescription(parameter.getDescription());
anArgument.setName(modelField.getName());
anArgument.setDataType(config.getDataTypeMapper().getReturnValueType(modelField.getDataType()));
anArgument.setDescription(modelField.getDescription());
arguments.add(anArgument);
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
if(method.getArguments() != null && method.getArguments().size() > 4){
List<Argument> arguments = new ArrayList<Argument>();
List<MethodArgument> arguments = new ArrayList<MethodArgument>();
Model modelforMethodInput = new Model();
modelforMethodInput.setName(inputobjectName);
List<Parameter> fields = new ArrayList<Parameter>();
for(Argument argument: method.getArguments()){
List<ModelField> fields = new ArrayList<ModelField>();
for(MethodArgument argument: method.getArguments()){
if(!argument.getName().equals("postObject") && !argument.getName().equals("authToken")){
Parameter aParameter = new Parameter();
aParameter.setAllowedValues(argument.getAllowedValues());
aParameter.setDescription(argument.getDescription());
aParameter.setName(argument.getName());
aParameter.setParamType(argument.getDataType());
fields.add(aParameter);
ModelField aModelField = new ModelField();
aModelField.setAllowedValues(argument.getAllowedValues());
aModelField.setDescription(argument.getDescription());
aModelField.setName(argument.getName());
aModelField.setParamType(argument.getDataType());
fields.add(aModelField);
}else{
arguments.add(argument);
}
}
modelforMethodInput.setFields(fields);
Argument anArgument = new Argument();
MethodArgument anArgument = new MethodArgument();
anArgument.setDataType(inputobjectName);
anArgument.setName(config.getNameGenerator().convertToMethodNameFormat(inputobjectName));
arguments.add(anArgument);
@ -281,7 +280,7 @@ public class EndpointOperation {
List<String> argumentDefinitions = new ArrayList<String>();
List<String> argumentNames = new ArrayList<String>();
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)){
argumentDefinitions.add(arg.getDataType() + " " + arg.getName());
argumentNames.add(arg.getName());

View File

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

View File

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

View File

@ -1,10 +1,13 @@
package com.wordnik.codegen.resource;
import com.wordnik.codegen.Method;
import com.wordnik.codegen.ResourceMethod;
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.List;
import java.util.Map;
/**
* User: ramesh
@ -13,39 +16,74 @@ import java.util.List;
*/
public class Resource {
private String version;
private String apiVersion;
@JsonProperty("swagrVersion")
private String swagrVersion;
@JsonProperty("apis")
private List<Endpoint> endPoints = new ArrayList<Endpoint>();
@JsonProperty("models")
private ApiModelListWrapper modelListWrapper;
private List<Model> models = new ArrayList<Model>();
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() {
return version;
public String getApiVersion() {
return apiVersion;
}
public void setVersion(String version) {
this.version = version;
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("apis")
public List<Endpoint> getEndPoints() {
return endPoints;
}
@JsonProperty("apis")
public void setEndPoints(List<Endpoint> 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() {
return models;
}
public void setModels(List<Model> models) {
/*public void setModels(List<Model> models) {
this.models = models;
}
}*/
public String generateClassName(CodeGenConfig config) {
if (generatedClassName == null) {
@ -55,9 +93,9 @@ public class Resource {
return generatedClassName;
}
public List<Method> generateMethods(Resource resource, CodeGenConfig config) {
public List<ResourceMethod> generateMethods(Resource resource, CodeGenConfig config) {
if(methods == null){
methods = new ArrayList<Method>();
methods = new ArrayList<ResourceMethod>();
if(getEndPoints() != null) {
for(Endpoint endpoint: getEndPoints()){
methods.addAll(endpoint.generateMethods(resource, config));
@ -66,5 +104,18 @@ public class Resource {
}
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 |
//$field.description$
private $field.attributeDefinition.returnType$ $field.attributeDefinition.name$ $field.attributeDefinition.initialization$;
private $field.fieldDefinition.returnType$ $field.fieldDefinition.name$ $field.fieldDefinition.initialization$;
}$
$fields:{ field |
@ -26,12 +26,12 @@ public class $className$ extends $extends$ {
@Required $endif$
$if(field.allowableValues)$
@AllowableValues(value="$field.allowableValues$")$endif$
public $field.attributeDefinition.returnType$ get$field.attributeDefinition.NameForMethod$() {
return $field.attributeDefinition.name$;
public $field.fieldDefinition.returnType$ get$field.fieldDefinition.NameForMethod$() {
return $field.fieldDefinition.name$;
}
public void set$field.attributeDefinition.NameForMethod$($field.attributeDefinition.returnType$ $field.attributeDefinition.name$) {
this.$field.attributeDefinition.name$ = $field.attributeDefinition.name$;
public void set$field.fieldDefinition.NameForMethod$($field.fieldDefinition.returnType$ $field.fieldDefinition.name$) {
this.$field.fieldDefinition.name$ = $field.fieldDefinition.name$;
}
}$
}