Skip to content

Commit 1abdddc

Browse files
author
Chris Santero
committed
don't fail on unknown fields; ignore instead
1 parent ec1cda6 commit 1abdddc

7 files changed

Lines changed: 153 additions & 2 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"data": {
3+
"type": "posts",
4+
"id": "202",
5+
"attributes": {
6+
"title": "New post title",
7+
"some-fake-attribute": 99
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"data": {
3+
"type": "posts",
4+
"id": "202",
5+
"attributes": {
6+
"title": "New post title"
7+
},
8+
"relationships": {
9+
"some-fake-relationship": {
10+
"data": { "type": "author", "id": "45000" }
11+
}
12+
}
13+
}
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"data": {
3+
"type": "posts",
4+
"id": "202",
5+
"attributes": {
6+
"content": "Post 2 content",
7+
"created": "2015-02-05T08:10:00.0000000+00:00",
8+
"title": "New post title"
9+
},
10+
"relationships": {
11+
"author": {
12+
"links": {
13+
"self": "https://www.example.com/posts/202/relationships/author",
14+
"related": "https://www.example.com/posts/202/author"
15+
}
16+
},
17+
"comments": {
18+
"links": {
19+
"self": "https://www.example.com/posts/202/relationships/comments",
20+
"related": "https://www.example.com/posts/202/comments"
21+
}
22+
},
23+
"tags": {
24+
"links": {
25+
"self": "https://www.example.com/posts/202/relationships/tags",
26+
"related": "https://www.example.com/posts/202/tags"
27+
}
28+
}
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"data": {
3+
"type": "posts",
4+
"id": "202",
5+
"attributes": {
6+
"content": "Post 2 content",
7+
"created": "2015-02-05T08:10:00.0000000+00:00",
8+
"title": "New post title"
9+
},
10+
"relationships": {
11+
"author": {
12+
"links": {
13+
"self": "https://www.example.com/posts/202/relationships/author",
14+
"related": "https://www.example.com/posts/202/author"
15+
}
16+
},
17+
"comments": {
18+
"links": {
19+
"self": "https://www.example.com/posts/202/relationships/comments",
20+
"related": "https://www.example.com/posts/202/comments"
21+
}
22+
},
23+
"tags": {
24+
"links": {
25+
"self": "https://www.example.com/posts/202/relationships/tags",
26+
"related": "https://www.example.com/posts/202/tags"
27+
}
28+
}
29+
}
30+
}
31+
}

JSONAPI.EntityFramework.Tests/Acceptance/UpdatingResourcesTests.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,64 @@ public async Task PatchWithAttributeUpdate()
4141
}
4242
}
4343

44+
[TestMethod]
45+
[DeploymentItem(@"Acceptance\Data\Comment.csv", @"Acceptance\Data")]
46+
[DeploymentItem(@"Acceptance\Data\Post.csv", @"Acceptance\Data")]
47+
[DeploymentItem(@"Acceptance\Data\PostTagLink.csv", @"Acceptance\Data")]
48+
[DeploymentItem(@"Acceptance\Data\Tag.csv", @"Acceptance\Data")]
49+
[DeploymentItem(@"Acceptance\Data\User.csv", @"Acceptance\Data")]
50+
public async Task Patch_with_unknown_attribute()
51+
{
52+
using (var effortConnection = GetEffortConnection())
53+
{
54+
var response = await SubmitPatch(effortConnection, "posts/202", @"Acceptance\Fixtures\UpdatingResources\Requests\Patch_with_unknown_attribute_Request.json");
55+
56+
await AssertResponseContent(response, @"Acceptance\Fixtures\UpdatingResources\Responses\Patch_with_unknown_attribute_Response.json", HttpStatusCode.OK);
57+
58+
using (var dbContext = new TestDbContext(effortConnection, false))
59+
{
60+
var allPosts = dbContext.Posts.Include(p => p.Tags).ToArray();
61+
allPosts.Length.Should().Be(4);
62+
var actualPost = allPosts.First(t => t.Id == "202");
63+
actualPost.Id.Should().Be("202");
64+
actualPost.Title.Should().Be("New post title");
65+
actualPost.Content.Should().Be("Post 2 content");
66+
actualPost.Created.Should().Be(new DateTimeOffset(2015, 02, 05, 08, 10, 0, new TimeSpan(0)));
67+
actualPost.AuthorId.Should().Be("401");
68+
actualPost.Tags.Select(t => t.Id).Should().BeEquivalentTo("302", "303");
69+
}
70+
}
71+
}
72+
73+
[TestMethod]
74+
[DeploymentItem(@"Acceptance\Data\Comment.csv", @"Acceptance\Data")]
75+
[DeploymentItem(@"Acceptance\Data\Post.csv", @"Acceptance\Data")]
76+
[DeploymentItem(@"Acceptance\Data\PostTagLink.csv", @"Acceptance\Data")]
77+
[DeploymentItem(@"Acceptance\Data\Tag.csv", @"Acceptance\Data")]
78+
[DeploymentItem(@"Acceptance\Data\User.csv", @"Acceptance\Data")]
79+
public async Task Patch_with_unknown_relationship()
80+
{
81+
using (var effortConnection = GetEffortConnection())
82+
{
83+
var response = await SubmitPatch(effortConnection, "posts/202", @"Acceptance\Fixtures\UpdatingResources\Requests\Patch_with_unknown_relationship_Request.json");
84+
85+
await AssertResponseContent(response, @"Acceptance\Fixtures\UpdatingResources\Responses\Patch_with_unknown_relationship_Response.json", HttpStatusCode.OK);
86+
87+
using (var dbContext = new TestDbContext(effortConnection, false))
88+
{
89+
var allPosts = dbContext.Posts.Include(p => p.Tags).ToArray();
90+
allPosts.Length.Should().Be(4);
91+
var actualPost = allPosts.First(t => t.Id == "202");
92+
actualPost.Id.Should().Be("202");
93+
actualPost.Title.Should().Be("New post title");
94+
actualPost.Content.Should().Be("Post 2 content");
95+
actualPost.Created.Should().Be(new DateTimeOffset(2015, 02, 05, 08, 10, 0, new TimeSpan(0)));
96+
actualPost.AuthorId.Should().Be("401");
97+
actualPost.Tags.Select(t => t.Id).Should().BeEquivalentTo("302", "303");
98+
}
99+
}
100+
}
101+
44102
[TestMethod]
45103
[DeploymentItem(@"Acceptance\Data\Comment.csv", @"Acceptance\Data")]
46104
[DeploymentItem(@"Acceptance\Data\Post.csv", @"Acceptance\Data")]

JSONAPI.EntityFramework.Tests/JSONAPI.EntityFramework.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@
200200
<EmbeddedResource Include="Acceptance\Fixtures\ComputedId\Responses\Get_resource_with_computed_id_by_id_Response.json" />
201201
<EmbeddedResource Include="Acceptance\Fixtures\CreatingResources\Requests\Post_with_empty_id_Request.json" />
202202
<EmbeddedResource Include="Acceptance\Fixtures\CreatingResources\Responses\Post_with_empty_id_Response.json" />
203+
<EmbeddedResource Include="Acceptance\Fixtures\UpdatingResources\Requests\Patch_with_unknown_attribute_Request.json" />
204+
<EmbeddedResource Include="Acceptance\Fixtures\UpdatingResources\Responses\Patch_with_unknown_attribute_Response.json" />
205+
<EmbeddedResource Include="Acceptance\Fixtures\UpdatingResources\Responses\Patch_with_unknown_relationship_Response.json" />
206+
<EmbeddedResource Include="Acceptance\Fixtures\UpdatingResources\Requests\Patch_with_unknown_relationship_Request.json" />
203207
<None Include="App.Config">
204208
<SubType>Designer</SubType>
205209
</None>

JSONAPI.EntityFramework/EntityFrameworkResourceObjectMaterializer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,18 @@ protected virtual async Task MergeFieldsIntoProperties(IResourceObject resourceO
8787
{
8888
foreach (var attributeValue in resourceObject.Attributes)
8989
{
90-
var attribute = (ResourceTypeAttribute) registration.GetFieldByName(attributeValue.Key);
90+
var attribute = registration.GetFieldByName(attributeValue.Key) as ResourceTypeAttribute;
91+
if (attribute == null) continue;
9192
attribute.SetValue(material, attributeValue.Value);
9293
}
9394

9495
foreach (var relationshipValue in resourceObject.Relationships)
9596
{
9697
var linkage = relationshipValue.Value.Linkage;
9798

98-
var typeRelationship = (ResourceTypeRelationship) registration.GetFieldByName(relationshipValue.Key);
99+
var typeRelationship = registration.GetFieldByName(relationshipValue.Key) as ResourceTypeRelationship;
100+
if (typeRelationship == null) continue;
101+
99102
if (typeRelationship.IsToMany)
100103
{
101104
if (linkage == null)

0 commit comments

Comments
 (0)