From c3f77c1229508279015054a9b4f5586404a88a14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 6 Oct 2017 14:01:51 +0300 Subject: [PATCH] Finalized jquery proxy script generation. --- .../AspNetCoreApiDescriptionModelProvider.cs | 2 +- .../DynamicHttpProxyInterceptor.cs | 4 +-- .../JQuery/JQueryProxyScriptGenerator.cs | 28 +++++++++++++++++++ .../VersioningTests/V1/CallDto.cs | 9 ++++++ .../VersioningTests/V1/CallsController.cs | 8 +----- .../VersioningTests/V2/Calls2Controller.cs | 2 +- .../Volo/Abp/Identity/FixtureController.cs | 2 +- .../Abp/Identity/V4/FixtureV4Controller.cs | 2 +- 8 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V1/CallDto.cs diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs index 1d1dc7d252..156e4114ae 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs @@ -84,7 +84,7 @@ namespace Volo.Abp.AspNetCore.Mvc method, apiDescription.RelativePath, apiDescription.HttpMethod, - setting?.ApiVersions.Select(v => v.ToString()).ToList() ?? new List() + setting?.ApiVersions.Select(v => v.ToString()).ToList() ?? new List() //TODO: Also get from ApiVersion attributes if available..? )); AddParameterDescriptionsToModel(actionModel, method, apiDescription); diff --git a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs index dee7b66238..21f36577d8 100644 --- a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs +++ b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs @@ -132,8 +132,8 @@ namespace Volo.Abp.Http.Client.DynamicProxying { var apiVersion = FindBestApiVersion(action); - //TODO: Make names configurable! - var versionParam = action.Parameters.FirstOrDefault(p => p.Name == "apiVersion") ?? + //TODO: Make names configurable? + var versionParam = action.Parameters.FirstOrDefault(p => p.Name == "apiVersion" && p.BindingSourceId == ParameterBindingSources.Path) ?? action.Parameters.FirstOrDefault(p => p.Name == "api-version" && p.BindingSourceId == ParameterBindingSources.Query); return new ApiVersionInfo(versionParam?.BindingSourceId, apiVersion); diff --git a/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs b/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs index d9fcb8559c..5c399ab83a 100644 --- a/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs +++ b/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs @@ -80,6 +80,16 @@ namespace Volo.Abp.Http.ProxyScripting.Generators.JQuery var parameterList = ProxyScriptingJsFuncHelper.GenerateJsFuncParameterList(action, "ajaxParams"); script.AppendLine($" {controllerName}{ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(normalizedActionName.RemovePostFix("Async").ToCamelCase())} = function({parameterList}) {{"); + + var versionParam = action.Parameters.FirstOrDefault(p => p.Name == "apiVersion" && p.BindingSourceId == ParameterBindingSources.Path) ?? + action.Parameters.FirstOrDefault(p => p.Name == "api-version" && p.BindingSourceId == ParameterBindingSources.Query); + + if (versionParam != null) + { + var version = FindBestApiVersion(action); + script.AppendLine($" var {ProxyScriptingJsFuncHelper.NormalizeJsVariableName(versionParam.Name)} = '{version}';"); + } + script.AppendLine(" return abp.ajax($.extend(true, {"); AddAjaxCallParameters(script, action); @@ -88,6 +98,24 @@ namespace Volo.Abp.Http.ProxyScripting.Generators.JQuery script.AppendLine(" };"); } + private static string FindBestApiVersion(ActionApiDescriptionModel action) + { + //var configuredVersion = GetConfiguredApiVersion(); //TODO: Implement + string configuredVersion = null; + + if (action.SupportedVersions.IsNullOrEmpty()) + { + return configuredVersion ?? "1.0"; + } + + if (action.SupportedVersions.Contains(configuredVersion)) + { + return configuredVersion; + } + + return action.SupportedVersions.Last(); //TODO: Ensure to get the latest version! + } + private static void AddAjaxCallParameters(StringBuilder script, ActionApiDescriptionModel action) { var httpMethod = action.HttpMethod?.ToUpperInvariant() ?? "POST"; diff --git a/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V1/CallDto.cs b/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V1/CallDto.cs new file mode 100644 index 0000000000..b8aa84f150 --- /dev/null +++ b/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V1/CallDto.cs @@ -0,0 +1,9 @@ +using Volo.Abp.Application.Dtos; + +namespace Volo.Abp.Identity.HttpApi.Host.VersioningTests.V1 +{ + public class CallDto : EntityDto + { + public string Number { get; set; } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V1/CallsController.cs b/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V1/CallsController.cs index 5837df2e2b..a61d3ca4a2 100644 --- a/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V1/CallsController.cs +++ b/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V1/CallsController.cs @@ -1,13 +1,12 @@ using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; -using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.AspNetCore.Mvc; namespace Volo.Abp.Identity.HttpApi.Host.VersioningTests.V1 { [ApiVersion("1.0")] - [Route("api/v{api-version:apiVersion}/calls")] + [Route("api/v{apiVersion:apiVersion}/calls")] public class CallsController : AbpController, IRemoteService { private static readonly List Calls = new List @@ -22,9 +21,4 @@ namespace Volo.Abp.Identity.HttpApi.Host.VersioningTests.V1 return Calls; } } - - public class CallDto : EntityDto - { - public string Number { get; set; } - } } diff --git a/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V2/Calls2Controller.cs b/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V2/Calls2Controller.cs index 4a7b908782..808d177c00 100644 --- a/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V2/Calls2Controller.cs +++ b/src/Volo.Abp.Identity.HttpApi.Host/VersioningTests/V2/Calls2Controller.cs @@ -8,7 +8,7 @@ using Volo.Abp.Identity.HttpApi.Host.VersioningTests.V1; namespace Volo.Abp.Identity.HttpApi.Host.VersioningTests.V2 { [ApiVersion("2.0")] - [Route("api/v{api-version:apiVersion}/calls")] + [Route("api/v{apiVersion:apiVersion}/calls")] public class CallsController : AbpController, IRemoteService { private static List _calls = new List diff --git a/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/FixtureController.cs b/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/FixtureController.cs index 552573e1d7..9321177b8a 100644 --- a/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/FixtureController.cs +++ b/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/FixtureController.cs @@ -8,7 +8,7 @@ namespace Volo.Abp.Identity //TODO: This is just a test controller and will be removed later [ApiVersion("3.0")] [ApiVersion("2.0", Deprecated = true)] - [Route("api/v{api-version:apiVersion}/identity/fixture")] + [Route("api/v{apiVersion:apiVersion}/identity/fixture")] public class FixtureController : AbpController, IRemoteService { private readonly IRequestedApiVersion _requestedApiVersion; diff --git a/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/V4/FixtureV4Controller.cs b/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/V4/FixtureV4Controller.cs index a5a3fa5655..dc271bdbba 100644 --- a/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/V4/FixtureV4Controller.cs +++ b/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/V4/FixtureV4Controller.cs @@ -7,7 +7,7 @@ namespace Volo.Abp.Identity.V4 { //TODO: This is just a test controller and will be removed later [ApiVersion("4.0")] - [Route("api/v{api-version:apiVersion}/identity/fixture")] + [Route("api/v{apiVersion:apiVersion}/identity/fixture")] public class FixtureController : AbpController, IRemoteService { private readonly IRequestedApiVersion _requestedApiVersion;