Skip to content

Commit 8626edc

Browse files
author
Bart Koelman
authored
Added MaximumPageSize and MaximumPageNumber to IJsonApiOptions (#681)
1 parent 2a68ce7 commit 8626edc

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

src/JsonApiDotNetCore/Configuration/IJsonApiOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public interface IJsonApiOptions : ILinksConfiguration, ISerializerOptions
1919
/// </example>
2020
bool IncludeTotalRecordCount { get; set; }
2121
int DefaultPageSize { get; }
22+
int? MaximumPageSize { get; }
23+
int? MaximumPageNumber { get; }
2224
bool ValidateModelState { get; }
2325
bool AllowClientGeneratedIds { get; }
2426
bool AllowCustomQueryParameters { get; set; }

src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ public class JsonApiOptions : IJsonApiOptions
7070
/// </example>
7171
public int DefaultPageSize { get; set; }
7272

73+
/// <summary>
74+
/// Optional. When set, limits the maximum page size for all resources.
75+
/// </summary>
76+
/// <example>
77+
/// <code>options.MaximumPageSize = 50;</code>
78+
/// </example>
79+
public int? MaximumPageSize { get; set; }
80+
81+
/// <summary>
82+
/// Optional. When set, limits the maximum page number for all resources.
83+
/// </summary>
84+
/// <example>
85+
/// <code>options.MaximumPageNumber = 100;</code>
86+
/// </example>
87+
public int? MaximumPageNumber { get; set; }
88+
7389
/// <summary>
7490
/// Whether or not the total-record count should be included in all document
7591
/// level meta objects.

src/JsonApiDotNetCore/QueryParameterServices/PageService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)
8383
{
8484
ThrowBadPagingRequest(queryParameter, "value needs to be greater than zero");
8585
}
86+
else if (size > _options.MaximumPageSize)
87+
{
88+
ThrowBadPagingRequest(queryParameter, $"page size cannot be higher than {_options.MaximumPageSize}.");
89+
}
8690
else
8791
{
8892
RequestedPageSize = size;
@@ -98,6 +102,10 @@ public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)
98102
{
99103
ThrowBadPagingRequest(queryParameter, "page index is not zero-based");
100104
}
105+
else if (number > _options.MaximumPageNumber)
106+
{
107+
ThrowBadPagingRequest(queryParameter, $"page index cannot be higher than {_options.MaximumPageNumber}.");
108+
}
101109
else
102110
{
103111
Backwards = (number < 0);

test/UnitTests/QueryParameters/PageServiceTests.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ namespace UnitTests.QueryParameters
99
{
1010
public class PageServiceTests : QueryParametersUnitTestCollection
1111
{
12-
public PageService GetService()
12+
public PageService GetService(int? maximumPageSize = null, int? maximumPageNumber = null)
1313
{
14-
return new PageService(new JsonApiOptions());
14+
return new PageService(new JsonApiOptions
15+
{
16+
MaximumPageSize = maximumPageSize,
17+
MaximumPageNumber = maximumPageNumber
18+
});
1519
}
1620

1721
[Fact]
@@ -28,14 +32,16 @@ public void Name_PageService_IsCorrect()
2832
}
2933

3034
[Theory]
31-
[InlineData("1", 1, false)]
32-
[InlineData("abcde", 0, true)]
33-
[InlineData("", 0, true)]
34-
public void Parse_PageSize_CanParse(string value, int expectedValue, bool shouldThrow)
35+
[InlineData("1", 1, null, false)]
36+
[InlineData("abcde", 0, null, true)]
37+
[InlineData("", 0, null, true)]
38+
[InlineData("5", 5, 10, false)]
39+
[InlineData("5", 5, 3, true)]
40+
public void Parse_PageSize_CanParse(string value, int expectedValue, int? maximumPageSize, bool shouldThrow)
3541
{
3642
// Arrange
3743
var query = new KeyValuePair<string, StringValues>($"page[size]", new StringValues(value));
38-
var service = GetService();
44+
var service = GetService(maximumPageSize: maximumPageSize);
3945

4046
// Act
4147
if (shouldThrow)
@@ -51,15 +57,16 @@ public void Parse_PageSize_CanParse(string value, int expectedValue, bool should
5157
}
5258

5359
[Theory]
54-
[InlineData("1", 1, false)]
55-
[InlineData("abcde", 0, true)]
56-
[InlineData("", 0, true)]
57-
public void Parse_PageNumber_CanParse(string value, int expectedValue, bool shouldThrow)
60+
[InlineData("1", 1, null, false)]
61+
[InlineData("abcde", 0, null, true)]
62+
[InlineData("", 0, null, true)]
63+
[InlineData("5", 5, 10, false)]
64+
[InlineData("5", 5, 3, true)]
65+
public void Parse_PageNumber_CanParse(string value, int expectedValue, int? maximumPageNumber, bool shouldThrow)
5866
{
5967
// Arrange
6068
var query = new KeyValuePair<string, StringValues>($"page[number]", new StringValues(value));
61-
var service = GetService();
62-
69+
var service = GetService(maximumPageNumber: maximumPageNumber);
6370

6471
// Act
6572
if (shouldThrow)

0 commit comments

Comments
 (0)