Respect SuppressInferBindingSourcesForParameters

Fixes https://github.com/aspnet/Mvc/issues/8657
This commit is contained in:
Pranav K 2018-10-30 13:27:31 -07:00
parent af6527dcef
commit 734b919b02
2 changed files with 69 additions and 1 deletions

View File

@ -59,7 +59,10 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
internal void InferParameterBindingSources(ActionModel action)
{
var inferredBindingSources = new BindingSource[action.Parameters.Count];
if (SuppressInferBindingSourcesForParameters)
{
return;
}
for (var i = 0; i < action.Parameters.Count; i++)
{

View File

@ -93,6 +93,71 @@ Environment.NewLine + "int b";
Assert.Equal(expected, ex.Message);
}
[Fact]
public void InferParameterBindingSources_InfersSources()
{
// Arrange
var actionName = nameof(ParameterBindingController.ComplexTypeModelWithCancellationToken);
var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
var convention = GetConvention(modelMetadataProvider);
var action = GetActionModel(typeof(ParameterBindingController), actionName, modelMetadataProvider);
// Act
convention.InferParameterBindingSources(action);
// Assert
Assert.Collection(
action.Parameters,
parameter =>
{
Assert.Equal("model", parameter.Name);
var bindingInfo = parameter.BindingInfo;
Assert.NotNull(bindingInfo);
Assert.Same(BindingSource.Body, bindingInfo.BindingSource);
},
parameter =>
{
Assert.Equal("cancellationToken", parameter.Name);
var bindingInfo = parameter.BindingInfo;
Assert.NotNull(bindingInfo);
Assert.Equal(BindingSource.Special, bindingInfo.BindingSource);
});
}
[Fact]
public void InferParameterBindingSources_DoesNotInferSources_IfSuppressInferBindingSourcesForParametersIsSet()
{
// Arrange
var actionName = nameof(ParameterBindingController.ComplexTypeModelWithCancellationToken);
var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
var convention = GetConvention(modelMetadataProvider);
var action = GetActionModel(typeof(ParameterBindingController), actionName, modelMetadataProvider);
convention.SuppressInferBindingSourcesForParameters = true;
// Act
convention.InferParameterBindingSources(action);
// Assert
Assert.Collection(
action.Parameters,
parameter =>
{
Assert.Equal("model", parameter.Name);
Assert.Null(parameter.BindingInfo);
},
parameter =>
{
Assert.Equal("cancellationToken", parameter.Name);
var bindingInfo = parameter.BindingInfo;
Assert.NotNull(bindingInfo);
Assert.Equal(BindingSource.Special, bindingInfo.BindingSource);
});
}
[Fact]
public void Apply_PreservesBindingInfo_WhenInferringFor_ParameterWithModelBinder_AndExplicitName()
{