From 45ff0a269cf1a6095bb1131095592c0a9c1bd032 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Fri, 20 Nov 2015 16:29:26 -0800 Subject: [PATCH] Make `ModelBinderAttribute.BindingSource` setter `protected` - #3428 - the `public` setter was not useful when this class is used as an attribute - make property `virtual` to support other override patterns - update existing test to use both override patterns --- .../ModelBinderAttribute.cs | 4 ++-- .../DefaultBindingMetadataProviderTest.cs | 16 ++++++++++++---- .../Metadata/ModelBinderAttributeTest.cs | 9 ++++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinderAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinderAttribute.cs index d7d4e91b87..d42cf6e0f0 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ModelBinderAttribute.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinderAttribute.cs @@ -32,7 +32,7 @@ namespace Microsoft.AspNet.Mvc public Type BinderType { get; set; } /// - public BindingSource BindingSource + public virtual BindingSource BindingSource { get { @@ -43,7 +43,7 @@ namespace Microsoft.AspNet.Mvc return _bindingSource; } - set + protected set { _bindingSource = value; } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Metadata/DefaultBindingMetadataProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Metadata/DefaultBindingMetadataProviderTest.cs index ca982f4ba5..6374136954 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Metadata/DefaultBindingMetadataProviderTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Metadata/DefaultBindingMetadataProviderTest.cs @@ -108,8 +108,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata // Arrange var attributes = new object[] { - new ModelBinderAttribute() { BindingSource = BindingSource.Body }, - new ModelBinderAttribute() { BindingSource = BindingSource.Query }, + new BindingSourceModelBinderAttribute(BindingSource.Body), + new BindingSourceModelBinderAttribute(BindingSource.Query), }; var context = new BindingMetadataProviderContext( @@ -132,8 +132,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata var attributes = new object[] { new ModelBinderAttribute(), - new ModelBinderAttribute() { BindingSource = BindingSource.Body }, - new ModelBinderAttribute() { BindingSource = BindingSource.Query }, + new BindingSourceModelBinderAttribute(BindingSource.Body), + new BindingSourceModelBinderAttribute(BindingSource.Query), }; var context = new BindingMetadataProviderContext( @@ -545,5 +545,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata private class BindRequiredOverridesInheritedBindNever : BindNeverOnClass { } + + private class BindingSourceModelBinderAttribute : ModelBinderAttribute + { + public BindingSourceModelBinderAttribute(BindingSource bindingSource) + { + BindingSource = bindingSource; + } + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Metadata/ModelBinderAttributeTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Metadata/ModelBinderAttributeTest.cs index 5d1d3689bf..bd3ecf2fcf 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Metadata/ModelBinderAttributeTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Metadata/ModelBinderAttributeTest.cs @@ -1,7 +1,6 @@ // 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 Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding @@ -39,8 +38,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding public void BinderType_SettingBindingSource_OverridesDefaultCustomBindingSource() { // Arrange - var attribute = new ModelBinderAttribute(); - attribute.BindingSource = BindingSource.Query; + var attribute = new FromQueryModelBinderAttribute(); attribute.BinderType = typeof(ByteArrayModelBinder); // Act @@ -49,5 +47,10 @@ namespace Microsoft.AspNet.Mvc.ModelBinding // Assert Assert.Equal(BindingSource.Query, source); } + + private class FromQueryModelBinderAttribute : ModelBinderAttribute + { + public override BindingSource BindingSource => BindingSource.Query; + } } } \ No newline at end of file