Skip to content

Commit

Permalink
fixes #986: pass Order value to selector model generated
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Aug 9, 2023
1 parent ddc59f7 commit 8569df8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void ProcessAttributeModel(AttributeRouteModel attributeRouteModel, IEnu
IServiceProvider sp = context.Options.RouteComponents[prefix].ServiceProvider;

SelectorModel newSelectorModel = CreateActionSelectorModel(prefix, model, sp, newRouteTemplate, actionSelector,
attributeRouteModel.Template, actionModel.ActionName, controllerModel.ControllerName);
attributeRouteModel.Template, actionModel.ActionName, controllerModel.ControllerName, attributeRouteModel.Order);
if (newSelectorModel != null)
{
IList<SelectorModel> selectors;
Expand All @@ -158,7 +158,7 @@ private void ProcessAttributeModel(AttributeRouteModel attributeRouteModel, IEnu

private SelectorModel CreateActionSelectorModel(string prefix, IEdmModel model, IServiceProvider sp,
string routeTemplate, SelectorModel actionSelectorModel,
string originalTemplate, string actionName, string controllerName)
string originalTemplate, string actionName, string controllerName, int? order)
{
try
{
Expand All @@ -182,7 +182,8 @@ private SelectorModel CreateActionSelectorModel(string prefix, IEdmModel model,
// replace the attribute routing template using absolute routing template to avoid appending any controller route template
newSelectorModel.AttributeRouteModel = new AttributeRouteModel()
{
Template = $"/{originalTemplate}" // add a "/" to make sure it's absolute template, don't combine with controller
Template = $"/{originalTemplate}", // add a "/" to make sure it's absolute template, don't combine with controller
Order = order
};

return newSelectorModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void AppliesToActionWithLongTemplateWorksAsExpected()
// Assert
SelectorModel actualSelectorModel = Assert.Single(action.Selectors);
Assert.Equal("/Customers({key})/Orders({relatedKey})/NS.MyOrder/Title", actualSelectorModel.AttributeRouteModel.Template);
Assert.Null(actualSelectorModel.AttributeRouteModel.Order);
Assert.Contains(actualSelectorModel.EndpointMetadata, a => a is ODataRoutingMetadata);
}

Expand Down Expand Up @@ -144,21 +145,74 @@ public void AppliesToActionWithRoutePrefixWorksAsExpected()
e =>
{
Assert.Equal("/Customers/{key}", e.AttributeRouteModel.Template);
Assert.Equal(9, e.AttributeRouteModel.Order);
Assert.Contains(e.EndpointMetadata, a => a is ODataRoutingMetadata);
},
e =>
{
Assert.Equal("/Orders/{key}", e.AttributeRouteModel.Template);
Assert.Equal(9, e.AttributeRouteModel.Order);
Assert.Contains(e.EndpointMetadata, a => a is ODataRoutingMetadata);
},
e =>
{
Assert.Equal("/Customers", e.AttributeRouteModel.Template);
Assert.Equal(3, e.AttributeRouteModel.Order);
Assert.Contains(e.EndpointMetadata, a => a is ODataRoutingMetadata);
},
e =>
{
Assert.Equal("/Orders", e.AttributeRouteModel.Template);
Assert.Equal(3, e.AttributeRouteModel.Order);
Assert.Contains(e.EndpointMetadata, a => a is ODataRoutingMetadata);
});
}

[Fact]
public void AppliesToActionWithOrderOnControllerRoutePrefixWorksAsExpected()
{
// Arrange
ControllerModel controller = ControllerModelHelpers.BuildControllerModel<WithPrefixController2>("List");
ActionModel action = controller.Actions.First();
Assert.Equal(2, action.Selectors.Count);

ODataControllerActionContext context = new ODataControllerActionContext(string.Empty, _edmModel, controller)
{
Action = action,
Options = _options,
};

AttributeRoutingConvention attributeConvention = CreateConvention();

// Act
bool ok = _attributeConvention.AppliesToAction(context);
Assert.False(ok);

// Assert
Assert.Equal(4, action.Selectors.Count);
Assert.Collection(action.Selectors,
e =>
{
Assert.Equal("/Customers/{key}", e.AttributeRouteModel.Template);
Assert.Equal(9, e.AttributeRouteModel.Order); // Order from controller
Assert.Contains(e.EndpointMetadata, a => a is ODataRoutingMetadata);
},
e =>
{
Assert.Equal("/Orders/{key}", e.AttributeRouteModel.Template);
Assert.Equal(8, e.AttributeRouteModel.Order); // Order from controller
Assert.Contains(e.EndpointMetadata, a => a is ODataRoutingMetadata);
},
e =>
{
Assert.Equal("/Customers", e.AttributeRouteModel.Template);
Assert.Equal(3, e.AttributeRouteModel.Order); // Order from action
Assert.Contains(e.EndpointMetadata, a => a is ODataRoutingMetadata);
},
e =>
{
Assert.Equal("/Orders", e.AttributeRouteModel.Template);
Assert.Equal(3, e.AttributeRouteModel.Order); // Order from action
Assert.Contains(e.EndpointMetadata, a => a is ODataRoutingMetadata);
});
}
Expand Down Expand Up @@ -284,13 +338,25 @@ public void LongAction()
[Route("Orders")]
private class WithPrefixController
{
[HttpGet("{key}")]
[HttpPost("")]
[HttpGet("{key}", Order = 9)]
[HttpPost("", Order = 3)]
public void List(int key)
{
}
}

[ODataAttributeRouting] // using this attribute if not derived from ODataController
[Route("Customers", Order = 9)]
[Route("Orders", Order = 8)]
private class WithPrefixController2
{
[HttpGet("{key}")]
[HttpPost("", Order = 3)] // 3 should override 8 on controller
public void List()
{
}
}

[Route("VipCustomer")]
public class SingletonTestControllerWithPrefix
{
Expand Down

0 comments on commit 8569df8

Please sign in to comment.