parent
28e16ed965
commit
525a479660
|
|
@ -3,12 +3,12 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||||
using Microsoft.AspNetCore.Mvc.Internal;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -16,15 +16,16 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
|
|||
{
|
||||
public class ExcludeBindingMetadataProviderIntegrationTest
|
||||
{
|
||||
[Fact(Skip = "See issue #6110")]
|
||||
[Fact]
|
||||
public async Task BindParameter_WithTypeProperty_IsNotBound()
|
||||
{
|
||||
// Arrange
|
||||
var options = new MvcOptions();
|
||||
var setup = new MvcCoreMvcOptionsSetup(new TestHttpRequestStreamReaderFactory());
|
||||
var modelBinderProvider = new TypeModelBinderProvider();
|
||||
|
||||
// Adding a custom model binder for Type to ensure it doesn't get called
|
||||
options.ModelBinderProviders.Insert(0, new TypeModelBinderProvider());
|
||||
options.ModelBinderProviders.Insert(0, modelBinderProvider);
|
||||
|
||||
setup.Configure(options);
|
||||
|
||||
|
|
@ -42,16 +43,9 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
|
|||
{
|
||||
{ "name", new[] { "Fred" } },
|
||||
{ "type", new[] { "SomeType" } },
|
||||
{ "typeArray", new[] { "SomeType1", "SomeType2" } },
|
||||
{ "typeList", new[] { "SomeType1", "SomeType2" } },
|
||||
{ "typeDictionary", new[] { "parameter[0].Key=key", "parameter[0].Value=value" } },
|
||||
{ "methodInfo", new[] { "value" } },
|
||||
{ "func", new[] { "value" } },
|
||||
});
|
||||
});
|
||||
|
||||
var modelState = testContext.ModelState;
|
||||
|
||||
// Act
|
||||
var modelBindingResult = await parameterBinder.BindModelAsync(parameter, testContext);
|
||||
|
||||
|
|
@ -64,10 +58,63 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
|
|||
Assert.NotNull(boundPerson);
|
||||
Assert.Equal("Fred", boundPerson.Name);
|
||||
|
||||
// ModelState
|
||||
|
||||
// The TypeModelBinder should not be called
|
||||
Assert.True(modelState.IsValid);
|
||||
Assert.False(modelBinderProvider.Invoked);
|
||||
}
|
||||
|
||||
[Fact()]
|
||||
public async Task BindParameter_WithTypeProperty_IsBound()
|
||||
{
|
||||
// Arrange
|
||||
var options = new MvcOptions();
|
||||
var setup = new MvcCoreMvcOptionsSetup(new TestHttpRequestStreamReaderFactory());
|
||||
var modelBinderProvider = new TypeModelBinderProvider();
|
||||
|
||||
// Adding a custom model binder for Type to ensure it doesn't get called
|
||||
options.ModelBinderProviders.Insert(0, modelBinderProvider);
|
||||
|
||||
setup.Configure(options);
|
||||
|
||||
// Remove the ExcludeBindingMetadataProvider
|
||||
for (var i = options.ModelMetadataDetailsProviders.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (options.ModelMetadataDetailsProviders[i] is ExcludeBindingMetadataProvider)
|
||||
{
|
||||
options.ModelMetadataDetailsProviders.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
var parameterBinder = ModelBindingTestHelper.GetParameterBinder(options);
|
||||
var parameter = new ParameterDescriptor()
|
||||
{
|
||||
Name = "Parameter1",
|
||||
BindingInfo = new BindingInfo(),
|
||||
ParameterType = typeof(TypesBundle),
|
||||
};
|
||||
|
||||
var testContext = ModelBindingTestHelper.GetTestContext(request =>
|
||||
{
|
||||
request.Form = new FormCollection(new Dictionary<string, StringValues>
|
||||
{
|
||||
{ "name", new[] { "Fred" } },
|
||||
{ "type", new[] { "SomeType" } },
|
||||
});
|
||||
});
|
||||
|
||||
// Act
|
||||
var modelBindingResult = await parameterBinder.BindModelAsync(parameter, testContext);
|
||||
|
||||
// Assert
|
||||
// ModelBindingResult
|
||||
Assert.True(modelBindingResult.IsModelSet);
|
||||
|
||||
// Model
|
||||
var boundPerson = Assert.IsType<TypesBundle>(modelBindingResult.Model);
|
||||
Assert.NotNull(boundPerson);
|
||||
Assert.Equal("Fred", boundPerson.Name);
|
||||
|
||||
// The TypeModelBinder should be called
|
||||
Assert.True(modelBinderProvider.Invoked);
|
||||
}
|
||||
|
||||
private class TypesBundle
|
||||
|
|
@ -75,20 +122,12 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
|
|||
public string Name { get; set; }
|
||||
|
||||
public Type Type { get; set; }
|
||||
|
||||
public Type[] TypeArray { get; set; }
|
||||
|
||||
public List<Type> TypeList { get; set; }
|
||||
|
||||
public Dictionary<string, Type> TypeDictionary { get; set; }
|
||||
|
||||
public MethodInfo MethodInfo { get; set; }
|
||||
|
||||
public Func<object> Func { get; set; }
|
||||
}
|
||||
|
||||
public class TypeModelBinderProvider : IModelBinderProvider
|
||||
{
|
||||
public bool Invoked { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IModelBinder GetBinder(ModelBinderProviderContext context)
|
||||
{
|
||||
|
|
@ -99,7 +138,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
|
|||
|
||||
if (context.Metadata.ModelType == typeof(Type))
|
||||
{
|
||||
throw new Exception();
|
||||
Invoked = true;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
|||
Loading…
Reference in New Issue