From f629f9a6596474f32db9afa201f1dd59a13c197d Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Thu, 20 Oct 2016 13:40:39 -0700 Subject: [PATCH] Check for null arguments in GetArgumentDictionary Addresses #5423 --- .../ViewComponents/DefaultViewComponentHelper.cs | 11 +++++++---- .../DefaultViewComponentHelperTest.cs | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentHelper.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentHelper.cs index fff5e3e689..19221c2916 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentHelper.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentHelper.cs @@ -135,12 +135,15 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents // Internal for testing internal IDictionary GetArgumentDictionary(ViewComponentDescriptor descriptor, object arguments) { - if (descriptor.Parameters.Count == 1 && descriptor.Parameters[0].ParameterType.IsAssignableFrom(arguments.GetType())) + if (arguments != null) { - return new Dictionary(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(capacity: 1, comparer: StringComparer.OrdinalIgnoreCase) + { + { descriptor.Parameters[0].Name, arguments } + }; + } } return PropertyHelper.ObjectToDictionary(arguments); diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/DefaultViewComponentHelperTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/DefaultViewComponentHelperTest.cs index 8098d6511e..ae4503c7a9 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/DefaultViewComponentHelperTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/DefaultViewComponentHelperTest.cs @@ -15,6 +15,21 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents { 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),argumentDictionary); + } + [Fact] public void GetArgumentDictionary_SupportsAnonymouslyTypedArguments() {