Check for null arguments in GetArgumentDictionary

Addresses #5423
This commit is contained in:
Jass Bagga 2016-10-20 13:40:39 -07:00 committed by GitHub
parent 79e576b86c
commit f629f9a659
2 changed files with 22 additions and 4 deletions

View File

@ -135,12 +135,15 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents
// Internal for testing // Internal for testing
internal IDictionary<string, object> GetArgumentDictionary(ViewComponentDescriptor descriptor, object arguments) internal IDictionary<string, object> GetArgumentDictionary(ViewComponentDescriptor descriptor, object arguments)
{ {
if (descriptor.Parameters.Count == 1 && descriptor.Parameters[0].ParameterType.IsAssignableFrom(arguments.GetType())) if (arguments != null)
{ {
return new Dictionary<string, object>(capacity: 1, comparer: StringComparer.OrdinalIgnoreCase) if (descriptor.Parameters.Count == 1 && descriptor.Parameters[0].ParameterType.IsAssignableFrom(arguments.GetType()))
{ {
{ descriptor.Parameters[0].Name, arguments } return new Dictionary<string, object>(capacity: 1, comparer: StringComparer.OrdinalIgnoreCase)
}; {
{ descriptor.Parameters[0].Name, arguments }
};
}
} }
return PropertyHelper.ObjectToDictionary(arguments); return PropertyHelper.ObjectToDictionary(arguments);

View File

@ -15,6 +15,21 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents
{ {
public class DefaultViewComponentHelperTest public class DefaultViewComponentHelperTest
{ {
[Fact]
public void GetArgumentDictionary_SupportsNullArguments()
{
// Arrange
var helper = CreateHelper();
var descriptor = CreateDescriptorForType(typeof(ViewComponentSingleParam));
// Act
var argumentDictionary = helper.GetArgumentDictionary(descriptor, null);
// Assert
Assert.Equal(0, argumentDictionary.Count);
Assert.IsType(typeof(Dictionary<string,object>),argumentDictionary);
}
[Fact] [Fact]
public void GetArgumentDictionary_SupportsAnonymouslyTypedArguments() public void GetArgumentDictionary_SupportsAnonymouslyTypedArguments()
{ {