Handling resource methods with the same name in languages where overloading is not supported

This commit is contained in:
Deepak Michael 2011-12-13 07:47:13 +05:30
parent 2038a9a8c1
commit 2c5c94d2e0
7 changed files with 87 additions and 17 deletions

View File

@ -53,7 +53,7 @@ package com.wordnik.swagger.common
_apiEventNotifier = eventNotifier;
}
public function invokeAPI(authToken: String , resourceURL: String, method: String, queryParams: Dictionary, postObject: Object): AsyncToken {
public function invokeAPI(resourceURL: String, method: String, queryParams: Dictionary, postObject: Object, headerParams: Dictionary): AsyncToken {
//make the communication
if(_useProxyServer) {
resourceURL = resourceURL = _apiProxyServerUrl + resourceURL;

View File

@ -54,7 +54,7 @@ $if(method.hasArguments)$
public function $method.name$($method.argumentDefinitions; separator=", "$): String {
$if(method.authToken)$
if(authToken == null || authToken.length == 0) {
if(_apiUsageCredentials == null || _apiUsageCredentials.authToken == null || _apiUsageCredentials.authToken.length == 0) {
throw new ApiError(ApiErrorCodes.AUTH_TOKEN_NOT_VALID);
}$endif$
var requestId: String = getUniqueId();
@ -63,6 +63,7 @@ $if(method.authToken)$
resourcePath = resourcePath.replace("{format}","xml");
var method: String = "$method.methodType$";
var queryParams:Dictionary = new Dictionary();
var headerParams:Dictionary = new Dictionary();
$if(!method.inputModel)$
$method.queryParameters:{ argument |
if( $argument.name$ != null) {
@ -74,6 +75,11 @@ $method.pathParameters:{ argument |
resourcePath = resourcePath.replace("{$argument.name$}", $argument.name$);
}
}$
$method.headerParameters:{ argument |
if( $argument.name$ != null) {
headerParams["$argument.name$"] = toPathValue($argument.name$);
}
}$
$endif$
$if(method.inputModel)$
$method.queryParameters:{ argument |
@ -86,23 +92,28 @@ $method.pathParameters:{ argument |
resourcePath = resourcePath.replace("{$argument.name$}", $argument.methodNameFromModelClass$);
}
}$
$method.headerParameters:{ argument |
if( $argument.inputModelClassArgument$ != null && $argument.methodNameFromModelClass$ != null) {
headerParams["$argument.name$"] = $argument.methodNameFromModelClass$;
}
}$
$endif$
//make the API Call
$if(method.postObject)$
$if(method.authToken)$
var token:AsyncToken = getApiInvoker().invokeAPI(authToken, resourcePath, method, queryParams, postData);
var token:AsyncToken = getApiInvoker().invokeAPI(resourcePath, method, queryParams, postData, headerParams);
$endif$
$if(!method.authToken)$
var token:AsyncToken = getApiInvoker().invokeAPI(null, resourcePath, method, queryParams, postData);
var token:AsyncToken = getApiInvoker().invokeAPI(resourcePath, method, queryParams, postData, headerParams);
$endif$
$endif$
$if(!method.postObject)$
$if(method.authToken)$
var token:AsyncToken = getApiInvoker().invokeAPI(authToken, resourcePath, method, queryParams, null);
var token:AsyncToken = getApiInvoker().invokeAPI(resourcePath, method, queryParams, null, headerParams);
$endif$
$if(!method.authToken)$
var token:AsyncToken = getApiInvoker().invokeAPI(null, resourcePath, method, queryParams, null);
var token:AsyncToken = getApiInvoker().invokeAPI(resourcePath, method, queryParams, null, headerParams);
$endif$
$endif$

View File

@ -366,7 +366,7 @@ public class LibraryCodeGenerator {
List<ResourceMethod> methods = new ArrayList<ResourceMethod>();
List<String> imports = new ArrayList<String>();
imports.addAll(this.config.getDefaultServiceImports());
methods = resource.generateMethods(resource, dataTypeMappingProvider, nameGenerator);
methods = resource.generateMethods(resource, dataTypeMappingProvider, nameGenerator, languageConfig);
StringTemplate template = templateGroup.getInstanceOf(API_OBJECT_TEMPLATE);
String className = resource.generateClassName(nameGenerator);

View File

@ -34,6 +34,7 @@ public class LanguageConfiguration {
private String annotationPackageName;
private boolean isModelEnumRequired = true;
private boolean isOutputWrapperRequired = false;
private boolean isMethodOverloadingSupported = true;
public String getClassFileExtension() {
return classFileExtension;
@ -147,4 +148,12 @@ public class LanguageConfiguration {
+ ", isModelEnumRequired=" + isModelEnumRequired
+ ", isOutputWrapperRequired=" + isOutputWrapperRequired + "]";
}
public boolean isMethodOverloadingSupported() {
return isMethodOverloadingSupported;
}
public void setMethodOverloadingSupported(boolean methodOverloadingSupported) {
isMethodOverloadingSupported = methodOverloadingSupported;
}
}

View File

@ -88,6 +88,7 @@ public class As3LibCodeGen extends LibraryCodeGenerator{
as3Configuration.setModelEnumRequired(false);
as3Configuration.setOutputWrapperRequired(true);
as3Configuration.setMethodOverloadingSupported(false);
return as3Configuration;
}
@ -108,12 +109,14 @@ public class As3LibCodeGen extends LibraryCodeGenerator{
for(Model model : resource.getModels()){
for(ModelField modelField : model.getFields()){
final String collectionItemType = modelField.getFieldDefinition().getCollectionItemType();
if(collectionItemType != null){
refModelField = new ModelField();
refModelField.setName(modelField.getName());
refModelField.setParamType(collectionItemType);
refFields.add(refModelField);
if (modelField.getFieldDefinition() != null) {
final String collectionItemType = modelField.getFieldDefinition().getCollectionItemType();
if(collectionItemType != null){
refModelField = new ModelField();
refModelField.setName(modelField.getName() + model.getName());
refModelField.setParamType(collectionItemType);
refFields.add(refModelField);
}
}
}
}

View File

@ -18,6 +18,7 @@ package com.wordnik.swagger.codegen.resource;
import com.wordnik.swagger.codegen.ResourceMethod;
import com.wordnik.swagger.codegen.config.DataTypeMappingProvider;
import com.wordnik.swagger.codegen.config.LanguageConfiguration;
import com.wordnik.swagger.codegen.config.NamingPolicyProvider;
import java.util.ArrayList;
@ -72,14 +73,27 @@ public class Endpoint {
this.operations = operations;
}
public List<ResourceMethod> generateMethods(Resource resource, DataTypeMappingProvider dataTypeMapper, NamingPolicyProvider nameGenerator) {
public List<ResourceMethod> generateMethods(Resource resource, DataTypeMappingProvider dataTypeMapper,
NamingPolicyProvider nameGenerator, LanguageConfiguration languageConfig) {
if(methods == null){
methods = new ArrayList<ResourceMethod>();
ResourceMethod newMethod;
List<String> endPointMethodNames = new ArrayList<String>();
if(getOperations() != null) {
for(EndpointOperation operation: getOperations()) {
//Note: Currently we are generating methods for depricated APIs also, We should provide this deprecation info on generated APIs also.
if(areModelsAvailable(operation.getParameters(), resource, dataTypeMapper)) {
methods.add(operation.generateMethod(this, resource, dataTypeMapper, nameGenerator));
newMethod = operation.generateMethod(this, resource, dataTypeMapper, nameGenerator);
if (!endPointMethodNames.contains(newMethod.getName())) {
methods.add(newMethod);
}
else{
//handleOverloadingSupport
if(!languageConfig.isMethodOverloadingSupported()){
handleOverloadedMethod(newMethod, endPointMethodNames);
}
}
endPointMethodNames.add(newMethod.getName());
}
}
}
@ -87,6 +101,25 @@ public class Endpoint {
return methods;
}
void handleOverloadedMethod(ResourceMethod method, List<String> methods) {
//handleOverloadingSupport
int counter = 1;
String newMethodName;
boolean generatedNewName = false;
do{
newMethodName = method.getName() + counter;
if (!methods.contains(newMethodName)){
method.setName(newMethodName);
generatedNewName = true;
}
System.out.println("Handling overloaded method for " + method.getName());
counter++;
}while (!generatedNewName);
System.out.println("Handling overloaded method : New method name - " + method.getName());
}
private boolean areModelsAvailable(List<ModelField> modelFields, Resource resource, DataTypeMappingProvider dataTypeMapper) {
Boolean isParamSetAvailable = true;
if(modelFields == null) return true;

View File

@ -18,6 +18,7 @@ package com.wordnik.swagger.codegen.resource;
import com.wordnik.swagger.codegen.ResourceMethod;
import com.wordnik.swagger.codegen.config.DataTypeMappingProvider;
import com.wordnik.swagger.codegen.config.LanguageConfiguration;
import com.wordnik.swagger.codegen.config.NamingPolicyProvider;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
@ -119,12 +120,25 @@ public class Resource {
return generatedClassName;
}
public List<ResourceMethod> generateMethods(Resource resource, DataTypeMappingProvider dataTypeMapper, NamingPolicyProvider nameGenerator) {
public List<ResourceMethod> generateMethods(Resource resource, DataTypeMappingProvider dataTypeMapper,
NamingPolicyProvider nameGenerator, LanguageConfiguration languageConfig) {
if(methods == null){
methods = new ArrayList<ResourceMethod>();
List<ResourceMethod> newMethods = new ArrayList<ResourceMethod>();
List<String> endPointMethodNames = new ArrayList<String>();
if(getEndPoints() != null) {
for(Endpoint endpoint: getEndPoints()){
methods.addAll(endpoint.generateMethods(resource, dataTypeMapper, nameGenerator));
newMethods = endpoint.generateMethods(resource, dataTypeMapper, nameGenerator, languageConfig);
if (!languageConfig.isMethodOverloadingSupported()) {
for(ResourceMethod newMethod: newMethods){
if(endPointMethodNames.contains( newMethod.getName() )) {
endpoint.handleOverloadedMethod(newMethod, endPointMethodNames);
}
endPointMethodNames.add(newMethod.getName());
}
}
methods.addAll(newMethods);
}
}
}