Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 16 additions & 5 deletions conf/as3/templates/ResourceObject.st
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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 |
Expand All @@ -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$

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ private void generateAPIClasses(List<Resource> resources, StringTemplateGroup te
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,4 +148,12 @@ public String toString() {
+ ", isModelEnumRequired=" + isModelEnumRequired
+ ", isOutputWrapperRequired=" + isOutputWrapperRequired + "]";
}

public boolean isMethodOverloadingSupported() {
return isMethodOverloadingSupported;
}

public void setMethodOverloadingSupported(boolean methodOverloadingSupported) {
isMethodOverloadingSupported = methodOverloadingSupported;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected LanguageConfiguration initializeLangConfig(LanguageConfiguration as3Co

as3Configuration.setModelEnumRequired(false);
as3Configuration.setOutputWrapperRequired(true);
as3Configuration.setMethodOverloadingSupported(false);
return as3Configuration;
}

Expand All @@ -108,12 +109,14 @@ private void generateReferencesObject(List<Resource> resources, StringTemplateGr
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);
}
}
}
}
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/com/wordnik/swagger/codegen/resource/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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;
Expand Down Expand Up @@ -72,21 +73,53 @@ public void setOperations(List<EndpointOperation> operations) {
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());
}
}
}
}
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;
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/wordnik/swagger/codegen/resource/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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;
Expand Down Expand Up @@ -119,12 +120,25 @@ public String generateClassName(NamingPolicyProvider nameGenerator) {
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);
}
}
}
Expand Down