Skip to content

Commit 399dcdc

Browse files
committed
fix: edge cases
1 parent 0f90ddf commit 399dcdc

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/JsonApiDotNetCore/Middleware/IncomingTypeMatchFilter.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
2+
using System.Collections;
23
using System.Linq;
34
using System.Net.Http;
45
using JsonApiDotNetCore.Configuration;
5-
using JsonApiDotNetCore.Controllers;
66
using JsonApiDotNetCore.Errors;
7-
using JsonApiDotNetCore.Resources;
87
using Microsoft.AspNetCore.Http;
98
using Microsoft.AspNetCore.Mvc.Filters;
9+
using Microsoft.EntityFrameworkCore.Internal;
1010

1111
namespace JsonApiDotNetCore.Middleware
1212
{
@@ -37,7 +37,7 @@ public void OnActionExecuting(ActionExecutingContext context)
3737

3838
if (request.Method == HttpMethods.Patch || request.Method == HttpMethods.Post)
3939
{
40-
var deserializedType = context.ActionArguments.LastOrDefault().Value?.GetType();
40+
var deserializedType = GetDeserializedType(context);
4141
var expectedType = GetExpectedType();
4242

4343
if (deserializedType != null && expectedType != null && deserializedType != expectedType)
@@ -50,16 +50,26 @@ public void OnActionExecuting(ActionExecutingContext context)
5050
}
5151
}
5252

53-
private Type GetExpectedType()
53+
private Type GetDeserializedType(ActionExecutingContext context)
5454
{
55-
if (_jsonApiRequest.Kind == EndpointKind.Primary)
55+
var deserializedValue = context.ActionArguments.LastOrDefault().Value;
56+
57+
if (deserializedValue is IList resourceCollection && resourceCollection.Any())
5658
{
57-
return _jsonApiRequest.PrimaryResource?.GetType();
59+
return resourceCollection[0].GetType();
5860
}
59-
else
61+
62+
return deserializedValue?.GetType();
63+
}
64+
65+
private Type GetExpectedType()
66+
{
67+
if (_jsonApiRequest.Kind == EndpointKind.Primary)
6068
{
61-
return _jsonApiRequest.SecondaryResource?.GetType();
69+
return _jsonApiRequest.PrimaryResource.ResourceType;
6270
}
71+
72+
return _jsonApiRequest.SecondaryResource?.ResourceType;
6373
}
6474

6575
public void OnActionExecuted(ActionExecutedContext context) { /* noop */ }

0 commit comments

Comments
 (0)