From f531bb08ba213059387522300efe889048da8144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Osman=20Ko=C3=A7?= Date: Mon, 25 Mar 2024 17:24:13 +0300 Subject: [PATCH] fix: Carbon.PagedList validation error (#127) * fix: Carbon.PagedList validation error --- .../Carbon.Domain.EntityFrameworkCore.csproj | 5 ++- ...arbon.PagedList.EntityFrameworkCore.csproj | 6 +++- .../PagedListExtensions.cs | 20 ++++++----- .../Carbon.PagedList.Mapster.csproj | 4 ++- Carbon.PagedList/BasePagedList.cs | 9 ++--- Carbon.PagedList/Carbon.PagedList.csproj | 6 +++- Carbon.PagedList/PagedList.cs | 18 ++++------ ....WebApplication.EntityFrameworkCore.csproj | 5 ++- .../Carbon.WebApplication.csproj | 33 ++----------------- .../QueryableExtensionsDataShare.cs | 13 -------- .../PagedListExtensionsTest.cs | 14 -------- .../BasePagedListTests.cs | 14 -------- .../PagedListTests.cs | 16 --------- .../BaseDtoValidatorTests.cs | 8 ----- 14 files changed, 44 insertions(+), 127 deletions(-) diff --git a/Carbon.Domain.EntityFrameworkCore.Extensions/Carbon.Domain.EntityFrameworkCore.csproj b/Carbon.Domain.EntityFrameworkCore.Extensions/Carbon.Domain.EntityFrameworkCore.csproj index 590ba7ac..7d031842 100644 --- a/Carbon.Domain.EntityFrameworkCore.Extensions/Carbon.Domain.EntityFrameworkCore.csproj +++ b/Carbon.Domain.EntityFrameworkCore.Extensions/Carbon.Domain.EntityFrameworkCore.csproj @@ -2,8 +2,11 @@ net6.0;net5.0;netstandard2.1 - 3.1.4 + 3.1.5 + - 3.1.5 + * Upgrade Carbon.PagedList (Remove pageSize and pageNumber validation check) + - 3.1.3 * Added new extension method to EFExtensions (ToPagedListEntityFilteredWithSolution, ToPagedListEntityAsync, WhereContains) diff --git a/Carbon.PagedList.EntityFrameworkCore/Carbon.PagedList.EntityFrameworkCore.csproj b/Carbon.PagedList.EntityFrameworkCore/Carbon.PagedList.EntityFrameworkCore.csproj index 6e9959ba..29e6a709 100644 --- a/Carbon.PagedList.EntityFrameworkCore/Carbon.PagedList.EntityFrameworkCore.csproj +++ b/Carbon.PagedList.EntityFrameworkCore/Carbon.PagedList.EntityFrameworkCore.csproj @@ -2,7 +2,11 @@ netstandard2.1 - 1.1.2 + 1.1.3 + + 1.1.3 + - Upgrade Carbon.PagedList (Remove pageSize and pageNumber validation check) + True diff --git a/Carbon.PagedList.EntityFrameworkCore/PagedListExtensions.cs b/Carbon.PagedList.EntityFrameworkCore/PagedListExtensions.cs index 5b7304a3..2f0812b0 100644 --- a/Carbon.PagedList.EntityFrameworkCore/PagedListExtensions.cs +++ b/Carbon.PagedList.EntityFrameworkCore/PagedListExtensions.cs @@ -23,20 +23,22 @@ public static class PagedListExtensions /// public static async Task> ToPagedListAsync(this IQueryable superset, int pageNumber, int pageSize) { - if (pageNumber < 1) - throw new ArgumentOutOfRangeException(nameof(pageNumber), pageNumber, "PageNumber cannot be below 1."); - if (pageSize < 1) - throw new ArgumentOutOfRangeException(nameof(pageSize), pageSize, "PageSize cannot be less than 1."); - + var isAllDataRequest = pageSize == 0 && pageNumber == 1; + var totalItemCount = superset?.Count() ?? 0; var result = new List(); - + if (superset != null && totalItemCount > 0) { - if (pageNumber == 1) - result = await superset.Skip(0).Take(pageSize).ToListAsync(); + if (isAllDataRequest) + { + result = await superset.ToListAsync(); + } else - result = await superset.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync(); + { + var skipCount = pageNumber == 1 ? 0 : (pageNumber - 1) * pageSize; + result = await superset.Skip(skipCount).Take(pageSize).ToListAsync(); + } } return new PagedList(result, pageNumber, pageSize, totalItemCount); diff --git a/Carbon.PagedList.Mapster/Carbon.PagedList.Mapster.csproj b/Carbon.PagedList.Mapster/Carbon.PagedList.Mapster.csproj index 54c6c1f3..bb5b3e0c 100644 --- a/Carbon.PagedList.Mapster/Carbon.PagedList.Mapster.csproj +++ b/Carbon.PagedList.Mapster/Carbon.PagedList.Mapster.csproj @@ -1,9 +1,11 @@  netstandard2.1 - 1.0.10 + 1.0.11 True + 1.0.11 + - Upgrade Carbon.PagedList (Remove pageSize and pageNumber validation check) 1.0.10 - Carbon.Common updated and IQueryable OrderBy extension method bug fixed diff --git a/Carbon.PagedList/BasePagedList.cs b/Carbon.PagedList/BasePagedList.cs index 9d7b1856..64d8cb07 100644 --- a/Carbon.PagedList/BasePagedList.cs +++ b/Carbon.PagedList/BasePagedList.cs @@ -36,10 +36,7 @@ protected internal BasePagedList() /// The size of the superset. protected internal BasePagedList(int pageNumber, int pageSize, int totalItemCount) { - if (pageNumber < 1) - throw new ArgumentOutOfRangeException(nameof(pageNumber), pageNumber, "PageNumber cannot be below 1."); - if (pageSize < 1) - throw new ArgumentOutOfRangeException(nameof(pageSize), pageSize, "PageSize cannot be less than 1."); + var isAllDataRequest = pageSize == 0 && pageNumber == 1; // set source to blank list if superset is null to prevent exceptions TotalItemCount = totalItemCount; @@ -50,8 +47,8 @@ protected internal BasePagedList(int pageNumber, int pageSize, int totalItemCoun : 0; HasPreviousPage = PageNumber > 1; HasNextPage = PageNumber < PageCount; - IsFirstPage = PageNumber == 1; - IsLastPage = PageNumber >= PageCount; + IsFirstPage = isAllDataRequest || PageNumber == 1; + IsLastPage = isAllDataRequest || PageNumber >= PageCount; FirstItemOnPage = (PageNumber - 1) * PageSize + 1; var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1; LastItemOnPage = numberOfLastItemOnPage > TotalItemCount diff --git a/Carbon.PagedList/Carbon.PagedList.csproj b/Carbon.PagedList/Carbon.PagedList.csproj index bb301c0a..dadbb510 100644 --- a/Carbon.PagedList/Carbon.PagedList.csproj +++ b/Carbon.PagedList/Carbon.PagedList.csproj @@ -2,7 +2,11 @@ netstandard2.1 - 1.1.2 + 1.1.3 + + 1.1.3 + - Remove pageSize and pageNumber validation check + True diff --git a/Carbon.PagedList/PagedList.cs b/Carbon.PagedList/PagedList.cs index 575bfdc4..c85b5134 100644 --- a/Carbon.PagedList/PagedList.cs +++ b/Carbon.PagedList/PagedList.cs @@ -35,10 +35,7 @@ public class PagedList : PagedListMetaData, IPagedList /// The specified page size cannot be less than one. public PagedList(IEnumerable subset, int pageNumber, int pageSize, int totalCount) { - if (pageNumber < 1) - throw new ArgumentOutOfRangeException(nameof(pageNumber), pageNumber, "PageNumber cannot be below 1."); - if (pageSize < 1) - throw new ArgumentOutOfRangeException(nameof(pageSize), pageSize, "PageSize cannot be less than 1."); + var isAllDataRequest = pageSize == 0 && pageNumber == 1; // set source to blank list if superset is null to prevent exceptions TotalItemCount = totalCount; @@ -49,8 +46,8 @@ public PagedList(IEnumerable subset, int pageNumber, int pageSize, int totalC : 0; HasPreviousPage = PageNumber > 1; HasNextPage = PageNumber < PageCount; - IsFirstPage = PageNumber == 1; - IsLastPage = PageNumber >= PageCount; + IsFirstPage = isAllDataRequest || PageNumber == 1; + IsLastPage = isAllDataRequest || PageNumber >= PageCount; FirstItemOnPage = (PageNumber - 1) * PageSize + 1; var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1; LastItemOnPage = numberOfLastItemOnPage > TotalItemCount @@ -72,10 +69,7 @@ public PagedList(IEnumerable subset, int pageNumber, int pageSize, int totalC /// The specified page size cannot be less than one. public PagedList(IQueryable superset, int pageNumber, int pageSize) { - if (pageNumber < 1) - throw new ArgumentOutOfRangeException(nameof(pageNumber), pageNumber, "PageNumber cannot be below 1."); - if (pageSize < 1) - throw new ArgumentOutOfRangeException(nameof(pageSize), pageSize, "PageSize cannot be less than 1."); + var isAllDataRequest = pageSize == 0 && pageNumber == 1; // set source to blank list if superset is null to prevent exceptions TotalItemCount = superset?.Count() ?? 0; @@ -86,8 +80,8 @@ public PagedList(IQueryable superset, int pageNumber, int pageSize) : 0; HasPreviousPage = PageNumber > 1; HasNextPage = PageNumber < PageCount; - IsFirstPage = PageNumber == 1; - IsLastPage = PageNumber >= PageCount; + IsFirstPage = isAllDataRequest || PageNumber == 1; + IsLastPage = isAllDataRequest || PageNumber >= PageCount; FirstItemOnPage = (PageNumber - 1) * PageSize + 1; var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1; LastItemOnPage = numberOfLastItemOnPage > TotalItemCount diff --git a/Carbon.WebApplication.EntityFrameworkCore/Carbon.WebApplication.EntityFrameworkCore.csproj b/Carbon.WebApplication.EntityFrameworkCore/Carbon.WebApplication.EntityFrameworkCore.csproj index e3695e1f..a256c044 100644 --- a/Carbon.WebApplication.EntityFrameworkCore/Carbon.WebApplication.EntityFrameworkCore.csproj +++ b/Carbon.WebApplication.EntityFrameworkCore/Carbon.WebApplication.EntityFrameworkCore.csproj @@ -2,8 +2,11 @@ net6.0;net5.0;netstandard2.1 - 2.3.3 + 2.3.4 + 2.3.4 + Upgrade Carbon.PagedList (Remove pageSize and pageNumber validation check) + 2.3.3 Upgrade Carbon.Domain.EntityFramework version (Added OwnerType.None in ownership filter) diff --git a/Carbon.WebApplication/Carbon.WebApplication.csproj b/Carbon.WebApplication/Carbon.WebApplication.csproj index 088138d7..5d6b286c 100644 --- a/Carbon.WebApplication/Carbon.WebApplication.csproj +++ b/Carbon.WebApplication/Carbon.WebApplication.csproj @@ -1,8 +1,10 @@  net6.0;net5.0;netcoreapp3.1 - 4.5.2 + 4.5.3 + 4.5.3 + - Upgrade Carbon.PagedList (Remove pageSize and pageNumber validation check) 4.5.2 - Updated Carbon.Common nuget package (Added StringExtensions class with ReplaceTurkishChars and ContainsTurkishIgnoreCase methods) 4.5.0 @@ -34,7 +36,6 @@ - Cors Wildcards accepted 4.1.1 - CarbonException messages are take arguments for dynamic exception messages - 4.1.0 - Critical bug fixed for Directory separator char to specific platform (Windows,Linux) 4.0.0 @@ -43,34 +44,6 @@ - Swagger Upgraded - BodyRewind deprecated, thus RoleFilteredDto is now unsupported as it is unnecessary just after repository-level validation introduced - Some Performance Improvements - - 3.7.0 - - Swagger server definition added - 3.6.0 - - ConfigureEndpoint is now a virtual class rather than an abstract - - HealthCheck partial healthy (degraded) also causes endpoint to generate 5xx http status code - 3.5.0 - - ConfigureEndpoint support added when needed - 3.4.5 - - Bearer Authenticator can now be passed some events - 3.4.4 - - Swager Header tenantId added if not exist - 3.4.0 - - GetUserName extension method added - 3.3.2 - -Swagger Header(P360SolutonId) Added - 3.3.1 - - Swagger RoutePrefix added - 3.3.0 - - Cors ExposeHeaders added - 3.2.0 - - Fluent Validation crash on dotnet 5 applications fixed - - CarbonValidator class moved here from ExceptionHandling Package - - Fluent Validation upgraded - 3.1.0 - - Tenant Ownership new features and performance improvements - 3.0.0 - - Added new error handling mechanism false True diff --git a/tests/Carbon.PagedList.EntityFrameworkCore.UnitTests/DataShares/QueryableExtensionsDataShare.cs b/tests/Carbon.PagedList.EntityFrameworkCore.UnitTests/DataShares/QueryableExtensionsDataShare.cs index 7129b2d5..7e941e31 100644 --- a/tests/Carbon.PagedList.EntityFrameworkCore.UnitTests/DataShares/QueryableExtensionsDataShare.cs +++ b/tests/Carbon.PagedList.EntityFrameworkCore.UnitTests/DataShares/QueryableExtensionsDataShare.cs @@ -18,17 +18,4 @@ public override IEnumerable GetData(MethodInfo testMethod) yield return new object[] { null, 10, 2 }; } } - - public class InValidToPagedListQueryableExtensions : DataAttribute - { - public override IEnumerable GetData(MethodInfo testMethod) - { - var context = EFRepositoryFixture.CreateContext(); - EFRepositoryFixture.CreateData(context, Guid.NewGuid(), Guid.NewGuid()); - - //data, pageNumber, pageSize - yield return new object[] { context.CarbonContextTestClass, -1, -1 }; - yield return new object[] { null, -10, -2 }; - } - } } \ No newline at end of file diff --git a/tests/Carbon.PagedList.EntityFrameworkCore.UnitTests/PagedListExtensionsTest.cs b/tests/Carbon.PagedList.EntityFrameworkCore.UnitTests/PagedListExtensionsTest.cs index 41630fd1..e8210cd6 100644 --- a/tests/Carbon.PagedList.EntityFrameworkCore.UnitTests/PagedListExtensionsTest.cs +++ b/tests/Carbon.PagedList.EntityFrameworkCore.UnitTests/PagedListExtensionsTest.cs @@ -32,19 +32,5 @@ public void ToPagedListAsync_Successfully_PagedListExtensions(IQueryable entity, int pageNumber, int pageSize) - { - // Arrange - // Act - var wrapper = new QueryableExtensionsWrapper(); - var response = wrapper.ToPagedListAsync(entity, pageNumber, pageSize); - // Assert - Assert.NotNull(response.Exception); - - _testOutputHelper.WriteLine("Test passed!"); - } } } \ No newline at end of file diff --git a/tests/Carbon.PagedList.UnitTests/BasePagedListTests.cs b/tests/Carbon.PagedList.UnitTests/BasePagedListTests.cs index 7cea808b..54902dca 100644 --- a/tests/Carbon.PagedList.UnitTests/BasePagedListTests.cs +++ b/tests/Carbon.PagedList.UnitTests/BasePagedListTests.cs @@ -31,20 +31,6 @@ public void CreateBasePagedList_CreateWithArguments_ReturnBagePagedList() Assert.IsAssignableFrom>(x); } - [Fact] - public void CreateBasePagedList_InvalidPageNumber_ThrowArgumentException() - { - // Act & Assert - Assert.Throws(() => new TestBasePagedList(0, 1, 1)); - } - - [Fact] - public void CreateBasePagedList_InvalidPageSize_ThrowArgumentException() - { - // Act & Assert - Assert.Throws(() => new TestBasePagedList(4, 0, 1)); - } - [Fact] public void GetEnumerator_GetSuccessfully_ReturnEnumerator() { diff --git a/tests/Carbon.PagedList.UnitTests/PagedListTests.cs b/tests/Carbon.PagedList.UnitTests/PagedListTests.cs index 8aac43d3..c19f6c8f 100644 --- a/tests/Carbon.PagedList.UnitTests/PagedListTests.cs +++ b/tests/Carbon.PagedList.UnitTests/PagedListTests.cs @@ -1,7 +1,6 @@ using Xunit; using System.Collections.Generic; using System.Linq; -using System; namespace Carbon.PagedList.UnitTests { @@ -42,21 +41,6 @@ public void CreatePagedList_CreateWithQueryable_ReturnPagedList() Assert.IsType>(x); } - [Fact] - public void CreatePagedList_InvalidPageSize_ThrowException() - { - // Act & Assert - Assert.Throws(() => new PagedList(dataList.AsQueryable(), 1, 0)); - - } - - [Fact] - public void CreatePagedList_InvalidPageNumber_ThrowException() - { - // Act & Assert - Assert.Throws(() => new PagedList(dataList.AsQueryable(), 0, 5)); - } - [Fact] public void GetEnumerator_GetSuccessfully_ReturnEnumerator() { diff --git a/tests/Carbon.WebApplication.UnitTests/BaseDtoValidatorTests.cs b/tests/Carbon.WebApplication.UnitTests/BaseDtoValidatorTests.cs index c544faa0..cc1ac7c9 100644 --- a/tests/Carbon.WebApplication.UnitTests/BaseDtoValidatorTests.cs +++ b/tests/Carbon.WebApplication.UnitTests/BaseDtoValidatorTests.cs @@ -2,7 +2,6 @@ using Carbon.PagedList; using FluentValidation; using Moq; -using System; using Xunit; namespace Carbon.WebApplication.UnitTests @@ -37,12 +36,5 @@ public void PageableDto_ValidPageNumberAndPageIndex() // Assert Assert.IsAssignableFrom>(x); } - [Fact] - public void PageableDto_InvalidPageNumberAndPageIndex_ThrowException() - { - // Act - // Assert - Assert.Throws(() => Mock.Of>().ToPagedList(0, 300)); - } } } \ No newline at end of file