Repository navigation
issue #568: Error when function/action name contains keyword "On" #576
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -65,31 +65,39 @@ protected void ProcessOperations(ODataControllerActionContext context, IEdmEntit | |||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // OperationNameOnCollectionOfEntityType | ||||||||||||
| string operationName = SplitActionName(actionName, out string cast, out bool isOnCollection); | ||||||||||||
|
|
||||||||||||
| bool isOnCollection = false; | ||||||||||||
| IEdmEntityType castTypeFromActionName = null; | ||||||||||||
| if (cast != null) | ||||||||||||
|
|
||||||||||||
| // Let's find the operations using the action first, | ||||||||||||
| // If not founding, let's split the action name, search again | ||||||||||||
| IEdmOperation[] candidates = FindCandidates(context, actionName); | ||||||||||||
|
|
||||||||||||
| if (candidates.Length == 0) | ||||||||||||
| { | ||||||||||||
| if (cast.Length == 0) | ||||||||||||
| { | ||||||||||||
| // Early return for the following cases: | ||||||||||||
| // - {OperationName}On | ||||||||||||
| // - {OperationName}OnCollectionOf | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| // OperationNameOnCollectionOfEntityType | ||||||||||||
| string operationName = SplitActionName(actionName, out string cast, out isOnCollection); | ||||||||||||
|
|
||||||||||||
| castTypeFromActionName = entityType.FindTypeInInheritance(context.Model, cast, context.Options?.RouteOptions?.EnableActionNameCaseInsensitive == true) as IEdmEntityType; | ||||||||||||
| if (castTypeFromActionName == null) | ||||||||||||
| castTypeFromActionName = null; | ||||||||||||
| if (cast != null) | ||||||||||||
|
Contributor
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. Do I understand correctly that these early
Contributor
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.
If that's really the case, it would be nice to avoid making this change in the same PR IMHO. Or at the very least, the change should be done in a separate commit in the branch. |
||||||||||||
| { | ||||||||||||
| return; | ||||||||||||
| if (cast.Length == 0) | ||||||||||||
| { | ||||||||||||
| // Early return for the following cases: | ||||||||||||
| // - {OperationName}On | ||||||||||||
| // - {OperationName}OnCollectionOf | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| castTypeFromActionName = entityType.FindTypeInInheritance(context.Model, cast, context.Options?.RouteOptions?.EnableActionNameCaseInsensitive == true) as IEdmEntityType; | ||||||||||||
| if (castTypeFromActionName == null) | ||||||||||||
| { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| candidates = FindCandidates(context, operationName); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // TODO: refactor here | ||||||||||||
| // If we have multiple same function defined, we should match the best one? | ||||||||||||
| StringComparison actionNameComparison = context.Options?.RouteOptions?.EnableActionNameCaseInsensitive == true ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; | ||||||||||||
| IEnumerable<IEdmOperation> candidates = context.Model.SchemaElements.OfType<IEdmOperation>().Where(f => f.IsBound && f.Name.Equals(operationName, actionNameComparison)); | ||||||||||||
| foreach (IEdmOperation edmOperation in candidates) | ||||||||||||
| { | ||||||||||||
| IEdmOperationParameter bindingParameter = edmOperation.Parameters.FirstOrDefault(); | ||||||||||||
|
|
@@ -172,6 +180,15 @@ protected void ProcessOperations(ODataControllerActionContext context, IEdmEntit | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| private static IEdmOperation[] FindCandidates(ODataControllerActionContext context, string operationName) | ||||||||||||
| { | ||||||||||||
| // TODO: refactor here | ||||||||||||
| // If we have multiple same function defined, we should match the best one? | ||||||||||||
|
|
||||||||||||
| StringComparison actionNameComparison = context.Options?.RouteOptions?.EnableActionNameCaseInsensitive == true ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; | ||||||||||||
| return context.Model.SchemaElements.OfType<IEdmOperation>().Where(f => f.IsBound && f.Name.Equals(operationName, actionNameComparison)).ToArray(); | ||||||||||||
|
Contributor
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. Why
Contributor
Author
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. we will do use it:
Contributor
Author
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. #Resolved.
Contributor
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. Long lines like this are really hard to read. I'd suggest reformatting like:
Suggested change
Contributor
Author
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. #Resolved. |
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// <summary> | ||||||||||||
| /// Split the action based on supporting pattern. | ||||||||||||
| /// </summary> | ||||||||||||
|
|
@@ -190,7 +207,7 @@ internal static string SplitActionName(string actionName, out string cast, out b | |||||||||||
| cast = null; | ||||||||||||
| isOnCollection = false; | ||||||||||||
| string operation; | ||||||||||||
| int index = actionName.IndexOf("OnCollectionOf", StringComparison.Ordinal); | ||||||||||||
| int index = actionName.LastIndexOf("OnCollectionOf", StringComparison.Ordinal); | ||||||||||||
|
Contributor
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. Do these comparisons also need to be updated to honor
Contributor
Author
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. #Resolved. |
||||||||||||
| if (index > 0) | ||||||||||||
| { | ||||||||||||
| operation = actionName.Substring(0, index); | ||||||||||||
|
|
@@ -199,7 +216,7 @@ internal static string SplitActionName(string actionName, out string cast, out b | |||||||||||
| return operation; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| index = actionName.IndexOf("On", StringComparison.Ordinal); | ||||||||||||
| index = actionName.LastIndexOf("On", StringComparison.Ordinal); | ||||||||||||
| if (index > 0) | ||||||||||||
| { | ||||||||||||
| operation = actionName.Substring(0, index); | ||||||||||||
|
|
||||||||||||
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.
These comments imply to me we have 2 strategies, and a fallback/priotity between them.
It would be nice to model these as 2 distinct functions and then have an explicit fallback using
??or even a small chain of responsibility implementation (though that might be a bit overkill here).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.
Refactor codes, please take a look. Thanks.