Add test for metadata precedence (#8642)
This commit is contained in:
parent
5656e7f455
commit
6bb292cfcc
|
|
@ -577,16 +577,16 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
bool suppressLinkGeneration,
|
bool suppressLinkGeneration,
|
||||||
bool suppressPathMatching)
|
bool suppressPathMatching)
|
||||||
{
|
{
|
||||||
var metadata = new List<object>
|
var metadata = new List<object>();
|
||||||
{
|
|
||||||
action
|
|
||||||
};
|
|
||||||
|
|
||||||
|
// Add action metadata first so it has a low precedence
|
||||||
if (action.EndpointMetadata != null)
|
if (action.EndpointMetadata != null)
|
||||||
{
|
{
|
||||||
metadata.AddRange(action.EndpointMetadata);
|
metadata.AddRange(action.EndpointMetadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
metadata.Add(action);
|
||||||
|
|
||||||
if (dataTokens != null)
|
if (dataTokens != null)
|
||||||
{
|
{
|
||||||
metadata.Add(new DataTokensMetadata(dataTokens));
|
metadata.Add(new DataTokensMetadata(dataTokens));
|
||||||
|
|
|
||||||
|
|
@ -1305,6 +1305,36 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Endpoints_AttributeRoutes_ActionMetadataDoesNotOverrideDataSourceMetadata()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var actionDescriptorCollection = GetActionDescriptorCollection(
|
||||||
|
CreateActionDescriptor(new { controller = "TestController", action = "TestAction" },
|
||||||
|
"{controller}/{action}/{id?}",
|
||||||
|
new List<object> { new RouteValuesAddressMetadata("fakeroutename", new RouteValueDictionary(new { fake = "Fake!" })) })
|
||||||
|
);
|
||||||
|
var dataSource = CreateMvcEndpointDataSource(actionDescriptorCollection);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var endpoints = dataSource.Endpoints;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Collection(
|
||||||
|
endpoints,
|
||||||
|
(ep) =>
|
||||||
|
{
|
||||||
|
var matcherEndpoint = Assert.IsType<RouteEndpoint>(ep);
|
||||||
|
Assert.Equal("TestController/TestAction/{id?}", matcherEndpoint.RoutePattern.RawText);
|
||||||
|
Assert.Equal(0, matcherEndpoint.Order);
|
||||||
|
|
||||||
|
var routeValuesAddress = matcherEndpoint.Metadata.GetMetadata<IRouteValuesAddressMetadata>();
|
||||||
|
Assert.Equal("{controller}/{action}/{id?}", routeValuesAddress.RouteName);
|
||||||
|
Assert.Equal("TestController", routeValuesAddress.RequiredValues["controller"]);
|
||||||
|
Assert.Equal("TestAction", routeValuesAddress.RequiredValues["action"]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private MvcEndpointDataSource CreateMvcEndpointDataSource(
|
private MvcEndpointDataSource CreateMvcEndpointDataSource(
|
||||||
IActionDescriptorCollectionProvider actionDescriptorCollectionProvider = null,
|
IActionDescriptorCollectionProvider actionDescriptorCollectionProvider = null,
|
||||||
MvcEndpointInvokerFactory mvcEndpointInvokerFactory = null)
|
MvcEndpointInvokerFactory mvcEndpointInvokerFactory = null)
|
||||||
|
|
@ -1381,6 +1411,11 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
actionDescriptors.Add(CreateActionDescriptor(requiredValue, attributeRouteTemplate));
|
actionDescriptors.Add(CreateActionDescriptor(requiredValue, attributeRouteTemplate));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return GetActionDescriptorCollection(actionDescriptors.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
private IActionDescriptorCollectionProvider GetActionDescriptorCollection(params ActionDescriptor[] actionDescriptors)
|
||||||
|
{
|
||||||
var actionDescriptorCollectionProviderMock = new Mock<IActionDescriptorCollectionProvider>();
|
var actionDescriptorCollectionProviderMock = new Mock<IActionDescriptorCollectionProvider>();
|
||||||
actionDescriptorCollectionProviderMock
|
actionDescriptorCollectionProviderMock
|
||||||
.Setup(m => m.ActionDescriptors)
|
.Setup(m => m.ActionDescriptors)
|
||||||
|
|
@ -1388,12 +1423,10 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
return actionDescriptorCollectionProviderMock.Object;
|
return actionDescriptorCollectionProviderMock.Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ActionDescriptor CreateActionDescriptor(string controller, string action, string area = null)
|
private ActionDescriptor CreateActionDescriptor(
|
||||||
{
|
object requiredValues,
|
||||||
return CreateActionDescriptor(new { controller = controller, action = action, area = area }, attributeRouteTemplate: null);
|
string attributeRouteTemplate = null,
|
||||||
}
|
IList<object> metadata = null)
|
||||||
|
|
||||||
private ActionDescriptor CreateActionDescriptor(object requiredValues, string attributeRouteTemplate = null)
|
|
||||||
{
|
{
|
||||||
var actionDescriptor = new ActionDescriptor();
|
var actionDescriptor = new ActionDescriptor();
|
||||||
var routeValues = new RouteValueDictionary(requiredValues);
|
var routeValues = new RouteValueDictionary(requiredValues);
|
||||||
|
|
@ -1409,6 +1442,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
||||||
Template = attributeRouteTemplate
|
Template = attributeRouteTemplate
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
actionDescriptor.EndpointMetadata = metadata;
|
||||||
return actionDescriptor;
|
return actionDescriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue