This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
Cleanup InferParameterBindingInfoConvention #8665
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,7 @@ | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Internal; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Routing.Template; | ||
|
@@ -13,13 +11,18 @@ | |
namespace Microsoft.AspNetCore.Mvc.ApplicationModels | ||
{ | ||
/// <summary> | ||
/// A <see cref="IControllerModelConvention"/> that | ||
/// <list type="bullet"> | ||
/// <item>infers binding sources for parameters</item> | ||
/// <item><see cref="BindingInfo.BinderModelName"/> for bound properties and parameters.</item> | ||
/// </list> | ||
/// An <see cref="IActionModelConvention"/> that infers <see cref="BindingInfo.BindingSource"/> for parameters. | ||
/// </summary> | ||
public class InferParameterBindingInfoConvention : IControllerModelConvention | ||
/// <remarks> | ||
/// The goal of this covention is to make intuitive and easy to document <see cref="BindingSource"/> inferences. The rules are: | ||
/// <list type="number"> | ||
/// <item>A previously specified <see cref="BindingInfo.BindingSource" /> is never overwritten.</item> | ||
/// <item>A complex type parameter (<see cref="ModelMetadata.IsComplexType"/>) is assigned <see cref="BindingSource.Body"/>.</item> | ||
/// <item>Parameter with a name that appears as a route value in ANY route template is assigned <see cref="BindingSource.Path"/>.</item> | ||
/// <item>All other parameters are <see cref="BindingSource.Query"/>.</item> | ||
/// </list> | ||
/// </remarks> | ||
public class InferParameterBindingInfoConvention : IActionModelConvention | ||
{ | ||
private readonly IModelMetadataProvider _modelMetadataProvider; | ||
|
||
|
@@ -29,41 +32,27 @@ public class InferParameterBindingInfoConvention : IControllerModelConvention | |
_modelMetadataProvider = modelMetadataProvider ?? throw new ArgumentNullException(nameof(modelMetadataProvider)); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets a value that determines if model binding sources are inferred for action parameters on controllers is suppressed. | ||
/// </summary> | ||
public bool SuppressInferBindingSourcesForParameters { get; set; } | ||
internal bool AllowInferringBindingSourceForCollectionTypesAsFromQuery { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This convention type is new to 2.2, so users wouldn't need a compat flag. |
||
|
||
protected virtual bool ShouldApply(ControllerModel controller) => true; | ||
protected virtual bool ShouldApply(ActionModel action) => true; | ||
|
||
public void Apply(ControllerModel controller) | ||
public void Apply(ActionModel action) | ||
{ | ||
if (controller == null) | ||
if (action == null) | ||
{ | ||
throw new ArgumentNullException(nameof(controller)); | ||
throw new ArgumentNullException(nameof(action)); | ||
} | ||
|
||
if (!ShouldApply(controller)) | ||
if (!ShouldApply(action)) | ||
{ | ||
return; | ||
} | ||
|
||
InferBoundPropertyModelPrefixes(controller); | ||
|
||
foreach (var action in controller.Actions) | ||
{ | ||
InferParameterBindingSources(action); | ||
InferParameterModelPrefixes(action); | ||
} | ||
InferParameterBindingSources(action); | ||
} | ||
|
||
internal void InferParameterBindingSources(ActionModel action) | ||
{ | ||
if (SuppressInferBindingSourcesForParameters) | ||
{ | ||
return; | ||
} | ||
|
||
for (var i = 0; i < action.Parameters.Count; i++) | ||
{ | ||
var parameter = action.Parameters[i]; | ||
|
@@ -95,56 +84,17 @@ internal void InferParameterBindingSources(ActionModel action) | |
// Internal for unit testing. | ||
internal BindingSource InferBindingSourceForParameter(ParameterModel parameter) | ||
{ | ||
if (ParameterExistsInAnyRoute(parameter.Action, parameter.ParameterName)) | ||
if (IsComplexTypeParameter(parameter)) | ||
{ | ||
return BindingSource.Path; | ||
return BindingSource.Body; | ||
} | ||
|
||
var bindingSource = IsComplexTypeParameter(parameter) ? | ||
BindingSource.Body : | ||
BindingSource.Query; | ||
|
||
return bindingSource; | ||
} | ||
|
||
// For any complex types that are bound from value providers, set the prefix | ||
// to the empty prefix by default. This makes binding much more predictable | ||
// and describable via ApiExplorer | ||
internal void InferBoundPropertyModelPrefixes(ControllerModel controllerModel) | ||
{ | ||
foreach (var property in controllerModel.ControllerProperties) | ||
if (ParameterExistsInAnyRoute(parameter.Action, parameter.ParameterName)) | ||
{ | ||
if (property.BindingInfo != null && | ||
property.BindingInfo.BinderModelName == null && | ||
property.BindingInfo.BindingSource != null && | ||
!property.BindingInfo.BindingSource.IsGreedy && | ||
!IsFormFile(property.ParameterType)) | ||
{ | ||
var metadata = _modelMetadataProvider.GetMetadataForProperty( | ||
controllerModel.ControllerType, | ||
property.PropertyInfo.Name); | ||
if (metadata.IsComplexType && !metadata.IsCollectionType) | ||
{ | ||
property.BindingInfo.BinderModelName = string.Empty; | ||
} | ||
} | ||
return BindingSource.Path; | ||
} | ||
} | ||
|
||
internal void InferParameterModelPrefixes(ActionModel action) | ||
{ | ||
foreach (var parameter in action.Parameters) | ||
{ | ||
var bindingInfo = parameter.BindingInfo; | ||
if (bindingInfo?.BindingSource != null && | ||
bindingInfo.BinderModelName == null && | ||
!bindingInfo.BindingSource.IsGreedy && | ||
!IsFormFile(parameter.ParameterType) && | ||
IsComplexTypeParameter(parameter)) | ||
{ | ||
parameter.BindingInfo.BinderModelName = string.Empty; | ||
} | ||
} | ||
return BindingSource.Query; | ||
} | ||
|
||
private bool ParameterExistsInAnyRoute(ActionModel action, string parameterName) | ||
|
@@ -171,13 +121,13 @@ private bool IsComplexTypeParameter(ParameterModel parameter) | |
// No need for information from attributes on the parameter. Just use its type. | ||
var metadata = _modelMetadataProvider | ||
.GetMetadataForType(parameter.ParameterInfo.ParameterType); | ||
return metadata.IsComplexType && !metadata.IsCollectionType; | ||
} | ||
|
||
private static bool IsFormFile(Type parameterType) | ||
{ | ||
return typeof(IFormFile).IsAssignableFrom(parameterType) || | ||
typeof(IEnumerable<IFormFile>).IsAssignableFrom(parameterType); | ||
if (AllowInferringBindingSourceForCollectionTypesAsFromQuery && metadata.IsCollectionType) | ||
{ | ||
return false; | ||
} | ||
|
||
return metadata.IsComplexType; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see we don't need
IControllerModelConvention
s anymore. But, what about our customers? Suggest leaving the property and the application of any items in the list. That is, limit change to initializing it as empty.If this is a removal we've already decided on, remove the
IControllerModelConvention
interface as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type is internal, so this does not affect users.