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,
"resourceId" : 12,
"input" : {
"orderId":"1"
"orderId":"3"
},
"assertions" : [
{

View File

@ -131,12 +131,12 @@ public class APIInvoker {
*
* @param resourceURL - URL for the rest resource
* @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.
* @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,
String> queryParams, Object postData) throws APIException {
public String invokeAPI(String resourceURL, String method, Map<String,
String> queryParams, Object postData, Map<String, String> headerParams) throws APIException {
Client apiClient = Client.create();
@ -181,6 +181,11 @@ public class APIInvoker {
for(String key : headerMap.keySet()){
builder.header(key, headerMap.get(key));
}
if(headerParams != null){
for(String key : headerParams.keySet()){
builder.header(key, headerParams.get(key));
}
}
ClientResponse clientResponse = null;
if(method.equals(GET)) {

View File

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

View File

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

View File

@ -131,12 +131,12 @@ public class APIInvoker {
*
* @param resourceURL - URL for the rest resource
* @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.
* @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,
String> queryParams, Object postData) throws APIException {
public String invokeAPI(String resourceURL, String method, Map<String,
String> queryParams, Object postData, Map<String, String> headerParams) throws APIException {
Client apiClient = Client.create();
@ -181,6 +181,11 @@ public class APIInvoker {
for(String key : headerMap.keySet()){
builder.header(key, headerMap.get(key));
}
if(headerParams != null){
for(String key : headerParams.keySet()){
builder.header(key, headerParams.get(key));
}
}
ClientResponse clientResponse = null;
if(method.equals(GET)) {

View File

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

View File

@ -268,8 +268,12 @@ public class LibraryCodeGenerator {
else{
valuePrefix = valueSuffix = "";
};
String namePrefix = "";
if(isNameStartsWithInteger(allowableValue) && !canEnumNameStartsWithNumber()){
namePrefix = "ENUM_";
}
template.setAttribute("values.{name,value}",
this.getNameGenerator().applyClassNamingPolicy(allowableValue.replaceAll("-","_")),
namePrefix+this.getNameGenerator().applyClassNamingPolicy(allowableValue.replaceAll("-","_")),
this.getNameGenerator().applyMethodNamingPolicy(valuePrefix.concat(allowableValue).concat(valueSuffix)));
}
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) {
List<String> generatedClasses = new ArrayList<String>();
StringTemplate template = templateGroup.getInstanceOf(WRAPPER_OBJECT_TEMPLATE);
@ -482,6 +495,14 @@ public class LibraryCodeGenerator {
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) {
CodeGenRulesProvider codeGenRules = null;
try {

View File

@ -26,6 +26,7 @@ public class ResourceMethod {
private List<MethodArgument> arguments;
private List<MethodArgument> queryParameters;
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
private String returnValueFromOperationJson;
private String returnValue;
@ -90,6 +91,15 @@ public class ResourceMethod {
this.pathParameters = pathParameters;
}
public List<MethodArgument> getHeaderParameters() {
return headerParameters;
}
public void setHeaderParameters(List<MethodArgument> headerParameters) {
this.headerParameters = headerParameters;
}
public String getReturnValue() {
return returnValue;
}

View File

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

View File

@ -214,9 +214,12 @@ public class EndpointOperation {
List<MethodArgument> arguments = new ArrayList<MethodArgument>();
List<MethodArgument> queryParams= new ArrayList<MethodArgument>();
List<MethodArgument> pathParams= new ArrayList<MethodArgument>();
List<MethodArgument> headerParams= new ArrayList<MethodArgument>();
method.setArguments(arguments);
method.setQueryParameters(queryParams);
method.setPathParameters(pathParams);
method.setHeaderParameters(headerParams);
for(ModelField modelField : this.getParameters()){
if(!argNames.contains(modelField.getName())) {
@ -233,9 +236,14 @@ public class EndpointOperation {
anArgument.setRequired(modelField.isRequired());
anArgument.setDefaultValue(modelField.getDefaultValue());
arguments.add(anArgument);
headerParams.add(anArgument);
}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
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) &&
!modelField.getName().equalsIgnoreCase(FORMAT_PARAM_NAME)) {
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.
*/
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();
@ -194,6 +194,11 @@ public class APIInvoker {
for(String key : headerMap.keySet()){
builder.header(key, headerMap.get(key));
}
if(headerParams != null){
for(String key : headerParams.keySet()){
builder.header(key, headerParams.get(key));
}
}
ClientResponse clientResponse = null;
if(method.equals(GET)) {