Use default values when binding action arguments

Fixes #545
This commit is contained in:
Pranav K 2014-05-22 10:34:16 -07:00
parent 1b7d52c84c
commit 822d84a2b4
3 changed files with 212 additions and 26 deletions

View File

@ -261,7 +261,7 @@ namespace Microsoft.AspNet.Mvc
await InvokeActionMethodFilter(); await InvokeActionMethodFilter();
} }
private async Task<IDictionary<string, object>> GetActionArguments(ModelStateDictionary modelState) internal async Task<IDictionary<string, object>> GetActionArguments(ModelStateDictionary modelState)
{ {
var actionBindingContext = await _bindingProvider.GetActionBindingContextAsync(_actionContext); var actionBindingContext = await _bindingProvider.GetActionBindingContextAsync(_actionContext);
var parameters = _descriptor.Parameters; var parameters = _descriptor.Parameters;
@ -304,8 +304,10 @@ namespace Microsoft.AspNet.Mvc
HttpContext = actionBindingContext.ActionContext.HttpContext, HttpContext = actionBindingContext.ActionContext.HttpContext,
FallbackToEmptyPrefix = true FallbackToEmptyPrefix = true
}; };
await actionBindingContext.ModelBinder.BindModelAsync(modelBindingContext); if (await actionBindingContext.ModelBinder.BindModelAsync(modelBindingContext))
parameterValues[parameter.Name] = modelBindingContext.Model; {
parameterValues[parameter.Name] = modelBindingContext.Model;
}
} }
} }

View File

