Use ParameterInfo for getting metadata of a parameter to show the correct information in ApiExplorer

[Fixes #7435] 2.1-Preview 1: IsBindingRequired and IsRequired still false with RequiredAttribute on controller parameter.
This commit is contained in:
Kiran Challa 2018-03-13 01:59:53 -07:00
parent e7b2e3fe2d
commit 09b5ff7b72
2 changed files with 44 additions and 3 deletions

View File

@ -162,7 +162,23 @@ namespace Microsoft.AspNetCore.Mvc.ApiExplorer
foreach (var actionParameter in context.ActionDescriptor.Parameters)
{
var visitor = new PseudoModelBindingVisitor(context, actionParameter);
var metadata = _modelMetadataProvider.GetMetadataForType(actionParameter.ParameterType);
ModelMetadata metadata = null;
if (actionParameter is ControllerParameterDescriptor controllerParameterDescriptor &&
_modelMetadataProvider is ModelMetadataProvider provider)
{
// The default model metadata provider derives from ModelMetadataProvider
// and can therefore supply information about attributes applied to parameters.
metadata = provider.GetMetadataForParameter(controllerParameterDescriptor.ParameterInfo);
}
else
{
// For backward compatibility, if there's a custom model metadata provider that
// only implements the older IModelMetadataProvider interface, access the more
// limited metadata information it supplies. In this scenario, validation attributes
// are not supported on parameters.
metadata = _modelMetadataProvider.GetMetadataForType(actionParameter.ParameterType);
}
var bindingContext = ApiParameterDescriptionContext.GetContext(
metadata,

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
@ -912,6 +913,25 @@ namespace Microsoft.AspNetCore.Mvc.Description
Assert.Equal(typeof(string), parameter.Type);
}
[Fact]
public void GetApiDescription_ParameterDescription_IsRequired()
{
// Arrange
var action = CreateActionDescriptor(nameof(RequiredParameter));
// Act
var descriptions = GetApiDescriptions(action);
// Assert
var description = Assert.Single(descriptions);
var parameter = Assert.Single(description.ParameterDescriptions);
Assert.Equal("name", parameter.Name);
Assert.Same(BindingSource.ModelBinding, parameter.Source);
Assert.Equal(typeof(string), parameter.Type);
Assert.True(parameter.ModelMetadata.IsRequired);
Assert.True(parameter.ModelMetadata.IsBindingRequired);
}
[Fact]
public void GetApiDescription_ParameterDescription_SourceFromRouteData()
{
@ -1472,11 +1492,12 @@ namespace Microsoft.AspNetCore.Mvc.Description
action.Parameters = new List<ParameterDescriptor>();
foreach (var parameter in action.MethodInfo.GetParameters())
{
action.Parameters.Add(new ParameterDescriptor()
action.Parameters.Add(new ControllerParameterDescriptor()
{
Name = parameter.Name,
ParameterType = parameter.ParameterType,
BindingInfo = BindingInfo.GetBindingInfo(parameter.GetCustomAttributes().OfType<object>())
BindingInfo = BindingInfo.GetBindingInfo(parameter.GetCustomAttributes().OfType<object>()),
ParameterInfo = parameter
});
}
@ -1552,6 +1573,10 @@ namespace Microsoft.AspNetCore.Mvc.Description
{
}
private void RequiredParameter([BindRequired, Required] string name)
{
}
private void AcceptsProduct_Body([FromBody] Product product)
{
}