This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing BindNever attribute and Type model binding
Fixes #4505
- Loading branch information
1 parent
d9aacd0
commit 2c639f8
Showing
9 changed files
with
302 additions
and
18 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Internal | ||
{ | ||
public class NoOpBinder : IModelBinder | ||
{ | ||
public static readonly IModelBinder Instance = new NoOpBinder(); | ||
|
||
public Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); | ||
return TaskCache.CompletedTask; | ||
} | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/ExcludeBindingMetadataProvider.cs
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata | ||
{ | ||
/// <summary> | ||
/// An <see cref="IBindingMetadataProvider"/> which configures <see cref="ModelMetadata.IsBindingAllowed"/> to | ||
/// <c>false</c> for matching types. | ||
/// </summary> | ||
public class ExcludeBindingMetadataProvider : IBindingMetadataProvider | ||
{ | ||
private readonly Type _type; | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="ExcludeBindingMetadataProvider"/> for the given <paramref name="type"/>. | ||
/// </summary> | ||
/// <param name="type"> | ||
/// The <see cref="Type"/>. All properties of this <see cref="Type"/> will have | ||
/// <see cref="ModelMetadata.IsBindingAllowed"/> set to <c>false</c>. | ||
/// </param> | ||
public ExcludeBindingMetadataProvider(Type type) | ||
{ | ||
if (type == null) | ||
{ | ||
throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
_type = type; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void CreateBindingMetadata(BindingMetadataProviderContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
// No-op if the metadata is not for the target type | ||
if (!_type.IsAssignableFrom(context.Key.ModelType)) | ||
{ | ||
return; | ||
} | ||
|
||
context.BindingMetadata.IsBindingAllowed = false; | ||
} | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
...soft.AspNetCore.Mvc.Core.Test/ModelBinding/Metadata/ExcludeBindingMetadataProviderTest.cs
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata | ||
{ | ||
public class ExcludeBindingMetadataProviderTest | ||
{ | ||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void IsBindingAllowed_LeftAlone_WhenTypeDoesntMatch(bool initialValue) | ||
{ | ||
// Arrange | ||
var provider = new ExcludeBindingMetadataProvider(typeof(string)); | ||
|
||
var key = ModelMetadataIdentity.ForProperty( | ||
typeof(int), | ||
nameof(Person.Age), | ||
typeof(Person)); | ||
|
||
var context = new BindingMetadataProviderContext(key, new ModelAttributes(new object[0], new object[0])); | ||
|
||
context.BindingMetadata.IsBindingAllowed = initialValue; | ||
|
||
// Act | ||
provider.CreateBindingMetadata(context); | ||
|
||
// Assert | ||
Assert.Equal(initialValue, context.BindingMetadata.IsBindingAllowed); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void IsBindingAllowed_IsFalse_WhenTypeMatches(bool initialValue) | ||
{ | ||
// Arrange | ||
var provider = new ExcludeBindingMetadataProvider(typeof(int)); | ||
|
||
var key = ModelMetadataIdentity.ForProperty( | ||
typeof(int), | ||
nameof(Person.Age), | ||
typeof(Person)); | ||
|
||
var context = new BindingMetadataProviderContext(key, new ModelAttributes(new object[0], new object[0])); | ||
|
||
context.BindingMetadata.IsBindingAllowed = initialValue; | ||
|
||
// Act | ||
provider.CreateBindingMetadata(context); | ||
|
||
// Assert | ||
Assert.False(context.BindingMetadata.IsBindingAllowed); | ||
} | ||
|
||
private class Person | ||
{ | ||
public int Age { get; set; } | ||
} | ||
} | ||
} |
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.