@ -5,8 +5,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
@ -1319,6 +1321,168 @@ namespace Microsoft.AspNet.Mvc
return invoker; return invoker;
} }
[Fact]
public async Task GetActionArguments_DoesNotAddActionArgumentsToModelStateDictionary_IfBinderReturnsFalse()
{
// Arrange
Func<object, int> method = x => 1;
var actionDescriptor = new ReflectedActionDescriptor
{
MethodInfo = method.Method,
Parameters = new List<ParameterDescriptor>
{
new ParameterDescriptor
{
Name = "foo",
ParameterBindingInfo = new ParameterBindingInfo("foo", typeof(object))
}
}
};
var binder = new Mock<IModelBinder>();
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
.Returns(Task.FromResult(result: false));
var actionContext = new ActionContext(new RouteContext(Mock.Of<HttpContext>()),
actionDescriptor);
var bindingContext = new ActionBindingContext(actionContext,
Mock.Of<IModelMetadataProvider>(),
binder.Object,
Mock.Of<IValueProvider>(),
Mock.Of<IInputFormatterProvider>(),
Enumerable.Empty<IModelValidatorProvider>());
var actionBindingContextProvider = new Mock<IActionBindingContextProvider>();
actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny<ActionContext>()))
.Returns(Task.FromResult(bindingContext));
var invoker = new ReflectedActionInvoker(actionContext,
actionDescriptor,
Mock.Of<IControllerFactory>(),
actionBindingContextProvider.Object,
Mock.Of<INestedProviderManager<FilterProviderContext>>());
var modelStateDictionary = new ModelStateDictionary();
// Act
var result = await invoker.GetActionArguments(modelStateDictionary);
// Assert
Assert.Empty(result);
}
[Fact]
public async Task GetActionArguments_AddsActionArgumentsToModelStateDictionary_IfBinderReturnsTrue()
{
// Arrange
Func<object, int> method = x => 1;
var actionDescriptor = new ReflectedActionDescriptor
{
MethodInfo = method.Method,
Parameters = new List<ParameterDescriptor>
{
new ParameterDescriptor
{
Name = "foo",
ParameterBindingInfo = new ParameterBindingInfo("foo", typeof(object))
}
}
};
var value = "Hello world";
var binder = new Mock<IModelBinder>();
var metadataProvider = new EmptyModelMetadataProvider();
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
.Callback((ModelBindingContext context) =>
{
context.ModelMetadata = metadataProvider.GetMetadataForType(modelAccessor: null,
modelType: typeof(string));
context.Model = value;
})
.Returns(Task.FromResult(result: true));
var actionContext = new ActionContext(new RouteContext(Mock.Of<HttpContext>()),
actionDescriptor);
var bindingContext = new ActionBindingContext(actionContext,
Mock.Of<IModelMetadataProvider>(),
binder.Object,
Mock.Of<IValueProvider>(),
Mock.Of<IInputFormatterProvider>(),
Enumerable.Empty<IModelValidatorProvider>());
var actionBindingContextProvider = new Mock<IActionBindingContextProvider>();
actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny<ActionContext>()))
.Returns(Task.FromResult(bindingContext));
var invoker = new ReflectedActionInvoker(actionContext,
actionDescriptor,
Mock.Of<IControllerFactory>(),
actionBindingContextProvider.Object,
Mock.Of<INestedProviderManager<FilterProviderContext>>());
var modelStateDictionary = new ModelStateDictionary();
// Act
var result = await invoker.GetActionArguments(modelStateDictionary);
// Assert
Assert.Equal(1, result.Count);
Assert.Equal(value, result["foo"]);
}
[Fact]
public async Task Invoke_UsesDefaultValuesIfNotBound()
{
// Arrange
var actionDescriptor = new ReflectedActionDescriptor
{
MethodInfo = typeof(TestController).GetTypeInfo()
.DeclaredMethods
.First(m => m.Name.Equals("ActionMethodWithDefaultValues", StringComparison.Ordinal)),
Parameters = new List<ParameterDescriptor>
{
new ParameterDescriptor
{
Name = "value",
ParameterBindingInfo = new ParameterBindingInfo("value", typeof(int))
}
},
FilterDescriptors = new List<FilterDescriptor>()
};
var binder = new Mock<IModelBinder>();
var metadataProvider = new EmptyModelMetadataProvider();
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
.Returns(Task.FromResult(result: false));
var context = new Mock<HttpContext>();
context.SetupGet(c => c.Items)
.Returns(new Dictionary<object, object>());
var routeContext = new RouteContext(context.Object);
var actionContext = new ActionContext(routeContext,
actionDescriptor);
var bindingContext = new ActionBindingContext(actionContext,
Mock.Of<IModelMetadataProvider>(),
binder.Object,
Mock.Of<IValueProvider>(),
Mock.Of<IInputFormatterProvider>(),
Enumerable.Empty<IModelValidatorProvider>());
var actionBindingContextProvider = new Mock<IActionBindingContextProvider>();
actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny<ActionContext>()))
.Returns(Task.FromResult(bindingContext));
var controllerFactory = new Mock<IControllerFactory>();
controllerFactory.Setup(c => c.CreateController(It.IsAny<ActionContext>()))
.Returns(new TestController());
var invoker = new ReflectedActionInvoker(actionContext,
actionDescriptor,
controllerFactory.Object,
actionBindingContextProvider.Object,
Mock.Of<INestedProviderManager<FilterProviderContext>>());
// Act
await invoker.InvokeActionAsync();
// Assert
Assert.Equal(5, context.Object.Items["Result"]);
}
public JsonResult ActionMethod() public JsonResult ActionMethod()
{ {
return _result; return _result;
@ -1328,5 +1492,24 @@ namespace Microsoft.AspNet.Mvc
{ {
throw _actionException; throw _actionException;
} }
private sealed class TestController
{
public IActionResult ActionMethodWithDefaultValues(int value = 5)
{
return new TestActionResult { Value = value };
}
}
private sealed class TestActionResult : IActionResult
{
public int Value { get; set; }
public Task ExecuteResultAsync(ActionContext context)
{
context.HttpContext.Items["Result"] = Value;
return Task.FromResult(0);
}
}
} }
} }

View File

@ -30,6 +30,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="ActionDescriptorCreationCounter.cs" /> <Compile Include="ActionDescriptorCreationCounter.cs" />
<Compile Include="Controllers\HomeController.cs" /> <Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\MonitorController.cs" />
<Compile Include="Startup.cs" /> <Compile Include="Startup.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>