Skip to content

Commit

Permalink
Added support for "~/entityset/cast" and "~/entityset/key/cast" routi…
Browse files Browse the repository at this point in the history
…ng conventions

Fixes #738
  • Loading branch information
icnocop authored and commonsensesoftware committed Jul 1, 2022
1 parent 88cb546 commit 0a43c62
Showing 1 changed file with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ internal ODataRouteActionType GetActionType( ControllerActionDescriptor action )
}
else if ( Operation == null )
{
if ( IsActionOrFunction( EntitySet, Singleton, action.ActionName, GetHttpMethods( action ) ) )
var httpMethods = GetHttpMethods( action );

if ( IsCast( EdmModel, EntitySet, action.ActionName, httpMethods ) )
{
return ODataRouteActionType.EntitySet;
}
else if ( IsActionOrFunction( EntitySet, Singleton, action.ActionName, httpMethods ) )
{
return ODataRouteActionType.Unknown;
}
Expand All @@ -122,6 +128,52 @@ internal ODataRouteActionType GetActionType( ControllerActionDescriptor action )
return ODataRouteActionType.UnboundOperation;
}

static bool IsCast( IEdmModel model, IEdmEntitySet? entitySet, string actionName, IEnumerable<string> methods )
{
using var iterator = methods.GetEnumerator();

if ( !iterator.MoveNext() )
{
return false;
}

var method = iterator.Current;

if ( iterator.MoveNext() )
{
return false;
}

if ( entitySet == null )
{
return false;
}

var entity = entitySet.EntityType();

const string ActionMethod = "Post";
const string FunctionMethod = "Get";

if ( ( FunctionMethod.Equals( method, OrdinalIgnoreCase ) ||
ActionMethod.Equals( method, OrdinalIgnoreCase ) ) &&
actionName != ActionMethod )
{
foreach ( var derivedType in model.FindAllDerivedTypes( entity ).OfType<EdmEntityType>() )
{
var fromTypeName = "From" + derivedType.Name;

if ( actionName.StartsWith( method + fromTypeName, OrdinalIgnoreCase ) ||
actionName.StartsWith( method + entitySet.Name + fromTypeName, OrdinalIgnoreCase ) ||
actionName.StartsWith( method + derivedType.Name, OrdinalIgnoreCase ) )
{
return true;
}
}
}

return false;
}

// Slash became the default 4/18/2018
// REF: https://github.com/OData/WebApi/pull/1393
static ODataUrlKeyDelimiter UrlKeyDelimiterOrDefault( ODataUrlKeyDelimiter? urlKeyDelimiter ) => urlKeyDelimiter ?? Slash;
Expand Down

0 comments on commit 0a43c62

Please sign in to comment.