Skip to content

Commit 3697967

Browse files
author
Bart Koelman
committed
Fixed: allow unpaged result when no maximum page size is set
Fixed: having default page size of 0 is dangerous (returns complete tables)
1 parent 674668e commit 3697967

File tree

6 files changed

+14
-7
lines changed

6 files changed

+14
-7
lines changed

src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public override void ConfigureServices(IServiceCollection services)
2525
options.IncludeTotalRecordCount = true;
2626
options.LoadDatabaseValues = true;
2727
options.AllowClientGeneratedIds = true;
28+
options.DefaultPageSize = 0;
2829
},
2930
discovery => discovery.AddAssembly(Assembly.Load(nameof(JsonApiDotNetCoreExample))),
3031
mvcBuilder: mvcBuilder);

src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public class JsonApiOptions : IJsonApiOptions
5353
public string Namespace { get; set; }
5454

5555
/// <summary>
56-
/// The default page size for all resources
56+
/// The default page size for all resources. The value zero means: no paging.
5757
/// </summary>
5858
/// <example>
5959
/// <code>options.DefaultPageSize = 10;</code>
6060
/// </example>
61-
public int DefaultPageSize { get; set; }
61+
public int DefaultPageSize { get; set; } = 10;
6262

6363
/// <summary>
6464
/// Optional. When set, limits the maximum page size for all resources.

src/JsonApiDotNetCore/QueryParameterServices/PageService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ public virtual void Parse(string parameterName, StringValues parameterValue)
105105
private int ParsePageSize(string parameterValue, int? maxValue)
106106
{
107107
bool success = int.TryParse(parameterValue, out int number);
108-
if (success && number >= 1)
108+
int minValue = maxValue != null ? 1 : 0;
109+
110+
if (success && number >= minValue)
109111
{
110112
if (maxValue == null || number <= maxValue)
111113
{
@@ -115,7 +117,7 @@ private int ParsePageSize(string parameterValue, int? maxValue)
115117

116118
var message = maxValue == null
117119
? $"Value '{parameterValue}' is invalid, because it must be a whole number that is greater than zero."
118-
: $"Value '{parameterValue}' is invalid, because it must be a whole number that is greater than zero and not higher than {maxValue}.";
120+
: $"Value '{parameterValue}' is invalid, because it must be a whole number that is zero or greater and not higher than {maxValue}.";
119121

120122
throw new InvalidQueryStringParameterException("page[size]",
121123
"The specified value is not in the range of valid values.", message);

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ public async Task GetResources_NoDefaultPageSize_ReturnsResources()
104104
{
105105
// Arrange
106106
var context = _fixture.GetService<AppDbContext>();
107+
context.TodoItems.RemoveRange(context.TodoItems);
108+
await context.SaveChangesAsync();
109+
107110
var todoItems = _todoItemFaker.Generate(20).ToList();
108111
context.TodoItems.AddRange(todoItems);
109112
await context.SaveChangesAsync();
@@ -122,7 +125,7 @@ public async Task GetResources_NoDefaultPageSize_ReturnsResources()
122125
var result = _fixture.GetDeserializer().DeserializeList<TodoItem>(body);
123126

124127
// Assert
125-
Assert.True(result.Data.Count >= 20);
128+
Assert.True(result.Data.Count == 20);
126129
}
127130

128131
[Fact]

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingDataTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using Microsoft.EntityFrameworkCore;
1717
using Microsoft.Extensions.Logging;
1818
using Newtonsoft.Json;
19-
using NLog.Extensions.Logging;
2019
using Xunit;
2120
using Person = JsonApiDotNetCoreExample.Models.Person;
2221

test/UnitTests/QueryParameters/PageServiceTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public void CanParse_PageService_FailOnMismatch()
4646
}
4747

4848
[Theory]
49+
[InlineData("0", 0, null, false)]
50+
[InlineData("0", 0, 50, true)]
4951
[InlineData("1", 1, null, false)]
5052
[InlineData("abcde", 0, null, true)]
5153
[InlineData("", 0, null, true)]
@@ -65,7 +67,7 @@ public void Parse_PageSize_CanParse(string value, int expectedValue, int? maximu
6567
Assert.Equal("page[size]", exception.QueryParameterName);
6668
Assert.Equal(HttpStatusCode.BadRequest, exception.Error.StatusCode);
6769
Assert.Equal("The specified value is not in the range of valid values.", exception.Error.Title);
68-
Assert.StartsWith($"Value '{value}' is invalid, because it must be a whole number that is greater than zero", exception.Error.Detail);
70+
Assert.StartsWith($"Value '{value}' is invalid, because it must be a whole number that is", exception.Error.Detail);
6971
Assert.Equal("page[size]", exception.Error.Source.Parameter);
7072
}
7173
else

0 commit comments

Comments
 (0)