Merge branch 'release/2.1' into dev

This commit is contained in:
Kiran Challa 2018-03-19 11:53:22 -07:00
commit 236cab7cc8
7 changed files with 49 additions and 20 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

@ -118,7 +118,9 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddSingleton<IPageHandlerMethodSelector, DefaultPageHandlerMethodSelector>();
// Page model binding
#pragma warning disable CS0618 // Type or member is obsolete
services.TryAddSingleton<PageArgumentBinder, DefaultPageArgumentBinder>();
#pragma warning restore CS0618 // Type or member is obsolete
// Action executors
services.TryAddSingleton<PageResultExecutor>();

View File

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
[Obsolete("This type is obsolete and will be removed in a future version.")]
public abstract class PageArgumentBinder
{
public async Task<object> BindModelAsync(PageContext context, Type type, object @default, string name)

View File

@ -9,7 +9,9 @@ using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{
#pragma warning disable CS0618 // Type or member is obsolete
public class DefaultPageArgumentBinder : PageArgumentBinder
#pragma warning restore CS0618 // Type or member is obsolete
{
private readonly ParameterBinder _parameterBinder;

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)
{
}

View File

@ -327,14 +327,5 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Test.Internal
throw new NotImplementedException();
}
}
private class MockBinder : PageArgumentBinder
{
protected override Task<ModelBindingResult> BindAsync(PageContext context, object value, string name, Type type)
{
var result = ModelBindingResult.Failed();
return Task.FromResult(result);
}
}
}
}

View File

@ -1942,13 +1942,5 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
throw new NotImplementedException();
}
}
private class TestPageArgumentBinder : PageArgumentBinder
{
protected override Task<ModelBindingResult> BindAsync(PageContext context, object value, string name, Type type)
{
return Task.FromResult(ModelBindingResult.Success(Guid.NewGuid()));
}
}
}
}