Merge branch 'actionmodel1495c' into dev

This commit is contained in:
ianhong 2014-11-18 16:59:01 -08:00
commit f470fa6d74
9 changed files with 50 additions and 42 deletions

View File

@ -9,12 +9,13 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
{ {
public class ActionModel public class ActionModel
{ {
public ActionModel([NotNull] MethodInfo actionMethod) public ActionModel([NotNull] MethodInfo actionMethod,
[NotNull] IReadOnlyList<object> attributes)
{ {
ActionMethod = actionMethod; ActionMethod = actionMethod;
ApiExplorer = new ApiExplorerModel(); ApiExplorer = new ApiExplorerModel();
Attributes = new List<object>(); Attributes = new List<object>(attributes);
ActionConstraints = new List<IActionConstraintMetadata>(); ActionConstraints = new List<IActionConstraintMetadata>();
Filters = new List<IFilter>(); Filters = new List<IFilter>();
HttpMethods = new List<string>(); HttpMethods = new List<string>();
@ -45,9 +46,10 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
AttributeRouteModel = new AttributeRouteModel(other.AttributeRouteModel); AttributeRouteModel = new AttributeRouteModel(other.AttributeRouteModel);
} }
} }
public List<IActionConstraintMetadata> ActionConstraints { get; private set; } public List<IActionConstraintMetadata> ActionConstraints { get; private set; }
public MethodInfo ActionMethod { get; private set; } public MethodInfo ActionMethod { get; }
public string ActionName { get; set; } public string ActionName { get; set; }
@ -60,7 +62,7 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
/// </remarks> /// </remarks>
public ApiExplorerModel ApiExplorer { get; set; } public ApiExplorerModel ApiExplorer { get; set; }
public List<object> Attributes { get; private set; } public IReadOnlyList<object> Attributes { get; }
public ControllerModel Controller { get; set; } public ControllerModel Controller { get; set; }

View File

@ -9,13 +9,14 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
{ {
public class ControllerModel public class ControllerModel
{ {
public ControllerModel([NotNull] TypeInfo controllerType) public ControllerModel([NotNull] TypeInfo controllerType,
[NotNull] IReadOnlyList<object> attributes)
{ {
ControllerType = controllerType; ControllerType = controllerType;
Actions = new List<ActionModel>(); Actions = new List<ActionModel>();
ApiExplorer = new ApiExplorerModel(); ApiExplorer = new ApiExplorerModel();
Attributes = new List<object>(); Attributes = new List<object>(attributes);
AttributeRoutes = new List<AttributeRouteModel>(); AttributeRoutes = new List<AttributeRouteModel>();
ActionConstraints = new List<IActionConstraintMetadata>(); ActionConstraints = new List<IActionConstraintMetadata>();
Filters = new List<IFilter>(); Filters = new List<IFilter>();
@ -56,7 +57,7 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public List<AttributeRouteModel> AttributeRoutes { get; private set; } public List<AttributeRouteModel> AttributeRoutes { get; private set; }
public List<object> Attributes { get; private set; } public IReadOnlyList<object> Attributes { get; }
public string ControllerName { get; set; } public string ControllerName { get; set; }

View File

@ -213,13 +213,11 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
[NotNull] MethodInfo methodInfo, [NotNull] MethodInfo methodInfo,
[NotNull] IReadOnlyList<object> attributes) [NotNull] IReadOnlyList<object> attributes)
{ {
var actionModel = new ActionModel(methodInfo) var actionModel = new ActionModel(methodInfo, attributes)
{ {
IsActionNameMatchRequired = true, IsActionNameMatchRequired = true,
}; };
actionModel.Attributes.AddRange(attributes);
actionModel.ActionConstraints.AddRange(attributes.OfType<IActionConstraintMetadata>()); actionModel.ActionConstraints.AddRange(attributes.OfType<IActionConstraintMetadata>());
actionModel.Filters.AddRange(attributes.OfType<IFilter>()); actionModel.Filters.AddRange(attributes.OfType<IFilter>());
@ -268,12 +266,10 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
/// <returns>A <see cref="ParameterModel"/> for the given <see cref="ParameterInfo"/>.</returns> /// <returns>A <see cref="ParameterModel"/> for the given <see cref="ParameterInfo"/>.</returns>
protected virtual ParameterModel CreateParameterModel([NotNull] ParameterInfo parameterInfo) protected virtual ParameterModel CreateParameterModel([NotNull] ParameterInfo parameterInfo)
{ {
var parameterModel = new ParameterModel(parameterInfo);
// CoreCLR returns IEnumerable<Attribute> from GetCustomAttributes - the OfType<object> // CoreCLR returns IEnumerable<Attribute> from GetCustomAttributes - the OfType<object>
// is needed to so that the result of ToArray() is object // is needed to so that the result of ToArray() is object
var attributes = parameterInfo.GetCustomAttributes(inherit: true).OfType<object>().ToArray(); var attributes = parameterInfo.GetCustomAttributes(inherit: true).OfType<object>().ToArray();
parameterModel.Attributes.AddRange(attributes); var parameterModel = new ParameterModel(parameterInfo, attributes);
parameterModel.BinderMetadata = attributes.OfType<IBinderMetadata>().FirstOrDefault(); parameterModel.BinderMetadata = attributes.OfType<IBinderMetadata>().FirstOrDefault();

View File

@ -88,12 +88,10 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
/// <returns>A <see cref="ControllerModel"/> for the given <see cref="TypeInfo"/>.</returns> /// <returns>A <see cref="ControllerModel"/> for the given <see cref="TypeInfo"/>.</returns>
protected virtual ControllerModel CreateControllerModel([NotNull] TypeInfo typeInfo) protected virtual ControllerModel CreateControllerModel([NotNull] TypeInfo typeInfo)
{ {
var controllerModel = new ControllerModel(typeInfo);
// CoreCLR returns IEnumerable<Attribute> from GetCustomAttributes - the OfType<object> // CoreCLR returns IEnumerable<Attribute> from GetCustomAttributes - the OfType<object>
// is needed to so that the result of ToArray() is object // is needed to so that the result of ToArray() is object
var attributes = typeInfo.GetCustomAttributes(inherit: true).OfType<object>().ToArray(); var attributes = typeInfo.GetCustomAttributes(inherit: true).OfType<object>().ToArray();
controllerModel.Attributes.AddRange(attributes); var controllerModel = new ControllerModel(typeInfo, attributes);
controllerModel.ControllerName = controllerModel.ControllerName =
typeInfo.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) ? typeInfo.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) ?

View File

@ -9,11 +9,12 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
{ {
public class ParameterModel public class ParameterModel
{ {
public ParameterModel(ParameterInfo parameterInfo) public ParameterModel([NotNull] ParameterInfo parameterInfo,
[NotNull] IReadOnlyList<object> attributes)
{ {
ParameterInfo = parameterInfo; ParameterInfo = parameterInfo;
Attributes = new List<object>(); Attributes = new List<object>(attributes);
} }
public ParameterModel([NotNull] ParameterModel other) public ParameterModel([NotNull] ParameterModel other)
@ -28,7 +29,7 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public ActionModel Action { get; set; } public ActionModel Action { get; set; }
public List<object> Attributes { get; private set; } public IReadOnlyList<object> Attributes { get; }
public IBinderMetadata BinderMetadata { get; set; } public IBinderMetadata BinderMetadata { get; set; }

View File

@ -14,9 +14,11 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public void CopyConstructor_DoesDeepCopyOfOtherModels() public void CopyConstructor_DoesDeepCopyOfOtherModels()
{ {
// Arrange // Arrange
var action = new ActionModel(typeof(TestController).GetMethod("Edit")); var action = new ActionModel(typeof(TestController).GetMethod("Edit"),
new List<object>());
var parameter = new ParameterModel(action.ActionMethod.GetParameters()[0]); var parameter = new ParameterModel(action.ActionMethod.GetParameters()[0],
new List<object>());
parameter.Action = action; parameter.Action = action;
action.Parameters.Add(parameter); action.Parameters.Add(parameter);
@ -40,13 +42,14 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public void CopyConstructor_CopiesAllProperties() public void CopyConstructor_CopiesAllProperties()
{ {
// Arrange // Arrange
var action = new ActionModel(typeof(TestController).GetMethod("Edit")); var action = new ActionModel(typeof(TestController).GetMethod("Edit"),
new List<object>() { new HttpGetAttribute() });
action.ActionConstraints.Add(new HttpMethodConstraint(new string[] { "GET" })); action.ActionConstraints.Add(new HttpMethodConstraint(new string[] { "GET" }));
action.ActionName = "Edit"; action.ActionName = "Edit";
action.Attributes.Add(new HttpGetAttribute()); action.Controller = new ControllerModel(typeof(TestController).GetTypeInfo(),
action.Controller = new ControllerModel(typeof(TestController).GetTypeInfo()); new List<object>());
action.Filters.Add(new AuthorizeAttribute()); action.Filters.Add(new AuthorizeAttribute());
action.HttpMethods.Add("GET"); action.HttpMethods.Add("GET");
action.IsActionNameMatchRequired = true; action.IsActionNameMatchRequired = true;

View File

@ -14,9 +14,11 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public void CopyConstructor_DoesDeepCopyOfOtherModels() public void CopyConstructor_DoesDeepCopyOfOtherModels()
{ {
// Arrange // Arrange
var controller = new ControllerModel(typeof(TestController).GetTypeInfo()); var controller = new ControllerModel(typeof(TestController).GetTypeInfo(),
new List<object>());
var action = new ActionModel(typeof(TestController).GetMethod("Edit")); var action = new ActionModel(typeof(TestController).GetMethod("Edit"),
new List<object>());
controller.Actions.Add(action); controller.Actions.Add(action);
action.Controller = controller; action.Controller = controller;
@ -46,11 +48,11 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public void CopyConstructor_CopiesAllProperties() public void CopyConstructor_CopiesAllProperties()
{ {
// Arrange // Arrange
var controller = new ControllerModel(typeof(TestController).GetTypeInfo()); var controller = new ControllerModel(typeof(TestController).GetTypeInfo(),
new List<object>() { new HttpGetAttribute() });
controller.ActionConstraints.Add(new HttpMethodConstraint(new string[] { "GET" })); controller.ActionConstraints.Add(new HttpMethodConstraint(new string[] { "GET" }));
controller.Application = new ApplicationModel(); controller.Application = new ApplicationModel();
controller.Attributes.Add(new HttpGetAttribute());
controller.ControllerName = "cool"; controller.ControllerName = "cool";
controller.Filters.Add(new AuthorizeAttribute()); controller.Filters.Add(new AuthorizeAttribute());
controller.RouteConstraints.Add(new AreaAttribute("Admin")); controller.RouteConstraints.Add(new AreaAttribute("Admin"));

View File

@ -14,10 +14,10 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public void CopyConstructor_CopiesAllProperties() public void CopyConstructor_CopiesAllProperties()
{ {
// Arrange // Arrange
var parameter = new ParameterModel(typeof(TestController).GetMethod("Edit").GetParameters()[0]); var parameter = new ParameterModel(typeof(TestController).GetMethod("Edit").GetParameters()[0],
new List<object>() { new FromBodyAttribute() });
parameter.Action = new ActionModel(typeof(TestController).GetMethod("Edit")); parameter.Action = new ActionModel(typeof(TestController).GetMethod("Edit"), new List<object>());
parameter.Attributes.Add(new FromBodyAttribute());
parameter.BinderMetadata = (IBinderMetadata)parameter.Attributes[0]; parameter.BinderMetadata = (IBinderMetadata)parameter.Attributes[0];
parameter.IsOptional = true; parameter.IsOptional = true;
parameter.ParameterName = "id"; parameter.ParameterName = "id";

View File

@ -1137,21 +1137,26 @@ namespace Microsoft.AspNet.Mvc.Test
var options = new MockMvcOptionsAccessor(); var options = new MockMvcOptionsAccessor();
options.Options.ApplicationModelConventions.Add(applicationConvention.Object); options.Options.ApplicationModelConventions.Add(applicationConvention.Object);
var provider = GetProvider(typeof(ConventionsController).GetTypeInfo(), options); var applicationModel = new ApplicationModel();
var model = provider.BuildModel(); var controller = new ControllerModel(typeof(ConventionsController).GetTypeInfo(),
new List<object>() { controllerConvention.Object });
controller.Application = applicationModel;
applicationModel.Controllers.Add(controller);
var controller = model.Controllers.Single(); var methodInfo = typeof(ConventionsController).GetMethod("Create");
controller.Attributes.Add(controllerConvention.Object); var actionModel = new ActionModel(methodInfo, new List<object>() { actionConvention.Object });
actionModel.Controller = controller;
controller.Actions.Add(actionModel);
var action = controller.Actions.Single(); var parameterInfo = actionModel.ActionMethod.GetParameters().Single();
action.Attributes.Add(actionConvention.Object); var parameterModel = new ParameterModel(parameterInfo,
new List<object>() { parameterConvention.Object });
var parameter = action.Parameters.Single(); parameterModel.Action = actionModel;
parameter.Attributes.Add(parameterConvention.Object); actionModel.Parameters.Add(parameterModel);
// Act // Act
ApplicationModelConventions.ApplyConventions(model, options.Options.ApplicationModelConventions); ApplicationModelConventions.ApplyConventions(applicationModel, options.Options.ApplicationModelConventions);
// Assert // Assert
Assert.Equal(4, sequence); Assert.Equal(4, sequence);