Skip to content

Commit bc083df

Browse files
author
Chris Santero
committed
don't crash when requesting related resource for non-existent relationship
1 parent b4fb7d2 commit bc083df

4 files changed

Lines changed: 32 additions & 0 deletions

File tree

JSONAPI.EntityFramework.Tests/Acceptance/FetchingResourcesTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,23 @@ await AssertResponseContent(response,
150150
HttpStatusCode.NotFound, true);
151151
}
152152
}
153+
154+
[TestMethod]
155+
[DeploymentItem(@"Acceptance\Data\Comment.csv", @"Acceptance\Data")]
156+
[DeploymentItem(@"Acceptance\Data\Post.csv", @"Acceptance\Data")]
157+
[DeploymentItem(@"Acceptance\Data\PostTagLink.csv", @"Acceptance\Data")]
158+
[DeploymentItem(@"Acceptance\Data\Tag.csv", @"Acceptance\Data")]
159+
[DeploymentItem(@"Acceptance\Data\User.csv", @"Acceptance\Data")]
160+
public async Task Get_related_resource_for_relationship_that_doesnt_exist()
161+
{
162+
using (var effortConnection = GetEffortConnection())
163+
{
164+
var response = await SubmitGet(effortConnection, "posts/201/bananas");
165+
166+
await AssertResponseContent(response,
167+
@"Acceptance\Fixtures\FetchingResources\Get_related_resource_for_relationship_that_doesnt_exist.json",
168+
HttpStatusCode.NotFound, true);
169+
}
170+
}
153171
}
154172
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"errors": [
3+
{
4+
"id": "{{SOME_GUID}}",
5+
"status": "404",
6+
"title": "Resource not found",
7+
"detail": "No relationship `bananas` exists for the resource with type `posts` and id `201`."
8+
}
9+
]
10+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
<EmbeddedResource Include="Acceptance\Fixtures\FetchingResources\Get_resource_by_id_that_doesnt_exist.json" />
207207
<EmbeddedResource Include="Acceptance\Fixtures\FetchingResources\Get_related_to_one_for_resource_that_doesnt_exist.json" />
208208
<EmbeddedResource Include="Acceptance\Fixtures\FetchingResources\Get_related_to_many_for_resource_that_doesnt_exist.json" />
209+
<EmbeddedResource Include="Acceptance\Fixtures\FetchingResources\Get_related_resource_for_relationship_that_doesnt_exist.json" />
209210
<None Include="App.Config">
210211
<SubType>Designer</SubType>
211212
</None>

JSONAPI.EntityFramework/Http/EntityFrameworkDocumentMaterializer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public virtual async Task<IJsonApiDocument> GetRelated(string id, string relatio
7979
{
8080
var registration = _resourceTypeRegistry.GetRegistrationForType(typeof (T));
8181
var relationship = (ResourceTypeRelationship) registration.GetFieldByName(relationshipKey);
82+
if (relationship == null)
83+
throw JsonApiException.CreateForNotFound(string.Format("No relationship `{0}` exists for the resource with type `{1}` and id `{2}`.",
84+
relationshipKey, registration.ResourceTypeName, id));
8285

8386
if (relationship.IsToMany)
8487
{

0 commit comments

Comments
 (0)