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

Commit 734b919

Browse files
committed
Respect SuppressInferBindingSourcesForParameters
Fixes #8657
1 parent af6527d commit 734b919

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/Microsoft.AspNetCore.Mvc.Core/ApplicationModels/InferParameterBindingInfoConvention.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public void Apply(ControllerModel controller)
5959

6060
internal void InferParameterBindingSources(ActionModel action)
6161
{
62-
var inferredBindingSources = new BindingSource[action.Parameters.Count];
62+
if (SuppressInferBindingSourcesForParameters)
63+
{
64+
return;
65+
}
6366

6467
for (var i = 0; i < action.Parameters.Count; i++)
6568
{

test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationModels/InferParameterBindingInfoConventionTest.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,71 @@ public void InferParameterBindingSources_Throws_IfMultipleParametersAreFromBody(
9393
Assert.Equal(expected, ex.Message);
9494
}
9595

96+
[Fact]
97+
public void InferParameterBindingSources_InfersSources()
98+
{
99+
// Arrange
100+
var actionName = nameof(ParameterBindingController.ComplexTypeModelWithCancellationToken);
101+
var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
102+
var convention = GetConvention(modelMetadataProvider);
103+
var action = GetActionModel(typeof(ParameterBindingController), actionName, modelMetadataProvider);
104+
105+
// Act
106+
convention.InferParameterBindingSources(action);
107+
108+
// Assert
109+
Assert.Collection(
110+
action.Parameters,
111+
parameter =>
112+
{
113+
Assert.Equal("model", parameter.Name);
114+
115+
var bindingInfo = parameter.BindingInfo;
116+
Assert.NotNull(bindingInfo);
117+
Assert.Same(BindingSource.Body, bindingInfo.BindingSource);
118+
},
119+
parameter =>
120+
{
121+
Assert.Equal("cancellationToken", parameter.Name);
122+
123+
var bindingInfo = parameter.BindingInfo;
124+
Assert.NotNull(bindingInfo);
125+
Assert.Equal(BindingSource.Special, bindingInfo.BindingSource);
126+
});
127+
}
128+
129+
[Fact]
130+
public void InferParameterBindingSources_DoesNotInferSources_IfSuppressInferBindingSourcesForParametersIsSet()
131+
{
132+
// Arrange
133+
var actionName = nameof(ParameterBindingController.ComplexTypeModelWithCancellationToken);
134+
var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
135+
var convention = GetConvention(modelMetadataProvider);
136+
var action = GetActionModel(typeof(ParameterBindingController), actionName, modelMetadataProvider);
137+
138+
convention.SuppressInferBindingSourcesForParameters = true;
139+
140+
// Act
141+
convention.InferParameterBindingSources(action);
142+
143+
// Assert
144+
Assert.Collection(
145+
action.Parameters,
146+
parameter =>
147+
{
148+
Assert.Equal("model", parameter.Name);
149+
Assert.Null(parameter.BindingInfo);
150+
},
151+
parameter =>
152+
{
153+
Assert.Equal("cancellationToken", parameter.Name);
154+
155+
var bindingInfo = parameter.BindingInfo;
156+
Assert.NotNull(bindingInfo);
157+
Assert.Equal(BindingSource.Special, bindingInfo.BindingSource);
158+
});
159+
}
160+
96161
[Fact]
97162
public void Apply_PreservesBindingInfo_WhenInferringFor_ParameterWithModelBinder_AndExplicitName()
98163
{

0 commit comments

Comments
 (0)