From 3c54e7d41cee58eab23251e991e11f7eb73900b8 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sun, 28 Nov 2021 12:09:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(wrapper):=20=E6=8F=90=E4=BE=9B=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=E7=89=B9=E5=AE=9A=E6=8E=A5=E5=8F=A3=E5=A4=84=E7=90=86?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LINGYUN/Abp/Wrapper/AbpWrapperOptions.cs | 9 ++++-- .../LINGYUN/Abp/Wrapper/IWrapDisabled.cs | 6 ++++ .../AbpDaprActorsAspNetCoreModule.cs | 2 +- .../Mvc/Wrapper/WrapResultChecker.cs | 12 ++++++++ .../Mvc/AbpAspNetCoreMvcTestModule.cs | 2 ++ .../DontWrapResultBaseTypeController.cs | 29 +++++++++++++++++++ .../Mvc/Results/DontWrapResultController.cs | 2 +- .../Mvc/Results/WrapResultController_Tests.cs | 10 ++++++- 8 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IWrapDisabled.cs create mode 100644 aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/DontWrapResultBaseTypeController.cs diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperOptions.cs b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperOptions.cs index 8d674bceb..9382bf43e 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperOptions.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperOptions.cs @@ -61,9 +61,9 @@ namespace LINGYUN.Abp.Wrapper /// public ITypeList IgnoreExceptions { get; } /// - /// 忽略服务类型 + /// 忽略接口类型 /// - public ITypeList IgnoreBaseTypes { get; } + public ITypeList IgnoredInterfaces { get; } internal IDictionary ExceptionHandles { get; } @@ -79,7 +79,10 @@ namespace LINGYUN.Abp.Wrapper IgnoreControllers = new TypeList(); IgnoreReturnTypes = new TypeList(); - IgnoreBaseTypes = new TypeList(); + IgnoredInterfaces = new TypeList() + { + typeof(IWrapDisabled) + }; IgnoreExceptions = new TypeList(); CodeWithEmptyResult = (_) => "404"; diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IWrapDisabled.cs b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IWrapDisabled.cs new file mode 100644 index 000000000..125a04246 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IWrapDisabled.cs @@ -0,0 +1,6 @@ +namespace LINGYUN.Abp.Wrapper +{ + public interface IWrapDisabled + { + } +} diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Actors.AspNetCore/LINGYUN/Abp/Dapr/Actors/AspNetCore/AbpDaprActorsAspNetCoreModule.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Actors.AspNetCore/LINGYUN/Abp/Dapr/Actors/AspNetCore/AbpDaprActorsAspNetCoreModule.cs index 771e6becc..427a1c056 100644 --- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Actors.AspNetCore/LINGYUN/Abp/Dapr/Actors/AspNetCore/AbpDaprActorsAspNetCoreModule.cs +++ b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Actors.AspNetCore/LINGYUN/Abp/Dapr/Actors/AspNetCore/AbpDaprActorsAspNetCoreModule.cs @@ -33,7 +33,7 @@ namespace LINGYUN.Abp.Dapr.Actors.AspNetCore Configure(options => { - options.IgnoreBaseTypes.TryAdd(); + options.IgnoredInterfaces.TryAdd(); }); } diff --git a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs index 9de0b99fc..950aabe49 100644 --- a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs +++ b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; using System; using System.Linq; +using System.Reflection; using Volo.Abp.DependencyInjection; using Volo.Abp.Threading; @@ -70,6 +71,11 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper return false; } + if (!CheckForInterfaces(descriptor)) + { + return false; + } + if (!CheckForMethod(descriptor)) { return false; @@ -123,6 +129,12 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper controllerActionDescriptor.ControllerTypeInfo.Namespace.StartsWith(nsp)); } + protected virtual bool CheckForInterfaces(ControllerActionDescriptor controllerActionDescriptor) + { + return !Options.IgnoredInterfaces.Any(type => + type.IsAssignableFrom(controllerActionDescriptor.ControllerTypeInfo)); + } + protected virtual bool CheckForReturnType(ControllerActionDescriptor controllerActionDescriptor) { var returnType = AsyncHelper.UnwrapTask(controllerActionDescriptor.MethodInfo.ReturnType); diff --git a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs index ba8d98a4f..1b9af6e0d 100644 --- a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs +++ b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs @@ -80,10 +80,12 @@ namespace LINGYUN.Abp.AspNetCore.Mvc options.IgnoreExceptions.Clear(); options.IgnoreNamespaces.Clear(); options.IgnorePrefixUrls.Clear(); + options.IgnoredInterfaces.Clear(); options.IgnoreReturnTypes.Clear(); // api/abp/api-definition options.IgnoreControllers.Add(); + options.IgnoredInterfaces.Add(); // api/abp/application-configuration options.IgnoreReturnTypes.Add(); options.IgnoreReturnTypes.Add(); diff --git a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/DontWrapResultBaseTypeController.cs b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/DontWrapResultBaseTypeController.cs new file mode 100644 index 000000000..be40ca0f9 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/DontWrapResultBaseTypeController.cs @@ -0,0 +1,29 @@ +using LINGYUN.Abp.Wrapper; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using Volo.Abp.AspNetCore.Mvc; + +namespace LINGYUN.Abp.AspNetCore.Mvc.Results +{ + [Route("api/dont-base-type/wrap-result-test")] + public class DontWrapResultBaseTypeController : AbpController, IWrapDisabled + { + [HttpGet] + public TestResultObject Wrap() + { + return new TestResultObject + { + Id = Guid.NewGuid(), + DateTime = Clock.Now, + Double = 3.141592653d, + Integer = 100, + Name = "Not Wrap For Base Type", + Properties = new Dictionary + { + { "TestKey", "TestValue" } + } + }; + } + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/DontWrapResultController.cs b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/DontWrapResultController.cs index 7169af056..e870c15ff 100644 --- a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/DontWrapResultController.cs +++ b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/DontWrapResultController.cs @@ -17,7 +17,7 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Results DateTime = Clock.Now, Double = 3.141592653d, Integer = 100, - Name = "Not Wrap", + Name = "Not Wrap For Url Prefix", Properties = new Dictionary { { "TestKey", "TestValue" } diff --git a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs index 0ddf305ca..63b37f1dd 100644 --- a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs +++ b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs @@ -39,7 +39,15 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Results { var result = await GetResponseAsObjectAsync("/api/dont/wrap-result-test"); result.ShouldNotBeNull(); - result.Name.ShouldBe("Not Wrap"); + result.Name.ShouldBe("Not Wrap For Url Prefix"); + } + + [Fact] + public async Task Should_Return_Not_Wrap_Result_For_Interfaces_With_Dont_Wrapper() + { + var result = await GetResponseAsObjectAsync("/api/dont-base-type/wrap-result-test"); + result.ShouldNotBeNull(); + result.Name.ShouldBe("Not Wrap For Base Type"); } [Fact]