From 11bc0881906a28b86e77c7a980178598a3506b26 Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 17:40:52 +0100 Subject: [PATCH 1/6] feat: new startup for test --- .../Startups/NoDefaultPageSizeStartup.cs | 44 +++++++++++++++++++ .../Properties/launchSettings.json | 14 +++--- 2 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs diff --git a/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs b/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs new file mode 100644 index 0000000000..334b48010c --- /dev/null +++ b/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using JsonApiDotNetCoreExample.Data; +using Microsoft.EntityFrameworkCore; +using JsonApiDotNetCore.Extensions; +using System.Reflection; + +namespace JsonApiDotNetCoreExample +{ + /// + /// This should be in JsonApiDotNetCoreExampleTests project but changes in .net core 3.0 + /// do no longer allow that. See https://github.com/aspnet/AspNetCore/issues/15373. + /// + public class ClientGeneratedIdsStartup : Startup + { + public ClientGeneratedIdsStartup(IWebHostEnvironment env) + : base(env) + { } + + public override void ConfigureServices(IServiceCollection services) + { + var loggerFactory = new LoggerFactory(); + var mvcBuilder = services.AddMvcCore(); + services + .AddSingleton(loggerFactory) + .AddLogging(builder => + { + builder.AddConsole(); + }) + .AddDbContext(options => options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Transient) + .AddJsonApi(options => { + options.Namespace = "api/v1"; + options.DefaultPageSize = 5; + options.IncludeTotalRecordCount = true; + options.EnableResourceHooks = true; + options.LoaDatabaseValues = true; + options.AllowClientGeneratedIds = true; + }, + discovery => discovery.AddAssembly(Assembly.Load(nameof(JsonApiDotNetCoreExample))), + mvcBuilder: mvcBuilder); + } + } +} diff --git a/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json b/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json index 1dff6cfe69..0abc738c49 100644 --- a/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json +++ b/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json @@ -8,20 +8,20 @@ } }, "profiles": { - "NoEntityFrameworkExample": { - "commandName": "Project", + "IIS Express": { + "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "http://localhost:5000/" + } }, - "IIS Express": { - "commandName": "IISExpress", + "NoEntityFrameworkExample": { + "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "applicationUrl": "http://localhost:5000/" } } } \ No newline at end of file From c13e970747e42af325b8550041d73dccee3b7283 Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 17:41:52 +0100 Subject: [PATCH 2/6] chore: rename startup class --- .../Startups/ClientGeneratedIdsStartup.cs | 6 +++--- .../Startups/NoDefaultPageSizeStartup.cs | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Examples/JsonApiDotNetCoreExample/Startups/ClientGeneratedIdsStartup.cs b/src/Examples/JsonApiDotNetCoreExample/Startups/ClientGeneratedIdsStartup.cs index 10255d6727..8f60d43a62 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Startups/ClientGeneratedIdsStartup.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Startups/ClientGeneratedIdsStartup.cs @@ -15,8 +15,8 @@ namespace JsonApiDotNetCoreExample public class ClientGeneratedIdsStartup : Startup { public ClientGeneratedIdsStartup(IWebHostEnvironment env) - : base (env) - { } + : base(env) + { } public override void ConfigureServices(IServiceCollection services) { @@ -41,4 +41,4 @@ public override void ConfigureServices(IServiceCollection services) mvcBuilder: mvcBuilder); } } -} +} \ No newline at end of file diff --git a/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs b/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs index 334b48010c..a15b71c2c5 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs @@ -12,9 +12,9 @@ namespace JsonApiDotNetCoreExample /// This should be in JsonApiDotNetCoreExampleTests project but changes in .net core 3.0 /// do no longer allow that. See https://github.com/aspnet/AspNetCore/issues/15373. /// - public class ClientGeneratedIdsStartup : Startup + public class NoDefaultPageSizeStartup : Startup { - public ClientGeneratedIdsStartup(IWebHostEnvironment env) + public NoDefaultPageSizeStartup(IWebHostEnvironment env) : base(env) { } @@ -31,7 +31,6 @@ public override void ConfigureServices(IServiceCollection services) .AddDbContext(options => options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Transient) .AddJsonApi(options => { options.Namespace = "api/v1"; - options.DefaultPageSize = 5; options.IncludeTotalRecordCount = true; options.EnableResourceHooks = true; options.LoaDatabaseValues = true; From ff1b6f933cae03dd9cddcc56fc473ccb90ae97a3 Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 17:42:37 +0100 Subject: [PATCH 3/6] test: reproduce #599 in test --- .../Acceptance/Spec/FetchingDataTests.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs index 77be87e23a..40d82b8888 100644 --- a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs @@ -1,4 +1,5 @@ -using System.Net; +using System.Linq; +using System.Net; using System.Net.Http; using System.Threading.Tasks; using Bogus; @@ -96,5 +97,31 @@ public async Task Included_Records_Contain_Relationship_Links() Assert.Equal($"http://localhost/api/v1/people/{person.Id}/relationships/todo-items", deserializedBody.Included[0].Relationships["todo-items"].Links.Self); context.Dispose(); } + + [Fact] + public async Task GetResources_NoDefaultPageSize_ReturnsResources() + { + // Arrange + var context = _fixture.GetService(); + var todoItems = _todoItemFaker.Generate(20).ToList(); + context.TodoItems.AddRange(todoItems); + await context.SaveChangesAsync(); + + var builder = new WebHostBuilder() + .UseStartup(); + var httpMethod = new HttpMethod("GET"); + var route = $"/api/v1/todo-items"; + var server = new TestServer(builder); + var client = server.CreateClient(); + var request = new HttpRequestMessage(httpMethod, route); + + // Act + var response = await client.SendAsync(request); + var body = await response.Content.ReadAsStringAsync(); + var result = _fixture.GetDeserializer().DeserializeList(body); + + // Assert + Assert.True(result.Data.Count >= 20); + } } } From 9c1a60a4652c1e2619acccd465b5a7987578478f Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 17:47:58 +0100 Subject: [PATCH 4/6] fix: zero division error --- src/JsonApiDotNetCore/QueryParameterServices/PageService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonApiDotNetCore/QueryParameterServices/PageService.cs b/src/JsonApiDotNetCore/QueryParameterServices/PageService.cs index d4aa5052ee..d2e821e005 100644 --- a/src/JsonApiDotNetCore/QueryParameterServices/PageService.cs +++ b/src/JsonApiDotNetCore/QueryParameterServices/PageService.cs @@ -27,7 +27,7 @@ public PageService(IJsonApiOptions options) /// public int CurrentPage { get; set; } /// - public int TotalPages => (TotalRecords == null) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize)); + public int TotalPages => (TotalRecords == null || DefaultPageSize == 0) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize)); /// public virtual void Parse(KeyValuePair queryParameter) From 9bbb1d7a492baef75009f7eb83eb9025367c3897 Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 17:51:46 +0100 Subject: [PATCH 5/6] chore: remove redundant DefaultPageSize on IPageService --- .../QueryParameterServices/Contracts/IPageService.cs | 6 ------ src/JsonApiDotNetCore/QueryParameterServices/PageService.cs | 5 +---- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/JsonApiDotNetCore/QueryParameterServices/Contracts/IPageService.cs b/src/JsonApiDotNetCore/QueryParameterServices/Contracts/IPageService.cs index 76f56baf6a..017773d3d6 100644 --- a/src/JsonApiDotNetCore/QueryParameterServices/Contracts/IPageService.cs +++ b/src/JsonApiDotNetCore/QueryParameterServices/Contracts/IPageService.cs @@ -14,19 +14,13 @@ public interface IPageService : IQueryParameterService /// int PageSize { get; set; } /// - /// What is the default page size - /// - int DefaultPageSize { get; set; } - /// /// What page are we currently on /// int CurrentPage { get; set; } - /// /// Total amount of pages for request /// int TotalPages { get; } - /// /// Checks if pagination is enabled /// diff --git a/src/JsonApiDotNetCore/QueryParameterServices/PageService.cs b/src/JsonApiDotNetCore/QueryParameterServices/PageService.cs index d2e821e005..6f1559246c 100644 --- a/src/JsonApiDotNetCore/QueryParameterServices/PageService.cs +++ b/src/JsonApiDotNetCore/QueryParameterServices/PageService.cs @@ -15,7 +15,6 @@ public class PageService : QueryParameterService, IPageService public PageService(IJsonApiOptions options) { _options = options; - DefaultPageSize = _options.DefaultPageSize; PageSize = _options.DefaultPageSize; } /// @@ -23,11 +22,9 @@ public PageService(IJsonApiOptions options) /// public int PageSize { get; set; } /// - public int DefaultPageSize { get; set; } // I think we shouldnt expose this - /// public int CurrentPage { get; set; } /// - public int TotalPages => (TotalRecords == null || DefaultPageSize == 0) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize)); + public int TotalPages => (TotalRecords == null || PageSize == 0) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize)); /// public virtual void Parse(KeyValuePair queryParameter) From b7c403b08bdd91172b1f779a91b0436020963bd2 Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 18:37:35 +0100 Subject: [PATCH 6/6] chore: update .gitignore --- .../NoEntityFrameworkExample/.gitignore | 2 ++ .../Properties/launchSettings.json | 27 ------------------- 2 files changed, 2 insertions(+), 27 deletions(-) delete mode 100644 src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json diff --git a/src/Examples/NoEntityFrameworkExample/.gitignore b/src/Examples/NoEntityFrameworkExample/.gitignore index 0ca27f04e1..700191e656 100644 --- a/src/Examples/NoEntityFrameworkExample/.gitignore +++ b/src/Examples/NoEntityFrameworkExample/.gitignore @@ -22,6 +22,8 @@ bld/ [Bb]in/ [Oo]bj/ +Properties/launchSettings.json + # Visual Studio 2015 cache/options directory .vs/ # Uncomment if you have tasks that create the project's static files in wwwroot diff --git a/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json b/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json deleted file mode 100644 index 0abc738c49..0000000000 --- a/src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:57181/", - "sslPort": 0 - } - }, - "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "NoEntityFrameworkExample": { - "commandName": "Project", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "http://localhost:5000/" - } - } -} \ No newline at end of file