Skip to content

Commit 4460029

Browse files
ClintGoodcsantero
authored andcommitted
Fix to allow filtering to work with entities that have integer keys (#109)
* Add Master and Child test entities that use integer keys Add failing test retrieving children related to a master * Fix to allow Get_related_to_many_integer_key to pass
1 parent 8a2477f commit 4460029

11 files changed

Lines changed: 319 additions & 207 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Id,ChildDescription,MasterId
2+
7500,"Child 1 Description",1500
3+
7501,"Child 2 Description",1501
4+
7502,"Child 3 Description",1501
5+
7503,"Child 4 Description",1503
6+
7504,"Child 5 Description",1503
7+
7505,"Child 6 Description",1503
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Id,Description
2+
1500,"Master 1 Description"
3+
1501,"Master 2 Description"
4+
1502,"Master 3 Description"
5+
1503,"Master 4 Description"

JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests/FetchingResourcesTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,18 @@ await AssertResponseContent(response,
183183
HttpStatusCode.OK);
184184
}
185185
}
186+
187+
[TestMethod]
188+
[DeploymentItem(@"Data\Master.csv", @"Data")]
189+
[DeploymentItem(@"Data\Child.csv", @"Data")]
190+
public async Task Get_related_to_many_integer_key()
191+
{
192+
using (var effortConnection = GetEffortConnection())
193+
{
194+
var response = await SubmitGet(effortConnection, "masters/1501/children");
195+
196+
await AssertResponseContent(response, @"Fixtures\FetchingResources\Get_related_to_many_integer_key_response.json", HttpStatusCode.OK);
197+
}
198+
}
186199
}
187200
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"data": [
3+
{
4+
"type": "children",
5+
"id": "7501",
6+
"attributes": {
7+
"child-description": "Child 2 Description"
8+
},
9+
"relationships": {
10+
"master": {
11+
"links": {
12+
"self": "https://www.example.com/children/7501/relationships/master",
13+
"related": "https://www.example.com/children/7501/master"
14+
}
15+
}
16+
}
17+
},
18+
{
19+
"type": "children",
20+
"id": "7502",
21+
"attributes": {
22+
"child-description": "Child 3 Description"
23+
},
24+
"relationships": {
25+
"master": {
26+
"links": {
27+
"self": "https://www.example.com/children/7502/relationships/master",
28+
"related": "https://www.example.com/children/7502/master"
29+
}
30+
}
31+
}
32+
}
33+
]
34+
}

JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests/JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@
123123
<EmbeddedResource Include="Fixtures\CreatingResources\Responses\Post_with_client_provided_id_Response.json" />
124124
<EmbeddedResource Include="Fixtures\CreatingResources\Responses\Post_with_empty_id_Response.json" />
125125
<None Include="App.config" />
126+
<None Include="Data\Child.csv">
127+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
128+
</None>
129+
<None Include="Data\Master.csv">
130+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
131+
</None>
126132
<None Include="Data\Officer.csv">
127133
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
128134
</None>
@@ -233,6 +239,7 @@
233239
<EmbeddedResource Include="Fixtures\Mapped\Responses\Get_resource_by_id_that_doesnt_exist.json" />
234240
<EmbeddedResource Include="Fixtures\Mapped\Responses\Get_related_to_one_response.json" />
235241
<EmbeddedResource Include="Fixtures\Mapped\Responses\Get_related_to_many_response.json" />
242+
<EmbeddedResource Include="Fixtures\FetchingResources\Get_related_to_many_integer_key_response.json" />
236243
<None Include="packages.config" />
237244
</ItemGroup>
238245
<Choose>

JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp/JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.csproj

Lines changed: 207 additions & 205 deletions
Large diffs are not rendered by default.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
using Newtonsoft.Json;
5+
6+
namespace JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Models
7+
{
8+
public class Child
9+
{
10+
public int Id { get; set; }
11+
12+
public string ChildDescription { get; set; }
13+
14+
[Required]
15+
[JsonIgnore]
16+
public int MasterId { get; set; }
17+
18+
[ForeignKey("MasterId")]
19+
public Master Master { get; set; }
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
6+
namespace JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Models
7+
{
8+
public class Master
9+
{
10+
public int Id { get; set; }
11+
12+
public string Description { get; set; }
13+
14+
public virtual ICollection<Child> Children { get; set; }
15+
}
16+
}

JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp/Models/TestDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
4747
public DbSet<StarshipClass> StarshipClasses { get; set; }
4848
public DbSet<Officer> Officers { get; set; }
4949
public DbSet<StarshipOfficerLink> StarshipOfficerLinks { get; set; }
50+
public DbSet<Master> Masters { get; set; }
51+
public DbSet<Child> Children { get; set; }
5052
}
5153
}

JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using JSONAPI.EntityFramework;
1414
using JSONAPI.EntityFramework.Configuration;
1515
using Owin;
16+
using System.Collections.Generic;
1617

1718
namespace JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp
1819
{
@@ -33,7 +34,9 @@ public Startup(Func<TestDbContext> dbContextFactory)
3334

3435
public void Configuration(IAppBuilder app)
3536
{
36-
var configuration = new JsonApiConfiguration();
37+
var configuration = new JsonApiConfiguration(
38+
new Core.PluralizationService(
39+
new Dictionary<string, string> { { "Child", "Children" } }));
3740
configuration.RegisterEntityFrameworkResourceType<Building>();
3841
configuration.RegisterEntityFrameworkResourceType<City>();
3942
configuration.RegisterEntityFrameworkResourceType<Comment>();
@@ -58,6 +61,8 @@ public void Configuration(IAppBuilder app)
5861
rc => rc.UseMaterializer<StarshipShipCounselorRelatedResourceMaterializer>());
5962
}); // Example of a resource that is mapped from a DB entity
6063
configuration.RegisterResourceType<StarshipOfficerDto>();
64+
configuration.RegisterEntityFrameworkResourceType<Master>();
65+
configuration.RegisterEntityFrameworkResourceType<Child>();
6166

6267
var configurator = new JsonApiHttpAutofacConfigurator();
6368
configurator.OnApplicationLifetimeScopeCreating(builder =>

0 commit comments

Comments
 (0)