Skip to content

Fix/deviating dbset name #603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class AppDbContext : DbContext
public DbSet<TodoItemCollection> TodoItemCollections { get; set; }
public DbSet<CamelCasedModel> CamelCasedModels { get; set; }
public DbSet<Article> Articles { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Author> AuthorDifferentDbContextName { get; set; }
public DbSet<NonJsonApiResource> NonJsonApiResources { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<PersonRole> PersonRoles { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Examples/NoEntityFrameworkExample/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private string GetResourceNameFromDbSetProperty(PropertyInfo property, Type reso

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

private (bool isJsonApiResource, Type idType) GetIdType(Type resourceType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 23 additions & 2 deletions test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ public void AddJsonApiInternals_Adds_All_Required_Services()
Assert.NotNull(provider.GetService(typeof(RepositoryRelationshipUpdateHelper<TodoItem>)));
}

[Fact]
public void RegisterResource_DeviatingDbContextPropertyName_RegistersCorrectly()
{
// Arrange
var services = new ServiceCollection();

services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase("UnitTestDb"), ServiceLifetime.Transient);
services.AddJsonApi<AppDbContext>();

// Act
// this is required because the DbContextResolver requires access to the current HttpContext
// to get the request scoped DbContext instance
services.AddScoped<IScopedServiceProvider, TestScopedServiceProvider>();
var provider = services.BuildServiceProvider();
var graph = provider.GetService<IResourceGraph>();
var resourceContext = graph.GetResourceContext<Author>();

// Assert
Assert.Equal("authors", resourceContext.ResourceName);
}

[Fact]
public void AddResourceService_Registers_All_Shorthand_Service_Interfaces()
{
Expand Down Expand Up @@ -116,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();
Expand All @@ -130,7 +151,7 @@ public void AddJsonApi_With_Context_Uses_DbSet_PropertyName_If_NoOtherSpecified(
var provider = services.BuildServiceProvider();
var resourceGraph = provider.GetService<IResourceGraph>();
var resource = resourceGraph.GetResourceContext(typeof(IntResource));
Assert.Equal("resource", resource.ResourceName);
Assert.Equal("int-resources", resource.ResourceName);
}

public class IntResource : Identifiable { }
Expand Down