11using System ;
2+ using System . Collections ;
23using System . Linq ;
34using System . Net . Http ;
45using JsonApiDotNetCore . Configuration ;
5- using JsonApiDotNetCore . Controllers ;
66using JsonApiDotNetCore . Errors ;
7- using JsonApiDotNetCore . Resources ;
87using Microsoft . AspNetCore . Http ;
98using Microsoft . AspNetCore . Mvc . Filters ;
9+ using Microsoft . EntityFrameworkCore . Internal ;
1010
1111namespace 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