Skip to content

Commit 0f90ddf

Browse files
committed
fix: tests
1 parent 8a94e3f commit 0f90ddf

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/JsonApiDotNetCore/Middleware/IncomingTypeMatchFilter.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,31 @@ public void OnActionExecuting(ActionExecutingContext context)
3737

3838
if (request.Method == HttpMethods.Patch || request.Method == HttpMethods.Post)
3939
{
40-
var deserializedType = context.ActionArguments.Last().Value.GetType();
41-
var targetType = (_jsonApiRequest.SecondaryResource ?? _jsonApiRequest.PrimaryResource).ResourceType;
40+
var deserializedType = context.ActionArguments.LastOrDefault().Value?.GetType();
41+
var expectedType = GetExpectedType();
4242

43-
if (deserializedType != targetType)
43+
if (deserializedType != null && expectedType != null && deserializedType != expectedType)
4444
{
45-
ResourceContext resourceFromEndpoint = _provider.GetResourceContext(targetType);
45+
ResourceContext resourceFromEndpoint = _provider.GetResourceContext(expectedType);
4646
ResourceContext resourceFromBody = _provider.GetResourceContext(deserializedType);
4747

4848
throw new ResourceTypeMismatchException(new HttpMethod(request.Method), request.Path, resourceFromEndpoint, resourceFromBody);
4949
}
5050
}
5151
}
52-
52+
53+
private Type GetExpectedType()
54+
{
55+
if (_jsonApiRequest.Kind == EndpointKind.Primary)
56+
{
57+
return _jsonApiRequest.PrimaryResource?.GetType();
58+
}
59+
else
60+
{
61+
return _jsonApiRequest.SecondaryResource?.GetType();
62+
}
63+
}
64+
5365
public void OnActionExecuted(ActionExecutedContext context) { /* noop */ }
5466
}
5567
}

0 commit comments

Comments
 (0)