Skip to content

Commit cfddf4b

Browse files
maureiwisepotato
authored andcommitted
Fix/deviating dbset name (#603)
* test: expose dbset name bug * fix: deviating dbset name * chore: add launchSettings.json to gitignore * chore: delete launchSettings.json from git
1 parent eee65ee commit cfddf4b

File tree

6 files changed

+28
-32
lines changed

6 files changed

+28
-32
lines changed

src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class AppDbContext : DbContext
1111
public DbSet<TodoItemCollection> TodoItemCollections { get; set; }
1212
public DbSet<CamelCasedModel> CamelCasedModels { get; set; }
1313
public DbSet<Article> Articles { get; set; }
14-
public DbSet<Author> Authors { get; set; }
14+
public DbSet<Author> AuthorDifferentDbContextName { get; set; }
1515
public DbSet<NonJsonApiResource> NonJsonApiResources { get; set; }
1616
public DbSet<User> Users { get; set; }
1717
public DbSet<PersonRole> PersonRoles { get; set; }

src/Examples/NoEntityFrameworkExample/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ bld/
2222
[Bb]in/
2323
[Oo]bj/
2424

25+
Properties/launchSettings.json
26+
2527
# Visual Studio 2015 cache/options directory
2628
.vs/
2729
# Uncomment if you have tasks that create the project's static files in wwwroot

src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private string GetResourceNameFromDbSetProperty(PropertyInfo property, Type reso
218218

219219
// fallback to the established convention using the DbSet Property.Name
220220
// e.g DbSet<FooBar> FooBars { get; set; } => "foo-bars"
221-
return _resourceNameFormatter.ApplyCasingConvention(property.Name);
221+
return _resourceNameFormatter.FormatResourceName(resourceType);
222222
}
223223

224224
private (bool isJsonApiResource, Type idType) GetIdType(Type resourceType)

test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public async Task Can_Create_Many_To_Many()
240240
var tag = _tagFaker.Generate();
241241
var author = new Author();
242242
context.Tags.Add(tag);
243-
context.Authors.Add(author);
243+
context.AuthorDifferentDbContextName.Add(author);
244244
await context.SaveChangesAsync();
245245

246246
var article = _articleFaker.Generate();

test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ public void AddJsonApiInternals_Adds_All_Required_Services()
5959
Assert.NotNull(provider.GetService(typeof(RepositoryRelationshipUpdateHelper<TodoItem>)));
6060
}
6161

62+
[Fact]
63+
public void RegisterResource_DeviatingDbContextPropertyName_RegistersCorrectly()
64+
{
65+
// Arrange
66+
var services = new ServiceCollection();
67+
68+
services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase("UnitTestDb"), ServiceLifetime.Transient);
69+
services.AddJsonApi<AppDbContext>();
70+
71+
// Act
72+
// this is required because the DbContextResolver requires access to the current HttpContext
73+
// to get the request scoped DbContext instance
74+
services.AddScoped<IScopedServiceProvider, TestScopedServiceProvider>();
75+
var provider = services.BuildServiceProvider();
76+
var graph = provider.GetService<IResourceGraph>();
77+
var resourceContext = graph.GetResourceContext<Author>();
78+
79+
// Assert
80+
Assert.Equal("authors", resourceContext.ResourceName);
81+
}
82+
6283
[Fact]
6384
public void AddResourceService_Registers_All_Shorthand_Service_Interfaces()
6485
{
@@ -116,7 +137,7 @@ public void AddResourceService_Throws_If_Type_Does_Not_Implement_Any_Interfaces(
116137
}
117138

118139
[Fact]
119-
public void AddJsonApi_With_Context_Uses_DbSet_PropertyName_If_NoOtherSpecified()
140+
public void AddJsonApi_With_Context_Uses_Resource_Type_Name_If_NoOtherSpecified()
120141
{
121142
// Arrange
122143
var services = new ServiceCollection();
@@ -130,7 +151,7 @@ public void AddJsonApi_With_Context_Uses_DbSet_PropertyName_If_NoOtherSpecified(
130151
var provider = services.BuildServiceProvider();
131152
var resourceGraph = provider.GetService<IResourceGraph>();
132153
var resource = resourceGraph.GetResourceContext(typeof(IntResource));
133-
Assert.Equal("resource", resource.ResourceName);
154+
Assert.Equal("int-resources", resource.ResourceName);
134155
}
135156

136157
public class IntResource : Identifiable { }

0 commit comments

Comments
 (0)