Skip to content

Commit

Permalink
Fixed pagination when endpoint uses custom PagedResult<T> type (jouke…
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-litvinov-work authored and joukevandermaas committed Oct 30, 2019
1 parent 01a91be commit e9cf163
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Saule/Queries/Pagination/PagedResultQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ private static bool IsPagedResult(object obj)
return false;
}

if (!obj.GetType().IsGenericType
|| obj.GetType().GetGenericTypeDefinition() != typeof(PagedResult<>))
// we need to check the whole hierarchy of classes
var type = obj.GetType();
while (type != null)
{
return false;
if (type.IsGenericType
&& type.GetGenericTypeDefinition() == typeof(PagedResult<>))
{
return true;
}

type = type.BaseType;
}

return true;
return false;
}

private static object UnwrapPagedResult(object obj)
Expand Down
13 changes: 13 additions & 0 deletions Tests/Controllers/CompaniesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ public PagedResult<Company> GetCompaniesWithPaging()
};
}

[HttpGet]
[Paginated(PerPage = 20, PageSizeLimit = 20)]
[Route("companies/paged-result-custom")]
[ReturnsResource(typeof(CompanyResource))]
public PagedResult<Company> GetCompaniesWithCustomPaging()
{
return new CustomPagedResult<Company>()
{
TotalResultsCount = 100,
Data = Get.Companies(100).ToList()
};
}

[HttpGet]
[Paginated(PerPage = 20, PageSizeLimit = 20, FirstPageNumber = 1)]
[Route("companies/paged-result-first-page")]
Expand Down
1 change: 1 addition & 0 deletions Tests/Integration/JsonApiMediaTypeFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ public async Task LimitsPageSizeIsNotSet()
}

[InlineData("api/companies/paged-result", 0)]
[InlineData("api/companies/paged-result-custom", 0)]
[InlineData("api/companies/paged-result-first-page", 1)]
[Theory(DisplayName = "Paged result calculates page counts")]
public async Task PagedResult(object baseUrl, int firstPageNumber)
Expand Down
9 changes: 9 additions & 0 deletions Tests/Models/CustomPagedResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Saule.Queries.Pagination;

namespace Tests.Models
{
public class CustomPagedResult<T>: PagedResult<T>
{
public int IgnoredValue { get; set; }
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
<Compile Include="Models\CompanyWithCustomersResource.cs" />
<Compile Include="Models\Customer.cs" />
<Compile Include="Models\CustomerResource.cs" />
<Compile Include="Models\CustomPagedResult.cs" />
<Compile Include="Models\GuidAsRelation.cs" />
<Compile Include="Models\GuidAsId.cs" />
<Compile Include="Helpers\ObsoleteSetupJsonApiServer.cs" />
Expand Down

0 comments on commit e9cf163

Please sign in to comment.