forked from loafle/openapi-generator-original
Handling resource methods with the same name in languages where overloading is not supported
This commit is contained in:
parent
2038a9a8c1
commit
2c5c94d2e0
@ -53,7 +53,7 @@ package com.wordnik.swagger.common
|
|||||||
_apiEventNotifier = eventNotifier;
|
_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
|
//make the communication
|
||||||
if(_useProxyServer) {
|
if(_useProxyServer) {
|
||||||
resourceURL = resourceURL = _apiProxyServerUrl + resourceURL;
|
resourceURL = resourceURL = _apiProxyServerUrl + resourceURL;
|
||||||
|
@ -54,7 +54,7 @@ $if(method.hasArguments)$
|
|||||||
public function $method.name$($method.argumentDefinitions; separator=", "$): String {
|
public function $method.name$($method.argumentDefinitions; separator=", "$): String {
|
||||||
|
|
||||||
$if(method.authToken)$
|
$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);
|
throw new ApiError(ApiErrorCodes.AUTH_TOKEN_NOT_VALID);
|
||||||
}$endif$
|
}$endif$
|
||||||
var requestId: String = getUniqueId();
|
var requestId: String = getUniqueId();
|
||||||
@ -63,6 +63,7 @@ $if(method.authToken)$
|
|||||||
resourcePath = resourcePath.replace("{format}","xml");
|
resourcePath = resourcePath.replace("{format}","xml");
|
||||||
var method: String = "$method.methodType$";
|
var method: String = "$method.methodType$";
|
||||||
var queryParams:Dictionary = new Dictionary();
|
var queryParams:Dictionary = new Dictionary();
|
||||||
|
var headerParams:Dictionary = new Dictionary();
|
||||||
$if(!method.inputModel)$
|
$if(!method.inputModel)$
|
||||||
$method.queryParameters:{ argument |
|
$method.queryParameters:{ argument |
|
||||||
if( $argument.name$ != null) {
|
if( $argument.name$ != null) {
|
||||||
@ -74,6 +75,11 @@ $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["$argument.name$"] = toPathValue($argument.name$);
|
||||||
|
}
|
||||||
|
}$
|
||||||
$endif$
|
$endif$
|
||||||
$if(method.inputModel)$
|
$if(method.inputModel)$
|
||||||
$method.queryParameters:{ argument |
|
$method.queryParameters:{ argument |
|
||||||
@ -86,23 +92,28 @@ $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["$argument.name$"] = $argument.methodNameFromModelClass$;
|
||||||
|
}
|
||||||
|
}$
|
||||||
$endif$
|
$endif$
|
||||||
//make the API Call
|
//make the API Call
|
||||||
$if(method.postObject)$
|
$if(method.postObject)$
|
||||||
$if(method.authToken)$
|
$if(method.authToken)$
|
||||||
var token:AsyncToken = getApiInvoker().invokeAPI(authToken, resourcePath, method, queryParams, postData);
|
var token:AsyncToken = getApiInvoker().invokeAPI(resourcePath, method, queryParams, postData, headerParams);
|
||||||
$endif$
|
$endif$
|
||||||
$if(!method.authToken)$
|
$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$
|
||||||
$endif$
|
$endif$
|
||||||
|
|
||||||
$if(!method.postObject)$
|
$if(!method.postObject)$
|
||||||
$if(method.authToken)$
|
$if(method.authToken)$
|
||||||
var token:AsyncToken = getApiInvoker().invokeAPI(authToken, resourcePath, method, queryParams, null);
|
var token:AsyncToken = getApiInvoker().invokeAPI(resourcePath, method, queryParams, null, headerParams);
|
||||||
$endif$
|
$endif$
|
||||||
$if(!method.authToken)$
|
$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$
|
||||||
$endif$
|
$endif$
|
||||||
|
|
||||||
|
@ -366,7 +366,7 @@ public class LibraryCodeGenerator {
|
|||||||
List<ResourceMethod> methods = new ArrayList<ResourceMethod>();
|
List<ResourceMethod> methods = new ArrayList<ResourceMethod>();
|
||||||
List<String> imports = new ArrayList<String>();
|
List<String> imports = new ArrayList<String>();
|
||||||
imports.addAll(this.config.getDefaultServiceImports());
|
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);
|
StringTemplate template = templateGroup.getInstanceOf(API_OBJECT_TEMPLATE);
|
||||||
String className = resource.generateClassName(nameGenerator);
|
String className = resource.generateClassName(nameGenerator);
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@ public class LanguageConfiguration {
|
|||||||
private String annotationPackageName;
|
private String annotationPackageName;
|
||||||
private boolean isModelEnumRequired = true;
|
private boolean isModelEnumRequired = true;
|
||||||
private boolean isOutputWrapperRequired = false;
|
private boolean isOutputWrapperRequired = false;
|
||||||
|
private boolean isMethodOverloadingSupported = true;
|
||||||
|
|
||||||
public String getClassFileExtension() {
|
public String getClassFileExtension() {
|
||||||
return classFileExtension;
|
return classFileExtension;
|
||||||
@ -147,4 +148,12 @@ public class LanguageConfiguration {
|
|||||||
+ ", isModelEnumRequired=" + isModelEnumRequired
|
+ ", isModelEnumRequired=" + isModelEnumRequired
|
||||||
+ ", isOutputWrapperRequired=" + isOutputWrapperRequired + "]";
|
+ ", isOutputWrapperRequired=" + isOutputWrapperRequired + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isMethodOverloadingSupported() {
|
||||||
|
return isMethodOverloadingSupported;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMethodOverloadingSupported(boolean methodOverloadingSupported) {
|
||||||
|
isMethodOverloadingSupported = methodOverloadingSupported;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,6 +88,7 @@ public class As3LibCodeGen extends LibraryCodeGenerator{
|
|||||||
|
|
||||||
as3Configuration.setModelEnumRequired(false);
|
as3Configuration.setModelEnumRequired(false);
|
||||||
as3Configuration.setOutputWrapperRequired(true);
|
as3Configuration.setOutputWrapperRequired(true);
|
||||||
|
as3Configuration.setMethodOverloadingSupported(false);
|
||||||
return as3Configuration;
|
return as3Configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,15 +109,17 @@ public class As3LibCodeGen extends LibraryCodeGenerator{
|
|||||||
for(Model model : resource.getModels()){
|
for(Model model : resource.getModels()){
|
||||||
|
|
||||||
for(ModelField modelField : model.getFields()){
|
for(ModelField modelField : model.getFields()){
|
||||||
|
if (modelField.getFieldDefinition() != null) {
|
||||||
final String collectionItemType = modelField.getFieldDefinition().getCollectionItemType();
|
final String collectionItemType = modelField.getFieldDefinition().getCollectionItemType();
|
||||||
if(collectionItemType != null){
|
if(collectionItemType != null){
|
||||||
refModelField = new ModelField();
|
refModelField = new ModelField();
|
||||||
refModelField.setName(modelField.getName());
|
refModelField.setName(modelField.getName() + model.getName());
|
||||||
refModelField.setParamType(collectionItemType);
|
refModelField.setParamType(collectionItemType);
|
||||||
refFields.add(refModelField);
|
refFields.add(refModelField);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
refModelField = new ModelField();
|
refModelField = new ModelField();
|
||||||
refModelField.setName( nameGenerator.applyMethodNamingPolicy( resource.generateClassName(nameGenerator) ) );
|
refModelField.setName( nameGenerator.applyMethodNamingPolicy( resource.generateClassName(nameGenerator) ) );
|
||||||
|
@ -18,6 +18,7 @@ package com.wordnik.swagger.codegen.resource;
|
|||||||
|
|
||||||
import com.wordnik.swagger.codegen.ResourceMethod;
|
import com.wordnik.swagger.codegen.ResourceMethod;
|
||||||
import com.wordnik.swagger.codegen.config.DataTypeMappingProvider;
|
import com.wordnik.swagger.codegen.config.DataTypeMappingProvider;
|
||||||
|
import com.wordnik.swagger.codegen.config.LanguageConfiguration;
|
||||||
import com.wordnik.swagger.codegen.config.NamingPolicyProvider;
|
import com.wordnik.swagger.codegen.config.NamingPolicyProvider;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -72,14 +73,27 @@ public class Endpoint {
|
|||||||
this.operations = 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){
|
if(methods == null){
|
||||||
methods = new ArrayList<ResourceMethod>();
|
methods = new ArrayList<ResourceMethod>();
|
||||||
|
ResourceMethod newMethod;
|
||||||
|
List<String> endPointMethodNames = new ArrayList<String>();
|
||||||
if(getOperations() != null) {
|
if(getOperations() != null) {
|
||||||
for(EndpointOperation operation: getOperations()) {
|
for(EndpointOperation operation: getOperations()) {
|
||||||
//Note: Currently we are generating methods for depricated APIs also, We should provide this deprecation info on generated APIs also.
|
//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)) {
|
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;
|
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) {
|
private boolean areModelsAvailable(List<ModelField> modelFields, Resource resource, DataTypeMappingProvider dataTypeMapper) {
|
||||||
Boolean isParamSetAvailable = true;
|
Boolean isParamSetAvailable = true;
|
||||||
if(modelFields == null) return true;
|
if(modelFields == null) return true;
|
||||||
|
@ -18,6 +18,7 @@ package com.wordnik.swagger.codegen.resource;
|
|||||||
|
|
||||||
import com.wordnik.swagger.codegen.ResourceMethod;
|
import com.wordnik.swagger.codegen.ResourceMethod;
|
||||||
import com.wordnik.swagger.codegen.config.DataTypeMappingProvider;
|
import com.wordnik.swagger.codegen.config.DataTypeMappingProvider;
|
||||||
|
import com.wordnik.swagger.codegen.config.LanguageConfiguration;
|
||||||
import com.wordnik.swagger.codegen.config.NamingPolicyProvider;
|
import com.wordnik.swagger.codegen.config.NamingPolicyProvider;
|
||||||
import org.codehaus.jackson.annotate.JsonCreator;
|
import org.codehaus.jackson.annotate.JsonCreator;
|
||||||
import org.codehaus.jackson.annotate.JsonProperty;
|
import org.codehaus.jackson.annotate.JsonProperty;
|
||||||
@ -119,12 +120,25 @@ public class Resource {
|
|||||||
return generatedClassName;
|
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){
|
if(methods == null){
|
||||||
methods = new ArrayList<ResourceMethod>();
|
methods = new ArrayList<ResourceMethod>();
|
||||||
|
List<ResourceMethod> newMethods = new ArrayList<ResourceMethod>();
|
||||||
|
List<String> endPointMethodNames = new ArrayList<String>();
|
||||||
if(getEndPoints() != null) {
|
if(getEndPoints() != null) {
|
||||||
for(Endpoint endpoint: getEndPoints()){
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user