Skip to content

Commit dae593f

Browse files
author
Chris Santero
committed
return 404 for to-one related resources to non-existant resource
1 parent edcb959 commit dae593f

5 files changed

Lines changed: 34 additions & 2 deletions

File tree

JSONAPI.EntityFramework.Tests/Acceptance/FetchingResourcesTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,23 @@ public async Task Get_related_to_one()
114114
await AssertResponseContent(response, @"Acceptance\Fixtures\FetchingResources\Get_related_to_one_response.json", HttpStatusCode.OK);
115115
}
116116
}
117+
118+
[TestMethod]
119+
[DeploymentItem(@"Acceptance\Data\Comment.csv", @"Acceptance\Data")]
120+
[DeploymentItem(@"Acceptance\Data\Post.csv", @"Acceptance\Data")]
121+
[DeploymentItem(@"Acceptance\Data\PostTagLink.csv", @"Acceptance\Data")]
122+
[DeploymentItem(@"Acceptance\Data\Tag.csv", @"Acceptance\Data")]
123+
[DeploymentItem(@"Acceptance\Data\User.csv", @"Acceptance\Data")]
124+
public async Task Get_related_to_one_for_resource_that_doesnt_exist()
125+
{
126+
using (var effortConnection = GetEffortConnection())
127+
{
128+
var response = await SubmitGet(effortConnection, "posts/3000/author");
129+
130+
await AssertResponseContent(response,
131+
@"Acceptance\Fixtures\FetchingResources\Get_related_to_one_for_resource_that_doesnt_exist.json",
132+
HttpStatusCode.NotFound, true);
133+
}
134+
}
117135
}
118136
}
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 resource of type `posts` exists with id `3000`."
8+
}
9+
]
10+
}

JSONAPI.EntityFramework.Tests/Acceptance/Fixtures/FetchingResources/Get_resource_by_id_that_doesnt_exist.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"id": "{{SOME_GUID}}",
55
"status": "404",
66
"title": "Resource not found",
7-
"detail": "No resource of type `posts` exists with id `3000`"
7+
"detail": "No resource of type `posts` exists with id `3000`."
88
}
99
]
1010
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
<EmbeddedResource Include="Acceptance\Fixtures\UpdatingResources\Responses\Patch_with_unknown_relationship_Response.json" />
205205
<EmbeddedResource Include="Acceptance\Fixtures\UpdatingResources\Requests\Patch_with_unknown_relationship_Request.json" />
206206
<EmbeddedResource Include="Acceptance\Fixtures\FetchingResources\Get_resource_by_id_that_doesnt_exist.json" />
207+
<EmbeddedResource Include="Acceptance\Fixtures\FetchingResources\Get_related_to_one_for_resource_that_doesnt_exist.json" />
207208
<None Include="App.Config">
208209
<SubType>Designer</SubType>
209210
</None>

JSONAPI.EntityFramework/Http/EntityFrameworkDocumentMaterializer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public virtual async Task<ISingleResourceDocument> GetRecordById(string id, Http
6969
var registration = _resourceTypeRegistry.GetRegistrationForType(typeof(T));
7070
var singleResource = await FilterById<T>(id, registration).FirstOrDefaultAsync(cancellationToken);
7171
if (singleResource == null)
72-
throw JsonApiException.CreateForNotFound(string.Format("No resource of type `{0}` exists with id `{1}`",
72+
throw JsonApiException.CreateForNotFound(string.Format("No resource of type `{0}` exists with id `{1}`.",
7373
registration.ResourceTypeName, id));
7474
return _singleResourceDocumentBuilder.BuildDocument(singleResource, apiBaseUrl, null);
7575
}
@@ -172,6 +172,9 @@ protected async Task<ISingleResourceDocument> GetRelatedToOne<TRelated>(string i
172172

173173
var primaryEntityQuery = FilterById<T>(id, primaryEntityRegistration);
174174
var relatedResource = await primaryEntityQuery.Select(lambda).FirstOrDefaultAsync(cancellationToken);
175+
if (relatedResource == null)
176+
throw JsonApiException.CreateForNotFound(string.Format("No resource of type `{0}` exists with id `{1}`.",
177+
primaryEntityRegistration.ResourceTypeName, id));
175178
return _singleResourceDocumentBuilder.BuildDocument(relatedResource, GetBaseUrlFromRequest(request), null);
176179
}
177180

0 commit comments

Comments
 (0)