Address feedback for PR: https://github.com/aspnet/Routing/pull/182
This commit is contained in:
parent
fc87a436c1
commit
f2e6c294b0
|
|
@ -31,23 +31,24 @@ namespace Microsoft.AspNet.Routing
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="RouteValueDictionary"/> initialized with the provided input value.
|
||||
/// Creates a <see cref="RouteValueDictionary"/> initialized with the specified <paramref name="values"/>.
|
||||
/// </summary>
|
||||
/// <param name="obj">An object to initialize the dictionary. The value can be of type
|
||||
/// <see cref="IDictionary{TKey, TValue}"/> or <see cref="IReadOnlyDictionary{TKey, TValue}"/> or
|
||||
/// any other object.
|
||||
/// <param name="values">An object to initialize the dictionary. The value can be of type
|
||||
/// <see cref="IDictionary{TKey, TValue}"/> or <see cref="IReadOnlyDictionary{TKey, TValue}"/>
|
||||
/// or an object with public properties as key-value pairs.
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// If the value is a dictionary, then its entries are copied. Otherwise the object is interpreted as a set
|
||||
/// of key-value-pairs where the property names are keys, and property values are the values, and copied
|
||||
/// into the dictionary. Only public instance non-index properties are considered.
|
||||
/// If the value is a dictionary or other <see cref="IEnumerable{KeyValuePair{string, object}}"/>,
|
||||
/// then its entries are copied. Otherwise the object is interpreted as a set of key-value pairs where the
|
||||
/// property names are keys, and property values are the values, and copied into the dictionary.
|
||||
/// Only public instance non-index properties are considered.
|
||||
/// </remarks>
|
||||
public RouteValueDictionary(object obj)
|
||||
public RouteValueDictionary(object values)
|
||||
: this()
|
||||
{
|
||||
if (obj != null)
|
||||
if (values != null)
|
||||
{
|
||||
var keyValuePairCollection = obj as IEnumerable<KeyValuePair<string, object>>;
|
||||
var keyValuePairCollection = values as IEnumerable<KeyValuePair<string, object>>;
|
||||
if (keyValuePairCollection != null)
|
||||
{
|
||||
foreach (var kvp in keyValuePairCollection)
|
||||
|
|
@ -57,7 +58,7 @@ namespace Microsoft.AspNet.Routing
|
|||
return;
|
||||
}
|
||||
|
||||
var type = obj.GetType();
|
||||
var type = values.GetType();
|
||||
var allProperties = type.GetRuntimeProperties();
|
||||
|
||||
// This is done to support 'new' properties that hide a property on a base class
|
||||
|
|
@ -69,7 +70,7 @@ namespace Microsoft.AspNet.Routing
|
|||
!property.GetMethod.IsStatic &&
|
||||
property.GetIndexParameters().Length == 0)
|
||||
{
|
||||
var value = property.GetValue(obj);
|
||||
var value = property.GetValue(values);
|
||||
if (ContainsKey(property.Name) && property.DeclaringType != type)
|
||||
{
|
||||
// This is a hidden property, ignore it.
|
||||
|
|
|
|||
|
|
@ -414,7 +414,7 @@ namespace Microsoft.AspNet.Routing.Template
|
|||
return string.Format(
|
||||
"{{Accepted: '{0}' Filters: '{1}'}}",
|
||||
string.Join(", ", _acceptedValues.Keys),
|
||||
string.Join(", ", _filters.Keys));
|
||||
string.Join(", ", _filters?.Keys));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Xunit;
|
||||
|
|
@ -168,36 +169,47 @@ namespace Microsoft.AspNet.Routing.Tests
|
|||
expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateFromReadOnlyDictionary_CopiesValues()
|
||||
public static IEnumerable<object[]> IEnumerableKeyValuePairData
|
||||
{
|
||||
// Arrange
|
||||
var dictionary = new Dictionary<string, object>();
|
||||
dictionary.Add("Name", "James");
|
||||
dictionary.Add("Age", 30);
|
||||
var readonlyDictionary = (IReadOnlyDictionary<string, object>)dictionary;
|
||||
get
|
||||
{
|
||||
var routeValues = new[]
|
||||
{
|
||||
new KeyValuePair<string, object>("Name", "James"),
|
||||
new KeyValuePair<string, object>("Age", 30),
|
||||
new KeyValuePair<string, object>("Address", new Address() { City = "Redmond", State = "WA" })
|
||||
};
|
||||
|
||||
// Act
|
||||
var dict = new RouteValueDictionary(readonlyDictionary);
|
||||
yield return new object[] { routeValues.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) };
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, dict.Count);
|
||||
Assert.Equal("James", dict["Name"]);
|
||||
Assert.Equal(30, dict["Age"]);
|
||||
yield return new object[] { routeValues.ToList() };
|
||||
|
||||
yield return new object[] { routeValues };
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateFromReadOnlyDictionary_ModifyRouteValueDictionary()
|
||||
[Theory]
|
||||
[MemberData(nameof(IEnumerableKeyValuePairData))]
|
||||
public void RouteValueDictionary_CopiesValues_FromIEnumerableKeyValuePair(object values)
|
||||
{
|
||||
// Arrange
|
||||
var dictionary = new Dictionary<string, object>();
|
||||
dictionary.Add("Name", "James");
|
||||
dictionary.Add("Age", 30);
|
||||
dictionary.Add("Address", new Address() { City = "Redmond", State = "WA" });
|
||||
var readonlyDictionary = (IReadOnlyDictionary<string, object>)dictionary;
|
||||
// Arrange & Act
|
||||
var dict = new RouteValueDictionary(values);
|
||||
|
||||
// Act
|
||||
var routeValueDictionary = new RouteValueDictionary(readonlyDictionary);
|
||||
// Assert
|
||||
Assert.Equal(3, dict.Count);
|
||||
Assert.Equal("James", dict["Name"]);
|
||||
Assert.Equal(30, dict["Age"]);
|
||||
var address = Assert.IsType<Address>(dict["Address"]);
|
||||
Assert.Equal("Redmond", address.City);
|
||||
Assert.Equal("WA", address.State);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(IEnumerableKeyValuePairData))]
|
||||
public void CreatedFrom_IEnumerableKeyValuePair_AllowsAddingOrModifyingValues(object values)
|
||||
{
|
||||
// Arrange & Act
|
||||
var routeValueDictionary = new RouteValueDictionary(values);
|
||||
routeValueDictionary.Add("City", "Redmond");
|
||||
|
||||
// Assert
|
||||
|
|
|
|||
Loading…
Reference in New Issue