-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix behaviour on status code requests. #2512
Merged
marabooy
merged 1 commit into
master
from
fix/add-webapi-fix-enablequery-with-status-results
Jul 15, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,7 @@ public override void OnActionExecuting(ActionExecutingContext context) | |
{ | ||
elementType = TypeHelper.GetImplementedIEnumerableType(returnType); | ||
} | ||
else if(TypeHelper.IsGenericType(returnType) && returnType.GetGenericTypeDefinition() == typeof(Task<>)) | ||
else if (TypeHelper.IsGenericType(returnType) && returnType.GetGenericTypeDefinition() == typeof(Task<>)) | ||
{ | ||
elementType = returnType.GetGenericArguments().First(); | ||
} | ||
|
@@ -228,46 +228,44 @@ public override void OnActionExecuted(ActionExecutedContext actionExecutedContex | |
{ | ||
// actionExecutedContext.Result might also indicate a status code that has not yet | ||
// been applied to the result; make sure it's also successful. | ||
StatusCodeResult statusCodeResult = actionExecutedContext.Result as StatusCodeResult; | ||
if (statusCodeResult == null || IsSuccessStatusCode(statusCodeResult.StatusCode)) | ||
ObjectResult responseContent = actionExecutedContext.Result as ObjectResult; | ||
|
||
if (responseContent != null && (responseContent.StatusCode == null || IsSuccessStatusCode(responseContent.StatusCode.Value))) | ||
{ | ||
ObjectResult responseContent = actionExecutedContext.Result as ObjectResult; | ||
if (responseContent != null) | ||
|
||
//throw Error.Argument("actionExecutedContext", SRResources.QueryingRequiresObjectContent, | ||
// actionExecutedContext.Result.GetType().FullName); | ||
Comment on lines
+236
to
+237
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you could remove this commented code. It seems hat it as meant to throw an exception if the result was not an ObjectResult. I wonder why it was commented out. But I think it should be removed if it's not used. |
||
|
||
// Get collection from SingleResult. | ||
IQueryable singleResultCollection = null; | ||
SingleResult singleResult = responseContent.Value as SingleResult; | ||
if (singleResult != null) | ||
{ | ||
// This could be a SingleResult, which has the property Queryable. | ||
// But it could be a SingleResult() or SingleResult<T>. Sort by number of parameters | ||
// on the property and get the one with the most parameters. | ||
PropertyInfo propInfo = responseContent.Value.GetType().GetProperties() | ||
.OrderBy(p => p.GetIndexParameters().Count()) | ||
.Where(p => p.Name.Equals("Queryable")) | ||
.LastOrDefault(); | ||
|
||
singleResultCollection = propInfo.GetValue(singleResult) as IQueryable; | ||
} | ||
|
||
// Execution the action. | ||
object queryResult = OnActionExecuted( | ||
responseContent.Value, | ||
singleResultCollection, | ||
new WebApiActionDescriptor(actionDescriptor as ControllerActionDescriptor), | ||
new WebApiRequestMessage(request), | ||
(elementClrType) => GetModel(elementClrType, request, actionDescriptor), | ||
(queryContext) => CreateAndValidateQueryOptions(request, queryContext), | ||
(statusCode) => actionExecutedContext.Result = new StatusCodeResult((int)statusCode), | ||
(statusCode, message, exception) => actionExecutedContext.Result = CreateBadRequestResult(message, exception)); | ||
|
||
if (queryResult != null) | ||
{ | ||
//throw Error.Argument("actionExecutedContext", SRResources.QueryingRequiresObjectContent, | ||
// actionExecutedContext.Result.GetType().FullName); | ||
|
||
// Get collection from SingleResult. | ||
IQueryable singleResultCollection = null; | ||
SingleResult singleResult = responseContent.Value as SingleResult; | ||
if (singleResult != null) | ||
{ | ||
// This could be a SingleResult, which has the property Queryable. | ||
// But it could be a SingleResult() or SingleResult<T>. Sort by number of parameters | ||
// on the property and get the one with the most parameters. | ||
PropertyInfo propInfo = responseContent.Value.GetType().GetProperties() | ||
.OrderBy(p => p.GetIndexParameters().Count()) | ||
.Where(p => p.Name.Equals("Queryable")) | ||
.LastOrDefault(); | ||
|
||
singleResultCollection = propInfo.GetValue(singleResult) as IQueryable; | ||
} | ||
|
||
// Execution the action. | ||
object queryResult = OnActionExecuted( | ||
responseContent.Value, | ||
singleResultCollection, | ||
new WebApiActionDescriptor(actionDescriptor as ControllerActionDescriptor), | ||
new WebApiRequestMessage(request), | ||
(elementClrType) => GetModel(elementClrType, request, actionDescriptor), | ||
(queryContext) => CreateAndValidateQueryOptions(request, queryContext), | ||
(statusCode) => actionExecutedContext.Result = new StatusCodeResult((int)statusCode), | ||
(statusCode, message, exception) => actionExecutedContext.Result = CreateBadRequestResult(message, exception)); | ||
|
||
if (queryResult != null) | ||
{ | ||
responseContent.Value = queryResult; | ||
} | ||
responseContent.Value = queryResult; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there other return result types other than
ObjectResult
that we should care about? I see thatIStatusCodeActionResult
is the common interface of all action results that have a status code. Why not use that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking further, it seems that there are a few other result types that have a
Value
, such asJsonResult
, and there's alsoContentResult
which hasContent
. Is the assumption here thatObjectResult
is the only one that has aValue
that can be processed by[EnableQuery]
and so we don't care about the rest?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The interface exists for net core 2.2 and above. I just went through the code and patched the behaviour as we don't look at other items such as JsonResult and Content result.