Skip to content

Commit b277e1f

Browse files
author
Bart Koelman
committed
Always set IJsonApiRequest.OperationKind from middleware
1 parent f23ab45 commit b277e1f

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

src/JsonApiDotNetCore/Middleware/IJsonApiRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public interface IJsonApiRequest
4646
bool IsReadOnly { get; }
4747

4848
/// <summary>
49-
/// In case of an atomic:operations request, this indicates the kind of operation currently being processed.
49+
/// In case of a non-readonly request, this indicates the kind of write operation currently being processed.
5050
/// </summary>
5151
OperationKind? OperationKind { get; }
5252

src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ private static void SetupResourceRequest(JsonApiRequest request, ResourceContext
212212
IResourceContextProvider resourceContextProvider, HttpRequest httpRequest)
213213
{
214214
request.IsReadOnly = httpRequest.Method == HttpMethod.Get.Method || httpRequest.Method == HttpMethod.Head.Method;
215-
request.Kind = EndpointKind.Primary;
216215
request.PrimaryResource = primaryResourceContext;
217216
request.PrimaryId = GetPrimaryRequestId(routeValues);
218217

@@ -222,6 +221,17 @@ private static void SetupResourceRequest(JsonApiRequest request, ResourceContext
222221
{
223222
request.Kind = IsRouteForRelationship(routeValues) ? EndpointKind.Relationship : EndpointKind.Secondary;
224223

224+
// @formatter:wrap_chained_method_calls chop_always
225+
// @formatter:keep_existing_linebreaks true
226+
227+
request.OperationKind =
228+
httpRequest.Method == HttpMethod.Post.Method ? OperationKind.AddToRelationship :
229+
httpRequest.Method == HttpMethod.Patch.Method ? OperationKind.SetRelationship :
230+
httpRequest.Method == HttpMethod.Delete.Method ? OperationKind.RemoveFromRelationship : null;
231+
232+
// @formatter:keep_existing_linebreaks restore
233+
// @formatter:wrap_chained_method_calls restore
234+
225235
RelationshipAttribute requestRelationship =
226236
primaryResourceContext.Relationships.SingleOrDefault(relationship => relationship.PublicName == relationshipName);
227237

@@ -231,6 +241,21 @@ private static void SetupResourceRequest(JsonApiRequest request, ResourceContext
231241
request.SecondaryResource = resourceContextProvider.GetResourceContext(requestRelationship.RightType);
232242
}
233243
}
244+
else
245+
{
246+
request.Kind = EndpointKind.Primary;
247+
248+
// @formatter:wrap_chained_method_calls chop_always
249+
// @formatter:keep_existing_linebreaks true
250+
251+
request.OperationKind =
252+
httpRequest.Method == HttpMethod.Post.Method ? OperationKind.CreateResource :
253+
httpRequest.Method == HttpMethod.Patch.Method ? OperationKind.UpdateResource :
254+
httpRequest.Method == HttpMethod.Delete.Method ? OperationKind.DeleteResource : null;
255+
256+
// @formatter:keep_existing_linebreaks restore
257+
// @formatter:wrap_chained_method_calls restore
258+
}
234259

235260
bool isGetAll = request.PrimaryId == null && request.IsReadOnly;
236261
request.IsCollection = isGetAll || request.Relationship is HasManyAttribute;

test/UnitTests/Middleware/JsonApiRequestTests.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ namespace UnitTests.Middleware
1818
public sealed class JsonApiRequestTests
1919
{
2020
[Theory]
21-
[InlineData("HEAD", "/todoItems", true, EndpointKind.Primary, true)]
22-
[InlineData("HEAD", "/todoItems/1", false, EndpointKind.Primary, true)]
23-
[InlineData("HEAD", "/todoItems/1/owner", false, EndpointKind.Secondary, true)]
24-
[InlineData("HEAD", "/todoItems/1/tags", true, EndpointKind.Secondary, true)]
25-
[InlineData("HEAD", "/todoItems/1/relationships/owner", false, EndpointKind.Relationship, true)]
26-
[InlineData("HEAD", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, true)]
27-
[InlineData("GET", "/todoItems", true, EndpointKind.Primary, true)]
28-
[InlineData("GET", "/todoItems/1", false, EndpointKind.Primary, true)]
29-
[InlineData("GET", "/todoItems/1/owner", false, EndpointKind.Secondary, true)]
30-
[InlineData("GET", "/todoItems/1/tags", true, EndpointKind.Secondary, true)]
31-
[InlineData("GET", "/todoItems/1/relationships/owner", false, EndpointKind.Relationship, true)]
32-
[InlineData("GET", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, true)]
33-
[InlineData("POST", "/todoItems", false, EndpointKind.Primary, false)]
34-
[InlineData("POST", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, false)]
35-
[InlineData("PATCH", "/todoItems/1", false, EndpointKind.Primary, false)]
36-
[InlineData("PATCH", "/todoItems/1/relationships/owner", false, EndpointKind.Relationship, false)]
37-
[InlineData("PATCH", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, false)]
38-
[InlineData("DELETE", "/todoItems/1", false, EndpointKind.Primary, false)]
39-
[InlineData("DELETE", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, false)]
21+
[InlineData("HEAD", "/todoItems", true, EndpointKind.Primary, null, true)]
22+
[InlineData("HEAD", "/todoItems/1", false, EndpointKind.Primary, null, true)]
23+
[InlineData("HEAD", "/todoItems/1/owner", false, EndpointKind.Secondary, null, true)]
24+
[InlineData("HEAD", "/todoItems/1/tags", true, EndpointKind.Secondary, null, true)]
25+
[InlineData("HEAD", "/todoItems/1/relationships/owner", false, EndpointKind.Relationship, null, true)]
26+
[InlineData("HEAD", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, null, true)]
27+
[InlineData("GET", "/todoItems", true, EndpointKind.Primary, null, true)]
28+
[InlineData("GET", "/todoItems/1", false, EndpointKind.Primary, null, true)]
29+
[InlineData("GET", "/todoItems/1/owner", false, EndpointKind.Secondary, null, true)]
30+
[InlineData("GET", "/todoItems/1/tags", true, EndpointKind.Secondary, null, true)]
31+
[InlineData("GET", "/todoItems/1/relationships/owner", false, EndpointKind.Relationship, null, true)]
32+
[InlineData("GET", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, null, true)]
33+
[InlineData("POST", "/todoItems", false, EndpointKind.Primary, OperationKind.CreateResource, false)]
34+
[InlineData("POST", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, OperationKind.AddToRelationship, false)]
35+
[InlineData("PATCH", "/todoItems/1", false, EndpointKind.Primary, OperationKind.UpdateResource, false)]
36+
[InlineData("PATCH", "/todoItems/1/relationships/owner", false, EndpointKind.Relationship, OperationKind.SetRelationship, false)]
37+
[InlineData("PATCH", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, OperationKind.SetRelationship, false)]
38+
[InlineData("DELETE", "/todoItems/1", false, EndpointKind.Primary, OperationKind.DeleteResource, false)]
39+
[InlineData("DELETE", "/todoItems/1/relationships/tags", true, EndpointKind.Relationship, OperationKind.RemoveFromRelationship, false)]
4040
public async Task Sets_request_properties_correctly(string requestMethod, string requestPath, bool expectIsCollection, EndpointKind expectKind,
41-
bool expectIsReadOnly)
41+
OperationKind? expectOperationKind, bool expectIsReadOnly)
4242
{
4343
// Arrange
4444
var options = new JsonApiOptions
@@ -69,6 +69,7 @@ public async Task Sets_request_properties_correctly(string requestMethod, string
6969
// Assert
7070
request.IsCollection.Should().Be(expectIsCollection);
7171
request.Kind.Should().Be(expectKind);
72+
request.OperationKind.Should().Be(expectOperationKind);
7273
request.IsReadOnly.Should().Be(expectIsReadOnly);
7374
request.PrimaryResource.Should().NotBeNull();
7475
request.PrimaryResource.PublicName.Should().Be("todoItems");

0 commit comments

Comments
 (0)