Skip to content

Commit 5889ae1

Browse files
committed
1 parent c3fb602 commit 5889ae1

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,15 @@ private static Expression CreateArgument(ParameterInfo parameter, RequestDelegat
821821
$"Nested {nameof(AsParametersAttribute)} is not supported and should be used only for handler parameters.");
822822
}
823823

824+
// Parameters like IFormFileCollection should throw "The abstract type ... is not supported" rather than
825+
// "...not supported on enumerable parameters.". It happens down the line.
826+
if (!parameter.ParameterType.IsAbstract
827+
&& typeof(System.Collections.IEnumerable).IsAssignableFrom(parameter.ParameterType))
828+
{
829+
throw new NotSupportedException(
830+
$"The {nameof(AsParametersAttribute)} is not supported on enumerable parameters.");
831+
}
832+
824833
return BindParameterFromProperties(parameter, factoryContext);
825834
}
826835
else if (parameter.ParameterType == typeof(HttpContext))

src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,33 @@ public void BuildRequestDelegateThrowsInvalidOperationExceptionForInvalidParamet
10221022
Assert.Equal(errorMessage, exception.Message);
10231023
}
10241024

1025+
public static object[][] BadArgumentEnumerableActions
1026+
{
1027+
get
1028+
{
1029+
void TestParameterStringList([AsParameters] List<string> req) { }
1030+
void TestParameterDictionary([AsParameters] Dictionary<string, string> req) { }
1031+
void TestParameterIntArray([AsParameters] int[] req) { }
1032+
1033+
return new object[][]
1034+
{
1035+
new object[] { (Action<List<string>>)TestParameterStringList },
1036+
new object[] { (Action<Dictionary<string, string>>)TestParameterDictionary },
1037+
new object[] { (Action<int[]>)TestParameterIntArray }
1038+
};
1039+
}
1040+
}
1041+
1042+
[Theory]
1043+
[MemberData(nameof(BadArgumentEnumerableActions))]
1044+
public void BuildRequestDelegateThrowsNotSupportedExceptionForEnumerable(Delegate @delegate)
1045+
{
1046+
var errorMessage = $"The {nameof(AsParametersAttribute)} is not supported on enumerable parameters.";
1047+
var exception = Assert.Throws<NotSupportedException>(() => RequestDelegateFactory.Create(@delegate));
1048+
1049+
Assert.Equal(errorMessage, exception.Message);
1050+
}
1051+
10251052
[Fact]
10261053
public void BuildRequestDelegateThrowsNotSupportedExceptionForNestedParametersList()
10271054
{
@@ -2014,6 +2041,15 @@ void TestJsonAndFormWithAttribute(Todo value1, [FromForm] string value2) { }
20142041
Assert.Throws<InvalidOperationException>(() => RequestDelegateFactory.Create(TestJsonAndFormWithAttribute));
20152042
}
20162043

2044+
[Fact]
2045+
public void CreateThrowsInvalidOperationExceptionIfIFormFileCollectionHasAsParametersAttribute()
2046+
{
2047+
void TestAction([AsParameters] IFormFileCollection formFiles) {}
2048+
2049+
var ioe = Assert.Throws<InvalidOperationException>(() => RequestDelegateFactory.Create(TestAction));
2050+
Assert.Equal("The abstract type 'IFormFileCollection' is not supported.", ioe.Message);
2051+
}
2052+
20172053
[Fact]
20182054
public void CreateThrowsNotSupportedExceptionIfIFormFileCollectionHasMetadataParameterName()
20192055
{

0 commit comments

Comments
 (0)