Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
[Fixes #2609] Support comma separated string include in BindAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaybhargavb committed Jun 8, 2015
1 parent 12ed69e commit eba3521
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
25 changes: 22 additions & 3 deletions src/Microsoft.AspNet.Mvc.Core/BindAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNet.Mvc.Core;
Expand Down Expand Up @@ -30,13 +31,19 @@ public class BindAttribute : Attribute, IModelNameProvider, IPropertyBindingPred
/// <param name="include">Names of parameters to include in binding.</param>
public BindAttribute(params string[] include)
{
Include = include;
var items = new List<string>();
foreach (var item in include)
{
items.AddRange(SplitString(item));
}

Include = items.ToArray();
}

/// <summary>
/// Creates a new instance of <see cref="BindAttribute"/>.
/// </summary>
/// <param name="predicateProviderType">The type which implements
/// <param name="predicateProviderType">The type which implements
/// <see cref="IPropertyBindingPredicateProvider"/>.
/// </param>
public BindAttribute([NotNull] Type predicateProviderType)
Expand Down Expand Up @@ -92,7 +99,7 @@ public Func<ModelBindingContext, string, bool> PropertyFilter
{
if (_predicateFromInclude == null)
{
_predicateFromInclude =
_predicateFromInclude =
(context, propertyName) => Include.Contains(propertyName, StringComparer.Ordinal);
}

Expand Down Expand Up @@ -136,5 +143,17 @@ private static Func<ModelBindingContext, string, bool> CreatePredicateFromProvid
return predicate(context, propertyName);
};
}

private static IEnumerable<string> SplitString(string original)
{
if (string.IsNullOrEmpty(original))
{
return new string[0];
}

var split = original.Split(',').Select(piece => piece.Trim()).Where(piece => !string.IsNullOrEmpty(piece));

return split;
}
}
}
11 changes: 8 additions & 3 deletions test/Microsoft.AspNet.Mvc.Core.Test/BindAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ public void Constructor_SetsThe_PropertyFilterProviderType_ForValidTypes(Type ty
[InlineData("UserName", true)]
[InlineData("Username", false)]
[InlineData("Password", false)]
[InlineData("LastName", true)]
[InlineData("MiddleName", true)]
[InlineData(" ", false)]
[InlineData("foo", true)]
[InlineData("bar", true)]
public void BindAttribute_Include(string property, bool isIncluded)
{
// Arrange
var bind = new BindAttribute(new string[] { "UserName", "FirstName" });
var bind = new BindAttribute(new string[] { "UserName", "FirstName", "LastName, MiddleName, ,foo,bar " });

var context = new ModelBindingContext();

Expand All @@ -74,7 +79,7 @@ public void BindAttribute_ProviderType(string property, bool isIncluded)
HttpContext = new DefaultHttpContext(),
};
var services = new Mock<IServiceProvider>();

context.OperationBindingContext.HttpContext.RequestServices = services.Object;

// Act
Expand All @@ -99,7 +104,7 @@ public void BindAttribute_ProviderType_Cached()
};

var services = new Mock<IServiceProvider>(MockBehavior.Strict);

context.OperationBindingContext.HttpContext.RequestServices = services.Object;

// Act
Expand Down

0 comments on commit eba3521

Please sign in to comment.