1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Linq ;
3
4
using System . Net . Http ;
4
5
using JsonApiDotNetCore . Configuration ;
5
- using JsonApiDotNetCore . Controllers ;
6
6
using JsonApiDotNetCore . Errors ;
7
- using JsonApiDotNetCore . Resources ;
8
7
using Microsoft . AspNetCore . Http ;
9
8
using Microsoft . AspNetCore . Mvc . Filters ;
9
+ using Microsoft . EntityFrameworkCore . Internal ;
10
10
11
11
namespace JsonApiDotNetCore . Middleware
12
12
{
@@ -37,7 +37,7 @@ public void OnActionExecuting(ActionExecutingContext context)
37
37
38
38
if ( request . Method == HttpMethods . Patch || request . Method == HttpMethods . Post )
39
39
{
40
- var deserializedType = context . ActionArguments . LastOrDefault ( ) . Value ? . GetType ( ) ;
40
+ var deserializedType = GetDeserializedType ( context ) ;
41
41
var expectedType = GetExpectedType ( ) ;
42
42
43
43
if ( deserializedType != null && expectedType != null && deserializedType != expectedType )
@@ -50,16 +50,26 @@ public void OnActionExecuting(ActionExecutingContext context)
50
50
}
51
51
}
52
52
53
- private Type GetExpectedType ( )
53
+ private Type GetDeserializedType ( ActionExecutingContext context )
54
54
{
55
- if ( _jsonApiRequest . Kind == EndpointKind . Primary )
55
+ var deserializedValue = context . ActionArguments . LastOrDefault ( ) . Value ;
56
+
57
+ if ( deserializedValue is IList resourceCollection && resourceCollection . Any ( ) )
56
58
{
57
- return _jsonApiRequest . PrimaryResource ? . GetType ( ) ;
59
+ return resourceCollection [ 0 ] . GetType ( ) ;
58
60
}
59
- else
61
+
62
+ return deserializedValue ? . GetType ( ) ;
63
+ }
64
+
65
+ private Type GetExpectedType ( )
66
+ {
67
+ if ( _jsonApiRequest . Kind == EndpointKind . Primary )
60
68
{
61
- return _jsonApiRequest . SecondaryResource ? . GetType ( ) ;
69
+ return _jsonApiRequest . PrimaryResource . ResourceType ;
62
70
}
71
+
72
+ return _jsonApiRequest . SecondaryResource ? . ResourceType ;
63
73
}
64
74
65
75
public void OnActionExecuted ( ActionExecutedContext context ) { /* noop */ }
0 commit comments