From ee403c37e9e9162dd100c3c4291bee545822985b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 26 Dec 2019 08:25:23 +0300 Subject: [PATCH] Resolved #2467: Add MaxMaxResultCount to LimitedResultRequestDto. --- .../Application/Dtos/ILimitedResultRequest.cs | 3 +- .../Dtos/LimitedResultRequestDto.cs | 24 +++++++++++++- .../Volo.Abp.Validation.Tests.csproj | 1 + .../ApplicationService_Validation_Tests.cs | 32 +++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ILimitedResultRequest.cs b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ILimitedResultRequest.cs index d0088fe155..36af42363c 100644 --- a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ILimitedResultRequest.cs +++ b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/ILimitedResultRequest.cs @@ -6,7 +6,8 @@ namespace Volo.Abp.Application.Dtos public interface ILimitedResultRequest { /// - /// Max expected result count. + /// Maximum result count should be returned. + /// This is generally used to limit result count on paging. /// int MaxResultCount { get; set; } } diff --git a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/LimitedResultRequestDto.cs b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/LimitedResultRequestDto.cs index 0931d090f3..2273f384b7 100644 --- a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/LimitedResultRequestDto.cs +++ b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Dtos/LimitedResultRequestDto.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace Volo.Abp.Application.Dtos @@ -7,11 +8,32 @@ namespace Volo.Abp.Application.Dtos /// Simply implements . /// [Serializable] - public class LimitedResultRequestDto : ILimitedResultRequest + public class LimitedResultRequestDto : ILimitedResultRequest, IValidatableObject { + /// + /// Default value: 10. + /// public static int DefaultMaxResultCount { get; set; } = 10; + /// + /// Maximum possible value of the . + /// Default value: 1,000. + /// + public static int MaxMaxResultCount { get; set; } = 1000; + + /// + /// Maximum result count should be returned. + /// This is generally used to limit result count on paging. + /// [Range(1, int.MaxValue)] public virtual int MaxResultCount { get; set; } = DefaultMaxResultCount; + + public virtual IEnumerable Validate(ValidationContext validationContext) + { + if (MaxResultCount > MaxMaxResultCount) + { + yield return new ValidationResult($"{nameof(MaxResultCount)} can not be more than {MaxMaxResultCount}! Increase {typeof(LimitedResultRequestDto).FullName}.{nameof(MaxMaxResultCount)} on the server side to allow more results.", new []{nameof(MaxResultCount)}); + } + } } } \ No newline at end of file diff --git a/framework/test/Volo.Abp.Validation.Tests/Volo.Abp.Validation.Tests.csproj b/framework/test/Volo.Abp.Validation.Tests/Volo.Abp.Validation.Tests.csproj index b86adccff8..ac14edcfb2 100644 --- a/framework/test/Volo.Abp.Validation.Tests/Volo.Abp.Validation.Tests.csproj +++ b/framework/test/Volo.Abp.Validation.Tests/Volo.Abp.Validation.Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/framework/test/Volo.Abp.Validation.Tests/Volo/Abp/Validation/ApplicationService_Validation_Tests.cs b/framework/test/Volo.Abp.Validation.Tests/Volo/Abp/Validation/ApplicationService_Validation_Tests.cs index 4035f2d371..2028ea60ca 100644 --- a/framework/test/Volo.Abp.Validation.Tests/Volo/Abp/Validation/ApplicationService_Validation_Tests.cs +++ b/framework/test/Volo.Abp.Validation.Tests/Volo/Abp/Validation/ApplicationService_Validation_Tests.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Shouldly; +using Volo.Abp.Application.Dtos; using Volo.Abp.Autofac; using Volo.Abp.DependencyInjection; using Volo.Abp.Modularity; @@ -135,6 +137,30 @@ namespace Volo.Abp.Validation }); } + //TODO: Create a Volo.Abp.Ddd.Application.Contracts.Tests project and move this to there and remove Volo.Abp.Ddd.Application.Contracts dependency from this project. + [Fact] + public async Task LimitedResultRequestDto_Should_Throw_Exception_For_Requests_More_Than_MaxMaxResultCount() + { + var exception = await Assert.ThrowsAsync(async () => + { + await _myAppService.MyMethodWithLimitedResult(new LimitedResultRequestDto + { + MaxResultCount = LimitedResultRequestDto.MaxMaxResultCount + 1 + }); + }); + + exception.ValidationErrors.ShouldContain(e => e.MemberNames.Contains(nameof(LimitedResultRequestDto.MaxResultCount))); + } + + [Fact] + public async Task LimitedResultRequestDto_Should_Be_Valid_For_Requests_Less_Than_MaxMaxResultCount() + { + await _myAppService.MyMethodWithLimitedResult(new LimitedResultRequestDto + { + MaxResultCount = LimitedResultRequestDto.MaxMaxResultCount -1 + }); + } + [Fact] public async Task Should_Stop_Recursive_Validation_In_A_Constant_Depth() { @@ -195,6 +221,7 @@ namespace Volo.Abp.Validation Task MyMethod6(MyMethod6Input input); Task MyMethod8(MyClassWithRecursiveReference input); Task MyMethodWithNullableEnum(MyEnum? value); + Task MyMethodWithLimitedResult(LimitedResultRequestDto input); } public class MyAppService : IMyAppService, ITransientDependency @@ -240,6 +267,11 @@ namespace Volo.Abp.Validation return Task.FromResult(new MyMethodOutput { Result = 42 }); } + public Task MyMethodWithLimitedResult(LimitedResultRequestDto input) + { + return Task.CompletedTask; + } + public Task MyMethodWithNullableEnum(MyEnum? value) { return Task.CompletedTask;