Skip to content

Added MaximumPageSize and MaximumPageNumber to IJsonApiOptions #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/JsonApiDotNetCore/Configuration/IJsonApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface IJsonApiOptions : ILinksConfiguration, ISerializerOptions
/// </example>
bool IncludeTotalRecordCount { get; set; }
int DefaultPageSize { get; }
int? MaximumPageSize { get; }
int? MaximumPageNumber { get; }
bool ValidateModelState { get; }
bool AllowClientGeneratedIds { get; }
bool AllowCustomQueryParameters { get; set; }
Expand Down
16 changes: 16 additions & 0 deletions src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ public class JsonApiOptions : IJsonApiOptions
/// </example>
public int DefaultPageSize { get; set; }

/// <summary>
/// Optional. When set, limits the maximum page size for all resources.
/// </summary>
/// <example>
/// <code>options.MaximumPageSize = 50;</code>
/// </example>
public int? MaximumPageSize { get; set; }

/// <summary>
/// Optional. When set, limits the maximum page number for all resources.
/// </summary>
/// <example>
/// <code>options.MaximumPageNumber = 100;</code>
/// </example>
public int? MaximumPageNumber { get; set; }

/// <summary>
/// Whether or not the total-record count should be included in all document
/// level meta objects.
Expand Down
8 changes: 8 additions & 0 deletions src/JsonApiDotNetCore/QueryParameterServices/PageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)
{
ThrowBadPagingRequest(queryParameter, "value needs to be greater than zero");
}
else if (size > _options.MaximumPageSize)
{
ThrowBadPagingRequest(queryParameter, $"page size cannot be higher than {_options.MaximumPageSize}.");
}
else
{
RequestedPageSize = size;
Expand All @@ -98,6 +102,10 @@ public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)
{
ThrowBadPagingRequest(queryParameter, "page index is not zero-based");
}
else if (number > _options.MaximumPageNumber)
{
ThrowBadPagingRequest(queryParameter, $"page index cannot be higher than {_options.MaximumPageNumber}.");
}
else
{
Backwards = (number < 0);
Expand Down
33 changes: 20 additions & 13 deletions test/UnitTests/QueryParameters/PageServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ namespace UnitTests.QueryParameters
{
public class PageServiceTests : QueryParametersUnitTestCollection
{
public PageService GetService()
public PageService GetService(int? maximumPageSize = null, int? maximumPageNumber = null)
{
return new PageService(new JsonApiOptions());
return new PageService(new JsonApiOptions
{
MaximumPageSize = maximumPageSize,
MaximumPageNumber = maximumPageNumber
});
}

[Fact]
Expand All @@ -28,14 +32,16 @@ public void Name_PageService_IsCorrect()
}

[Theory]
[InlineData("1", 1, false)]
[InlineData("abcde", 0, true)]
[InlineData("", 0, true)]
public void Parse_PageSize_CanParse(string value, int expectedValue, bool shouldThrow)
[InlineData("1", 1, null, false)]
[InlineData("abcde", 0, null, true)]
[InlineData("", 0, null, true)]
[InlineData("5", 5, 10, false)]
[InlineData("5", 5, 3, true)]
public void Parse_PageSize_CanParse(string value, int expectedValue, int? maximumPageSize, bool shouldThrow)
{
// Arrange
var query = new KeyValuePair<string, StringValues>($"page[size]", new StringValues(value));
var service = GetService();
var service = GetService(maximumPageSize: maximumPageSize);

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

[Theory]
[InlineData("1", 1, false)]
[InlineData("abcde", 0, true)]
[InlineData("", 0, true)]
public void Parse_PageNumber_CanParse(string value, int expectedValue, bool shouldThrow)
[InlineData("1", 1, null, false)]
[InlineData("abcde", 0, null, true)]
[InlineData("", 0, null, true)]
[InlineData("5", 5, 10, false)]
[InlineData("5", 5, 3, true)]
public void Parse_PageNumber_CanParse(string value, int expectedValue, int? maximumPageNumber, bool shouldThrow)
{
// Arrange
var query = new KeyValuePair<string, StringValues>($"page[number]", new StringValues(value));
var service = GetService();

var service = GetService(maximumPageNumber: maximumPageNumber);

// Act
if (shouldThrow)
Expand Down