From 60c577a2839e4b58d6b0b15bfa6aa139e5ac87dc Mon Sep 17 00:00:00 2001 From: rpidikiti Date: Mon, 17 Oct 2011 07:09:15 -0700 Subject: [PATCH] 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. --- conf/java/sample/lib-test-script.json | 2 +- .../wordnik/swagger/common/APIInvoker.java | 21 +++++++++------- conf/java/templates/EnumObject.st | 6 ++--- conf/java/templates/ResourceObject.st | 24 ++++++++++++------- .../wordnik/swagger/common/APIInvoker.java | 21 +++++++++------- conf/scala/templates/ResourceObject.st | 20 ++++++++++++---- .../swagger/codegen/LibraryCodeGenerator.java | 23 +++++++++++++++++- .../swagger/codegen/ResourceMethod.java | 12 +++++++++- .../codegen/config/java/JavaLibCodeGen.java | 6 +++++ .../codegen/resource/EndpointOperation.java | 12 ++++++++-- .../swagger/runtime/common/APIInvoker.java | 9 +++++-- 11 files changed, 118 insertions(+), 38 deletions(-) diff --git a/conf/java/sample/lib-test-script.json b/conf/java/sample/lib-test-script.json index 7953c939cfb..7ca629d1c4a 100644 --- a/conf/java/sample/lib-test-script.json +++ b/conf/java/sample/lib-test-script.json @@ -229,7 +229,7 @@ "id" : 1, "resourceId" : 12, "input" : { - "orderId":"1" + "orderId":"3" }, "assertions" : [ { diff --git a/conf/java/structure/src/main/java/com/wordnik/swagger/common/APIInvoker.java b/conf/java/structure/src/main/java/com/wordnik/swagger/common/APIInvoker.java index 6f2d5c4b2db..2074fe630c4 100644 --- a/conf/java/structure/src/main/java/com/wordnik/swagger/common/APIInvoker.java +++ b/conf/java/structure/src/main/java/com/wordnik/swagger/common/APIInvoker.java @@ -131,16 +131,16 @@ 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. - * @return JSON response of the API call. - * @throws com.wordnik.swagger.exception.APIException if the call to API server fails. + * @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.runtime.exception.APIException if the call to API server fails. */ - public static String invokeAPI(String resourceURL, String method, Map queryParams, Object postData) throws APIException { + public String invokeAPI(String resourceURL, String method, Map queryParams, Object postData, Map headerParams) throws APIException { Client apiClient = Client.create(); - + //check for app server values if(getApiServer() == null || getApiServer().length() == 0) { String[] args = {getApiServer()}; @@ -155,7 +155,7 @@ public class APIInvoker { apiClient.addFilter(new LoggingFilter(logger)); } } - + //make the communication resourceURL = getApiServer() + resourceURL; if(queryParams.keySet().size() > 0){ @@ -181,7 +181,12 @@ 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)) { clientResponse = builder.get(ClientResponse.class); diff --git a/conf/java/templates/EnumObject.st b/conf/java/templates/EnumObject.st index f93d4d5c717..944ac697a62 100644 --- a/conf/java/templates/EnumObject.st +++ b/conf/java/templates/EnumObject.st @@ -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; @@ -40,7 +40,7 @@ public enum $className$ { return value; } - @Override public String toString() { + @Override public String toString() { return String.valueOf(this.getValue()); } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/conf/java/templates/ResourceObject.st b/conf/java/templates/ResourceObject.st index 0be5014f185..f71fe465d21 100644 --- a/conf/java/templates/ResourceObject.st +++ b/conf/java/templates/ResourceObject.st @@ -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,7 +69,8 @@ $endif$ resourcePath = resourcePath.replace("{format}","json"); String method = "$method.methodType$"; Map queryParams = new HashMap(); -$if(!method.inputModel)$ + Map headerParams = new HashMap(); +$if(!method.inputModel)$ $method.queryParameters:{ argument | if( $argument.name$ != null) { queryParams.put("$argument.name$", APIInvoker.toPathValue($argument.name$)); @@ -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)$ diff --git a/conf/scala/structure/src/main/java/com/wordnik/swagger/common/APIInvoker.java b/conf/scala/structure/src/main/java/com/wordnik/swagger/common/APIInvoker.java index 6f2d5c4b2db..2074fe630c4 100644 --- a/conf/scala/structure/src/main/java/com/wordnik/swagger/common/APIInvoker.java +++ b/conf/scala/structure/src/main/java/com/wordnik/swagger/common/APIInvoker.java @@ -131,16 +131,16 @@ 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. - * @return JSON response of the API call. - * @throws com.wordnik.swagger.exception.APIException if the call to API server fails. + * @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.runtime.exception.APIException if the call to API server fails. */ - public static String invokeAPI(String resourceURL, String method, Map queryParams, Object postData) throws APIException { + public String invokeAPI(String resourceURL, String method, Map queryParams, Object postData, Map headerParams) throws APIException { Client apiClient = Client.create(); - + //check for app server values if(getApiServer() == null || getApiServer().length() == 0) { String[] args = {getApiServer()}; @@ -155,7 +155,7 @@ public class APIInvoker { apiClient.addFilter(new LoggingFilter(logger)); } } - + //make the communication resourceURL = getApiServer() + resourceURL; if(queryParams.keySet().size() > 0){ @@ -181,7 +181,12 @@ 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)) { clientResponse = builder.get(ClientResponse.class); diff --git a/conf/scala/templates/ResourceObject.st b/conf/scala/templates/ResourceObject.st index 1088ab48bad..f8ad1e7d21a 100644 --- a/conf/scala/templates/ResourceObject.st +++ b/conf/scala/templates/ResourceObject.st @@ -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)$ diff --git a/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java b/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java index dd0fc64ad0b..4587c7dc16a 100644 --- a/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java +++ b/src/main/java/com/wordnik/swagger/codegen/LibraryCodeGenerator.java @@ -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 resources, StringTemplateGroup templateGroup) { List generatedClasses = new ArrayList(); 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 { diff --git a/src/main/java/com/wordnik/swagger/codegen/ResourceMethod.java b/src/main/java/com/wordnik/swagger/codegen/ResourceMethod.java index c56782a37db..b40bde3c84e 100644 --- a/src/main/java/com/wordnik/swagger/codegen/ResourceMethod.java +++ b/src/main/java/com/wordnik/swagger/codegen/ResourceMethod.java @@ -26,6 +26,7 @@ public class ResourceMethod { private List arguments; private List queryParameters; private List pathParameters; + private List 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; @@ -89,7 +90,16 @@ public class ResourceMethod { public void setPathParameters(List pathParameters) { this.pathParameters = pathParameters; } - + + public List getHeaderParameters() { + return headerParameters; + } + + public void setHeaderParameters(List headerParameters) { + this.headerParameters = headerParameters; + } + + public String getReturnValue() { return returnValue; } diff --git a/src/main/java/com/wordnik/swagger/codegen/config/java/JavaLibCodeGen.java b/src/main/java/com/wordnik/swagger/codegen/config/java/JavaLibCodeGen.java index 710f2dc7aab..0407fd7679c 100644 --- a/src/main/java/com/wordnik/swagger/codegen/config/java/JavaLibCodeGen.java +++ b/src/main/java/com/wordnik/swagger/codegen/config/java/JavaLibCodeGen.java @@ -93,4 +93,10 @@ public class JavaLibCodeGen extends LibraryCodeGenerator { return javaConfiguration; } + @Override + protected boolean canEnumNameStartsWithNumber() { + return false; + } + + } diff --git a/src/main/java/com/wordnik/swagger/codegen/resource/EndpointOperation.java b/src/main/java/com/wordnik/swagger/codegen/resource/EndpointOperation.java index 10f76e2766e..5c8af5a67b1 100644 --- a/src/main/java/com/wordnik/swagger/codegen/resource/EndpointOperation.java +++ b/src/main/java/com/wordnik/swagger/codegen/resource/EndpointOperation.java @@ -214,10 +214,13 @@ public class EndpointOperation { List arguments = new ArrayList(); List queryParams= new ArrayList(); List pathParams= new ArrayList(); + List headerParams= new ArrayList(); + method.setArguments(arguments); method.setQueryParameters(queryParams); method.setPathParameters(pathParams); - + method.setHeaderParameters(headerParams); + for(ModelField modelField : this.getParameters()){ if(!argNames.contains(modelField.getName())) { argNames.add(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()); diff --git a/src/main/java/com/wordnik/swagger/runtime/common/APIInvoker.java b/src/main/java/com/wordnik/swagger/runtime/common/APIInvoker.java index 7bbbcc03091..a04d449c85c 100644 --- a/src/main/java/com/wordnik/swagger/runtime/common/APIInvoker.java +++ b/src/main/java/com/wordnik/swagger/runtime/common/APIInvoker.java @@ -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 queryParams, Object postData) throws APIException { + String> queryParams, Object postData, Map headerParams) throws APIException { Client apiClient = Client.create(); @@ -194,7 +194,12 @@ 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)) { clientResponse = builder.get(ClientResponse.class);