Skip to content

Commit 5733fe5

Browse files
authored
Fix/pagesize (#601)
* feat: new startup for test * chore: rename startup class * test: reproduce #599 in test * fix: zero division error * chore: remove redundant DefaultPageSize on IPageService * chore: update .gitignore
1 parent cfddf4b commit 5733fe5

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

src/Examples/JsonApiDotNetCoreExample/Startups/ClientGeneratedIdsStartup.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace JsonApiDotNetCoreExample
1515
public class ClientGeneratedIdsStartup : Startup
1616
{
1717
public ClientGeneratedIdsStartup(IWebHostEnvironment env)
18-
: base (env)
19-
{ }
18+
: base(env)
19+
{ }
2020

2121
public override void ConfigureServices(IServiceCollection services)
2222
{
@@ -41,4 +41,4 @@ public override void ConfigureServices(IServiceCollection services)
4141
mvcBuilder: mvcBuilder);
4242
}
4343
}
44-
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Logging;
4+
using JsonApiDotNetCoreExample.Data;
5+
using Microsoft.EntityFrameworkCore;
6+
using JsonApiDotNetCore.Extensions;
7+
using System.Reflection;
8+
9+
namespace JsonApiDotNetCoreExample
10+
{
11+
/// <summary>
12+
/// This should be in JsonApiDotNetCoreExampleTests project but changes in .net core 3.0
13+
/// do no longer allow that. See https://github.com/aspnet/AspNetCore/issues/15373.
14+
/// </summary>
15+
public class NoDefaultPageSizeStartup : Startup
16+
{
17+
public NoDefaultPageSizeStartup(IWebHostEnvironment env)
18+
: base(env)
19+
{ }
20+
21+
public override void ConfigureServices(IServiceCollection services)
22+
{
23+
var loggerFactory = new LoggerFactory();
24+
var mvcBuilder = services.AddMvcCore();
25+
services
26+
.AddSingleton<ILoggerFactory>(loggerFactory)
27+
.AddLogging(builder =>
28+
{
29+
builder.AddConsole();
30+
})
31+
.AddDbContext<AppDbContext>(options => options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Transient)
32+
.AddJsonApi(options => {
33+
options.Namespace = "api/v1";
34+
options.IncludeTotalRecordCount = true;
35+
options.EnableResourceHooks = true;
36+
options.LoaDatabaseValues = true;
37+
options.AllowClientGeneratedIds = true;
38+
},
39+
discovery => discovery.AddAssembly(Assembly.Load(nameof(JsonApiDotNetCoreExample))),
40+
mvcBuilder: mvcBuilder);
41+
}
42+
}
43+
}

src/JsonApiDotNetCore/QueryParameterServices/Contracts/IPageService.cs

-6
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,13 @@ public interface IPageService : IQueryParameterService
1414
/// </summary>
1515
int PageSize { get; set; }
1616
/// <summary>
17-
/// What is the default page size
18-
/// </summary>
19-
int DefaultPageSize { get; set; }
20-
/// <summary>
2117
/// What page are we currently on
2218
/// </summary>
2319
int CurrentPage { get; set; }
24-
2520
/// <summary>
2621
/// Total amount of pages for request
2722
/// </summary>
2823
int TotalPages { get; }
29-
3024
/// <summary>
3125
/// Checks if pagination is enabled
3226
/// </summary>

src/JsonApiDotNetCore/QueryParameterServices/PageService.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@ public class PageService : QueryParameterService, IPageService
1515
public PageService(IJsonApiOptions options)
1616
{
1717
_options = options;
18-
DefaultPageSize = _options.DefaultPageSize;
1918
PageSize = _options.DefaultPageSize;
2019
}
2120
/// <inheritdoc/>
2221
public int? TotalRecords { get; set; }
2322
/// <inheritdoc/>
2423
public int PageSize { get; set; }
2524
/// <inheritdoc/>
26-
public int DefaultPageSize { get; set; } // I think we shouldnt expose this
27-
/// <inheritdoc/>
2825
public int CurrentPage { get; set; }
2926
/// <inheritdoc/>
30-
public int TotalPages => (TotalRecords == null) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize));
27+
public int TotalPages => (TotalRecords == null || PageSize == 0) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize));
3128

3229
/// <inheritdoc/>
3330
public virtual void Parse(KeyValuePair<string, StringValues> queryParameter)

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net;
1+
using System.Linq;
2+
using System.Net;
23
using System.Net.Http;
34
using System.Threading.Tasks;
45
using Bogus;
@@ -96,5 +97,31 @@ public async Task Included_Records_Contain_Relationship_Links()
9697
Assert.Equal($"http://localhost/api/v1/people/{person.Id}/relationships/todo-items", deserializedBody.Included[0].Relationships["todo-items"].Links.Self);
9798
context.Dispose();
9899
}
100+
101+
[Fact]
102+
public async Task GetResources_NoDefaultPageSize_ReturnsResources()
103+
{
104+
// Arrange
105+
var context = _fixture.GetService<AppDbContext>();
106+
var todoItems = _todoItemFaker.Generate(20).ToList();
107+
context.TodoItems.AddRange(todoItems);
108+
await context.SaveChangesAsync();
109+
110+
var builder = new WebHostBuilder()
111+
.UseStartup<NoDefaultPageSizeStartup>();
112+
var httpMethod = new HttpMethod("GET");
113+
var route = $"/api/v1/todo-items";
114+
var server = new TestServer(builder);
115+
var client = server.CreateClient();
116+
var request = new HttpRequestMessage(httpMethod, route);
117+
118+
// Act
119+
var response = await client.SendAsync(request);
120+
var body = await response.Content.ReadAsStringAsync();
121+
var result = _fixture.GetDeserializer().DeserializeList<TodoItem>(body);
122+
123+
// Assert
124+
Assert.True(result.Data.Count >= 20);
125+
}
99126
}
100127
}

0 commit comments

Comments
 (0)