1. Added logic to make sure header parameters are set in the headers in java and scala code gen. Header parameter names are also available in templates for other languages to use

2. Fixed issues with enum name generation in java code gen library.
This commit is contained in:
rpidikiti 2011-10-17 07:09:15 -07:00
parent 907c32af95
commit 60c577a283
11 changed files with 118 additions and 38 deletions

View File

@ -229,7 +229,7 @@
"id" : 1, "id" : 1,
"resourceId" : 12, "resourceId" : 12,
"input" : { "input" : {
"orderId":"1" "orderId":"3"
}, },
"assertions" : [ "assertions" : [
{ {

View File

@ -131,16 +131,16 @@ public class APIInvoker {
* *
* @param resourceURL - URL for the rest resource * @param resourceURL - URL for the rest resource
* @param method - Method we should use for communicating to the back end. * @param method - Method we should use for communicating to the back end.
* @param postObject - if the method is POST, provide the object that should be sent as part of post request. * @param postData - if the method is POST, provide the object that should be sent as part of post request.
* @return JSON response of the API call. * @return JSON response of the API call.
* @throws com.wordnik.swagger.exception.APIException if the call to API server fails. * @throws com.wordnik.swagger.runtime.exception.APIException if the call to API server fails.
*/ */
public static String invokeAPI(String resourceURL, String method, Map<String, public String invokeAPI(String resourceURL, String method, Map<String,
String> queryParams, Object postData) throws APIException { String> queryParams, Object postData, Map<String, String> headerParams) throws APIException {
Client apiClient = Client.create(); Client apiClient = Client.create();
//check for app server values //check for app server values
if(getApiServer() == null || getApiServer().length() == 0) { if(getApiServer() == null || getApiServer().length() == 0) {
String[] args = {getApiServer()}; String[] args = {getApiServer()};
@ -155,7 +155,7 @@ public class APIInvoker {
apiClient.addFilter(new LoggingFilter(logger)); apiClient.addFilter(new LoggingFilter(logger));
} }
} }
//make the communication //make the communication
resourceURL = getApiServer() + resourceURL; resourceURL = getApiServer() + resourceURL;
if(queryParams.keySet().size() > 0){ if(queryParams.keySet().size() > 0){
@ -181,7 +181,12 @@ public class APIInvoker {
for(String key : headerMap.keySet()){ for(String key : headerMap.keySet()){
builder.header(key, headerMap.get(key)); builder.header(key, headerMap.get(key));
} }
if(headerParams != null){
for(String key : headerParams.keySet()){
builder.header(key, headerParams.get(key));
}
}
ClientResponse clientResponse = null; ClientResponse clientResponse = null;
if(method.equals(GET)) { if(method.equals(GET)) {
clientResponse = builder.get(ClientResponse.class); clientResponse = builder.get(ClientResponse.class);

View File

@ -30,7 +30,7 @@ public enum $className$ {
$values: { value | $value.name$($value.value$)};separator=", "$; $values: { value | $value.name$($value.value$)};separator=", "$;
final $enumValueType$ value; private $enumValueType$ value;
$className$($enumValueType$ value) { $className$($enumValueType$ value) {
this.value = value; this.value = value;
@ -40,7 +40,7 @@ public enum $className$ {
return value; return value;
} }
@Override public String toString() { @Override public String toString() {
return String.valueOf(this.getValue()); return String.valueOf(this.getValue());
} }
}; }

View File

@ -17,11 +17,6 @@
package $packageName$; package $packageName$;
//import $annotationPackageName$.MethodArgumentNames;
//import $exceptionPackageName$.APIExceptionCodes;
//import $exceptionPackageName$.APIException;
import $modelPackageName$.*; import $modelPackageName$.*;
import org.codehaus.jackson.map.DeserializationConfig.Feature; import org.codehaus.jackson.map.DeserializationConfig.Feature;
@ -32,6 +27,7 @@ import com.wordnik.swagger.runtime.common.*;
import com.wordnik.swagger.runtime.exception.*; import com.wordnik.swagger.runtime.exception.*;
import java.util.*; import java.util.*;
import java.lang.Long;
import java.io.IOException; import java.io.IOException;
$imports:{ import | $imports:{ import |
@ -73,7 +69,8 @@ $endif$
resourcePath = resourcePath.replace("{format}","json"); resourcePath = resourcePath.replace("{format}","json");
String method = "$method.methodType$"; String method = "$method.methodType$";
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
$if(!method.inputModel)$ Map<String, String> headerParams = new HashMap<String, String>();
$if(!method.inputModel)$
$method.queryParameters:{ argument | $method.queryParameters:{ argument |
if( $argument.name$ != null) { if( $argument.name$ != null) {
queryParams.put("$argument.name$", APIInvoker.toPathValue($argument.name$)); queryParams.put("$argument.name$", APIInvoker.toPathValue($argument.name$));
@ -84,6 +81,12 @@ $method.pathParameters:{ argument |
resourcePath = resourcePath.replace("{$argument.name$}", $argument.name$); resourcePath = resourcePath.replace("{$argument.name$}", $argument.name$);
} }
}$ }$
$method.headerParameters:{ argument |
if( $argument.name$ != null) {
headerParams.put("$argument.name$", APIInvoker.toPathValue($argument.name$));
}
}$
$endif$ $endif$
$if(method.inputModel)$ $if(method.inputModel)$
$method.queryParameters:{ argument | $method.queryParameters:{ argument |
@ -96,14 +99,19 @@ $method.pathParameters:{ argument |
resourcePath = resourcePath.replace("{$argument.name$}", $argument.methodNameFromModelClass$); resourcePath = resourcePath.replace("{$argument.name$}", $argument.methodNameFromModelClass$);
} }
}$ }$
$method.headerParameters:{ argument |
if( $argument.inputModelClassArgument$ != null && $argument.methodNameFromModelClass$ != null) {
headerParams.put("$argument.name$", $argument.methodNameFromModelClass$);
}
}$
$endif$ $endif$
//make the API Call //make the API Call
$if(method.postObject)$ $if(method.postObject)$
String response = getApiInvoker().invokeAPI(resourcePath, method, queryParams, postData); String response = getApiInvoker().invokeAPI(resourcePath, method, queryParams, postData, headerParams);
$endif$ $endif$
$if(!method.postObject)$ $if(!method.postObject)$
String response = getApiInvoker().invokeAPI(resourcePath, method, queryParams, null); String response = getApiInvoker().invokeAPI(resourcePath, method, queryParams, null, headerParams);
$endif$ $endif$
$if(!method.responseVoid)$ $if(!method.responseVoid)$

View File

@ -131,16 +131,16 @@ public class APIInvoker {
* *
* @param resourceURL - URL for the rest resource * @param resourceURL - URL for the rest resource
* @param method - Method we should use for communicating to the back end. * @param method - Method we should use for communicating to the back end.
* @param postObject - if the method is POST, provide the object that should be sent as part of post request. * @param postData - if the method is POST, provide the object that should be sent as part of post request.
* @return JSON response of the API call. * @return JSON response of the API call.
* @throws com.wordnik.swagger.exception.APIException if the call to API server fails. * @throws com.wordnik.swagger.runtime.exception.APIException if the call to API server fails.
*/ */
public static String invokeAPI(String resourceURL, String method, Map<String, public String invokeAPI(String resourceURL, String method, Map<String,
String> queryParams, Object postData) throws APIException { String> queryParams, Object postData, Map<String, String> headerParams) throws APIException {
Client apiClient = Client.create(); Client apiClient = Client.create();
//check for app server values //check for app server values
if(getApiServer() == null || getApiServer().length() == 0) { if(getApiServer() == null || getApiServer().length() == 0) {
String[] args = {getApiServer()}; String[] args = {getApiServer()};
@ -155,7 +155,7 @@ public class APIInvoker {
apiClient.addFilter(new LoggingFilter(logger)); apiClient.addFilter(new LoggingFilter(logger));
} }
} }
//make the communication //make the communication
resourceURL = getApiServer() + resourceURL; resourceURL = getApiServer() + resourceURL;
if(queryParams.keySet().size() > 0){ if(queryParams.keySet().size() > 0){
@ -181,7 +181,12 @@ public class APIInvoker {
for(String key : headerMap.keySet()){ for(String key : headerMap.keySet()){
builder.header(key, headerMap.get(key)); builder.header(key, headerMap.get(key));
} }
if(headerParams != null){
for(String key : headerParams.keySet()){
builder.header(key, headerParams.get(key));
}
}
ClientResponse clientResponse = null; ClientResponse clientResponse = null;
if(method.equals(GET)) { if(method.equals(GET)) {
clientResponse = builder.get(ClientResponse.class); clientResponse = builder.get(ClientResponse.class);

View File

@ -66,12 +66,19 @@ $endif$
var resourcePath = "$method.resourcePath$".replace("{format}","json") var resourcePath = "$method.resourcePath$".replace("{format}","json")
val method = "$method.methodType$"; val method = "$method.methodType$";
var queryParams = new HashMap[String, String] var queryParams = new HashMap[String, String]
var headerParams = new HashMap[String, String]
$if(!method.inputModel)$ $if(!method.inputModel)$
$method.queryParameters:{ argument | $method.queryParameters:{ argument |
if(null != $argument.name$) { if(null != $argument.name$) {
queryParams += "$argument.name$" -> APIInvoker.toPathValue($argument.name$) queryParams += "$argument.name$" -> APIInvoker.toPathValue($argument.name$)
} }
}$ }$
$method.headerParameters:{ argument |
if(null != $argument.name$) {
headerParams += "$argument.name$" -> APIInvoker.toPathValue($argument.name$)
}
}$
$method.pathParameters:{ argument | $method.pathParameters:{ argument |
if(null != $argument.name$) { if(null != $argument.name$) {
resourcePath = resourcePath.replace("{$argument.name$}", $argument.name$) resourcePath = resourcePath.replace("{$argument.name$}", $argument.name$)
@ -84,6 +91,11 @@ $method.queryParameters:{ argument |
queryParams += "$argument.name$" -> $argument.methodNameFromModelClass$ queryParams += "$argument.name$" -> $argument.methodNameFromModelClass$
} }
}$ }$
$method.headerParameters:{ argument |
if(null != $argument.inputModelClassArgument$ && null != $argument.methodNameFromModelClass$ ) {
headerParams += "$argument.name$" -> $argument.methodNameFromModelClass$
}
}$
$method.pathParameters:{ argument | $method.pathParameters:{ argument |
if(null != $argument.inputModelClassArgument$ && null != $argument.methodNameFromModelClass$ ) { if(null != $argument.inputModelClassArgument$ && null != $argument.methodNameFromModelClass$ ) {
resourcePath = resourcePath.replace("{$argument.name$}", $argument.methodNameFromModelClass$) resourcePath = resourcePath.replace("{$argument.name$}", $argument.methodNameFromModelClass$)
@ -93,15 +105,15 @@ $endif$
//make the API Call //make the API Call
$if(method.hasResponseValue)$ $if(method.hasResponseValue)$
$if(method.postObject)$ $if(method.postObject)$
val response = APIInvoker.getApiInvoker.invokeAPI(resourcePath, method, queryParams, postData) val response = APIInvoker.getApiInvoker.invokeAPI(resourcePath, method, queryParams, postData, headerParams)
$else$ $else$
val response = APIInvoker.getApiInvoker.invokeAPI(resourcePath, method, queryParams, null) val response = APIInvoker.getApiInvoker.invokeAPI(resourcePath, method, queryParams, null, headerParams)
$endif$ $endif$
$else$ $else$
$if(method.postObject)$ $if(method.postObject)$
APIInvoker.getApiInvoker.invokeAPI(resourcePath, method, queryParams, postData) APIInvoker.getApiInvoker.invokeAPI(resourcePath, method, queryParams, postData, headerParams)
$else$ $else$
APIInvoker.getApiInvoker.invokeAPI(resourcePath, method, queryParams, null) APIInvoker.getApiInvoker.invokeAPI(resourcePath, method, queryParams, null, headerParams)
$endif$ $endif$
$endif$ $endif$
$if(!method.responseVoid)$ $if(!method.responseVoid)$

View File

@ -268,8 +268,12 @@ public class LibraryCodeGenerator {
else{ else{
valuePrefix = valueSuffix = ""; valuePrefix = valueSuffix = "";
}; };
String namePrefix = "";
if(isNameStartsWithInteger(allowableValue) && !canEnumNameStartsWithNumber()){
namePrefix = "ENUM_";
}
template.setAttribute("values.{name,value}", template.setAttribute("values.{name,value}",
this.getNameGenerator().applyClassNamingPolicy(allowableValue.replaceAll("-","_")), namePrefix+this.getNameGenerator().applyClassNamingPolicy(allowableValue.replaceAll("-","_")),
this.getNameGenerator().applyMethodNamingPolicy(valuePrefix.concat(allowableValue).concat(valueSuffix))); this.getNameGenerator().applyMethodNamingPolicy(valuePrefix.concat(allowableValue).concat(valueSuffix)));
} }
template.setAttribute(PACKAGE_NAME, config.getModelPackageName()); template.setAttribute(PACKAGE_NAME, config.getModelPackageName());
@ -288,6 +292,15 @@ public class LibraryCodeGenerator {
} }
} }
private boolean isNameStartsWithInteger(String name) {
for(int i=0; i <= 9 ; i++){
if(name.startsWith(i+"")){
return true;
}
}
return false;
}
private void generateOutputWrappers(List<Resource> resources, StringTemplateGroup templateGroup) { private void generateOutputWrappers(List<Resource> resources, StringTemplateGroup templateGroup) {
List<String> generatedClasses = new ArrayList<String>(); List<String> generatedClasses = new ArrayList<String>();
StringTemplate template = templateGroup.getInstanceOf(WRAPPER_OBJECT_TEMPLATE); StringTemplate template = templateGroup.getInstanceOf(WRAPPER_OBJECT_TEMPLATE);
@ -482,6 +495,14 @@ public class LibraryCodeGenerator {
return nameGenerator; return nameGenerator;
} }
/**
* In java enum names can't start with number so if the enums are numbers then we prefix them with ENUM_
* @return
*/
protected boolean canEnumNameStartsWithNumber() {
return true;
}
protected CodeGenRulesProvider readRulesProviderConfig(String rulesProviderLocation, ObjectMapper mapper, File configFile) { protected CodeGenRulesProvider readRulesProviderConfig(String rulesProviderLocation, ObjectMapper mapper, File configFile) {
CodeGenRulesProvider codeGenRules = null; CodeGenRulesProvider codeGenRules = null;
try { try {

View File

@ -26,6 +26,7 @@ public class ResourceMethod {
private List<MethodArgument> arguments; private List<MethodArgument> arguments;
private List<MethodArgument> queryParameters; private List<MethodArgument> queryParameters;
private List<MethodArgument> pathParameters; private List<MethodArgument> pathParameters;
private List<MethodArgument> headerParameters;
//set the original response name, this is used in identifying if the response is single valued or multi valued //set the original response name, this is used in identifying if the response is single valued or multi valued
private String returnValueFromOperationJson; private String returnValueFromOperationJson;
private String returnValue; private String returnValue;
@ -89,7 +90,16 @@ public class ResourceMethod {
public void setPathParameters(List<MethodArgument> pathParameters) { public void setPathParameters(List<MethodArgument> pathParameters) {
this.pathParameters = pathParameters; this.pathParameters = pathParameters;
} }
public List<MethodArgument> getHeaderParameters() {
return headerParameters;
}
public void setHeaderParameters(List<MethodArgument> headerParameters) {
this.headerParameters = headerParameters;
}
public String getReturnValue() { public String getReturnValue() {
return returnValue; return returnValue;
} }

View File

@ -93,4 +93,10 @@ public class JavaLibCodeGen extends LibraryCodeGenerator {
return javaConfiguration; return javaConfiguration;
} }
@Override
protected boolean canEnumNameStartsWithNumber() {
return false;
}
} }

View File

@ -214,10 +214,13 @@ public class EndpointOperation {
List<MethodArgument> arguments = new ArrayList<MethodArgument>(); List<MethodArgument> arguments = new ArrayList<MethodArgument>();
List<MethodArgument> queryParams= new ArrayList<MethodArgument>(); List<MethodArgument> queryParams= new ArrayList<MethodArgument>();
List<MethodArgument> pathParams= new ArrayList<MethodArgument>(); List<MethodArgument> pathParams= new ArrayList<MethodArgument>();
List<MethodArgument> headerParams= new ArrayList<MethodArgument>();
method.setArguments(arguments); method.setArguments(arguments);
method.setQueryParameters(queryParams); method.setQueryParameters(queryParams);
method.setPathParameters(pathParams); method.setPathParameters(pathParams);
method.setHeaderParameters(headerParams);
for(ModelField modelField : this.getParameters()){ for(ModelField modelField : this.getParameters()){
if(!argNames.contains(modelField.getName())) { if(!argNames.contains(modelField.getName())) {
argNames.add(modelField.getName()); argNames.add(modelField.getName());
@ -233,9 +236,14 @@ public class EndpointOperation {
anArgument.setRequired(modelField.isRequired()); anArgument.setRequired(modelField.isRequired());
anArgument.setDefaultValue(modelField.getDefaultValue()); anArgument.setDefaultValue(modelField.getDefaultValue());
arguments.add(anArgument); arguments.add(anArgument);
headerParams.add(anArgument);
}else if(modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) && }else if(modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_HEADER) &&
modelField.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 anArgument.setName(API_KEY_PARAM_NAME);
anArgument.setDataType(MethodArgument.ARGUMENT_STRING);
anArgument.setRequired(true);
arguments.add(anArgument);
headerParams.add(anArgument);
}else if (modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_PATH) && }else if (modelField.getParamType().equalsIgnoreCase(PARAM_TYPE_PATH) &&
!modelField.getName().equalsIgnoreCase(FORMAT_PARAM_NAME)) { !modelField.getName().equalsIgnoreCase(FORMAT_PARAM_NAME)) {
anArgument.setName(modelField.getName()); anArgument.setName(modelField.getName());

View File

@ -149,7 +149,7 @@ public class APIInvoker {
* @throws com.wordnik.swagger.runtime.exception.APIException if the call to API server fails. * @throws com.wordnik.swagger.runtime.exception.APIException if the call to API server fails.
*/ */
public String invokeAPI(String resourceURL, String method, Map<String, public String invokeAPI(String resourceURL, String method, Map<String,
String> queryParams, Object postData) throws APIException { String> queryParams, Object postData, Map<String, String> headerParams) throws APIException {
Client apiClient = Client.create(); Client apiClient = Client.create();
@ -194,7 +194,12 @@ public class APIInvoker {
for(String key : headerMap.keySet()){ for(String key : headerMap.keySet()){
builder.header(key, headerMap.get(key)); builder.header(key, headerMap.get(key));
} }
if(headerParams != null){
for(String key : headerParams.keySet()){
builder.header(key, headerParams.get(key));
}
}
ClientResponse clientResponse = null; ClientResponse clientResponse = null;
if(method.equals(GET)) { if(method.equals(GET)) {
clientResponse = builder.get(ClientResponse.class); clientResponse = builder.get(ClientResponse.class);