Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Respect SuppressInferBindingSourcesForParameters
Browse files Browse the repository at this point in the history
Fixes #8657
  • Loading branch information
pranavkm committed Oct 30, 2018
1 parent af6527d commit 734b919
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Expand Up @@ -59,7 +59,10 @@ public void Apply(ControllerModel controller)

internal void InferParameterBindingSources(ActionModel action)
{
var inferredBindingSources = new BindingSource[action.Parameters.Count];
if (SuppressInferBindingSourcesForParameters)
{
return;
}

for (var i = 0; i < action.Parameters.Count; i++)
{
Expand Down
Expand Up @@ -93,6 +93,71 @@ public void InferParameterBindingSources_Throws_IfMultipleParametersAreFromBody(
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()
{
Expand Down

0 comments on commit 734b919

Please sign in to comment.