From 226681de97420f03b7372e3e1710f904195aa3df Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 18:25:22 +0100 Subject: [PATCH 1/4] test: expose dbset name bug --- .../Data/AppDbContext.cs | 2 +- .../Properties/launchSettings.json | 14 ++++++------- .../Acceptance/ManyToManyTests.cs | 2 +- .../IServiceCollectionExtensionsTests.cs | 21 +++++++++++++++++++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs b/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs index a1887ba235..7faa22bd55 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs @@ -11,7 +11,7 @@ public class AppDbContext : DbContext public DbSet TodoItemCollections { get; set; } public DbSet CamelCasedModels { get; set; } public DbSet
Articles { get; set; } - public DbSet Authors { get; set; } + public DbSet AuthorDifferentDbContextName { get; set; } public DbSet NonJsonApiResources { get; set; } public DbSet Users { get; set; } public DbSet PersonRoles { get; set; } 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 diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs index 1df5593fb0..6b6bb526a3 100644 --- a/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs @@ -240,7 +240,7 @@ public async Task Can_Create_Many_To_Many() var tag = _tagFaker.Generate(); var author = new Author(); context.Tags.Add(tag); - context.Authors.Add(author); + context.AuthorDifferentDbContextName.Add(author); await context.SaveChangesAsync(); var article = _articleFaker.Generate(); diff --git a/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs b/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs index d23b1f4d9d..60d1f99fd8 100644 --- a/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs +++ b/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs @@ -59,6 +59,27 @@ public void AddJsonApiInternals_Adds_All_Required_Services() Assert.NotNull(provider.GetService(typeof(RepositoryRelationshipUpdateHelper))); } + [Fact] + public void RegisterResource_DeviatingDbContextPropertyName_RegistersCorrectly() + { + // Arrange + var services = new ServiceCollection(); + + services.AddDbContext(options => options.UseInMemoryDatabase("UnitTestDb"), ServiceLifetime.Transient); + services.AddJsonApi(); + + // Act + // this is required because the DbContextResolver requires access to the current HttpContext + // to get the request scoped DbContext instance + services.AddScoped(); + var provider = services.BuildServiceProvider(); + var graph = provider.GetService(); + var resourceContext = graph.GetResourceContext(); + + // Assert + Assert.Equal("authors", resourceContext.ResourceName); + } + [Fact] public void AddResourceService_Registers_All_Shorthand_Service_Interfaces() { From edbeed13d94272bb889ee6b91688cdf583a94325 Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 18:29:47 +0100 Subject: [PATCH 2/4] fix: deviating dbset name --- src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs | 2 +- .../UnitTests/Extensions/IServiceCollectionExtensionsTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs b/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs index 405fe64936..e025d4f7de 100644 --- a/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs @@ -218,7 +218,7 @@ private string GetResourceNameFromDbSetProperty(PropertyInfo property, Type reso // fallback to the established convention using the DbSet Property.Name // e.g DbSet FooBars { get; set; } => "foo-bars" - return _resourceNameFormatter.ApplyCasingConvention(property.Name); + return _resourceNameFormatter.FormatResourceName(resourceType); } private (bool isJsonApiResource, Type idType) GetIdType(Type resourceType) diff --git a/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs b/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs index 60d1f99fd8..20fa848b35 100644 --- a/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs +++ b/test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs @@ -137,7 +137,7 @@ public void AddResourceService_Throws_If_Type_Does_Not_Implement_Any_Interfaces( } [Fact] - public void AddJsonApi_With_Context_Uses_DbSet_PropertyName_If_NoOtherSpecified() + public void AddJsonApi_With_Context_Uses_Resource_Type_Name_If_NoOtherSpecified() { // Arrange var services = new ServiceCollection(); @@ -151,7 +151,7 @@ public void AddJsonApi_With_Context_Uses_DbSet_PropertyName_If_NoOtherSpecified( var provider = services.BuildServiceProvider(); var resourceGraph = provider.GetService(); var resource = resourceGraph.GetResourceContext(typeof(IntResource)); - Assert.Equal("resource", resource.ResourceName); + Assert.Equal("int-resources", resource.ResourceName); } public class IntResource : Identifiable { } From ff6c4f306df357c35aeb56652c0a787ebe685393 Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 18:34:19 +0100 Subject: [PATCH 3/4] chore: add launchSettings.json to gitignore --- src/Examples/NoEntityFrameworkExample/.gitignore | 2 ++ 1 file changed, 2 insertions(+) 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 From 080840a9e212deb9d83ff96f78391fc4e9422999 Mon Sep 17 00:00:00 2001 From: Maurits Moeys Date: Wed, 30 Oct 2019 18:35:08 +0100 Subject: [PATCH 4/4] chore: delete launchSettings.json from git --- .../Properties/launchSettings.json | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json 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