Skip to content

Commit 334fe40

Browse files
StickFunStickFun
andauthored
Added string arrays to simple binding (#63072)
* Added string arrays to simple binding * Added unit test for handling string array from form parameter * Updated test with HttpContext check * ParameterBindingMetadata check returned --------- Co-authored-by: StickFun <2000stickfun@gmail.com>
1 parent 15aec22 commit 334fe40

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public static partial class RequestDelegateFactory
129129
private static readonly string[] FormFileContentType = new[] { "multipart/form-data" };
130130
private static readonly string[] FormContentType = new[] { "multipart/form-data", "application/x-www-form-urlencoded" };
131131
private static readonly string[] PlaintextContentType = new[] { "text/plain" };
132+
private static readonly Type[] StringTypes = new[] {typeof(string), typeof(StringValues), typeof(StringValues?) };
132133

133134
/// <summary>
134135
/// Returns metadata inferred automatically for the <see cref="RequestDelegate"/> created by <see cref="Create(Delegate, RequestDelegateFactoryOptions?, RequestDelegateMetadataResult?)"/>.
@@ -791,11 +792,11 @@ private static Expression CreateArgument(ParameterInfo parameter, RequestDelegat
791792
// For complex types, leverage the shared form binding infrastructure. For example,
792793
// shared form binding does not currently only supports types that implement IParsable
793794
// while RDF's binding implementation supports all TryParse implementations.
794-
var useSimpleBinding = parameter.ParameterType == typeof(string) ||
795-
parameter.ParameterType == typeof(StringValues) ||
796-
parameter.ParameterType == typeof(StringValues?) ||
795+
var useSimpleBinding = StringTypes.Contains(parameter.ParameterType) ||
797796
ParameterBindingMethodCache.Instance.HasTryParseMethod(parameter.ParameterType) ||
798-
(parameter.ParameterType.IsArray && ParameterBindingMethodCache.Instance.HasTryParseMethod(parameter.ParameterType.GetElementType()!));
797+
(parameter.ParameterType.IsArray &&
798+
(StringTypes.Contains(parameter.ParameterType.GetElementType()) ||
799+
ParameterBindingMethodCache.Instance.HasTryParseMethod(parameter.ParameterType.GetElementType()!)));
799800
hasTryParse = useSimpleBinding;
800801
return useSimpleBinding
801802
? BindParameterFromFormItem(parameter, formAttribute.Name ?? parameter.Name, factoryContext)

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,34 @@ public async Task RequestDelegateHandlesNullableStringValuesFromExplicitQueryStr
684684
Assert.Null(httpContext.Items["form"]);
685685
}
686686

687+
[Fact]
688+
public async Task RequestDelegateHandlesFromFormStringArrayParameter()
689+
{
690+
var httpContext = CreateHttpContext();
691+
httpContext.Request.Form = new FormCollection(new Dictionary<string, StringValues>
692+
{
693+
["form"] = new(new[] { "1", "2", "3" })
694+
});
695+
696+
var factoryResult = RequestDelegateFactory.Create(
697+
(HttpContext context, [FromForm(Name = "form")] string[] formValues) =>
698+
{
699+
context.Items["form"] = formValues;
700+
});
701+
702+
var requestDelegate = factoryResult.RequestDelegate;
703+
704+
await requestDelegate(httpContext);
705+
706+
var parameterBindingMetadata = factoryResult.EndpointMetadata
707+
.FirstOrDefault(e => e is ParameterBindingMetadata metadata &&
708+
metadata.Name == "formValues") as ParameterBindingMetadata;
709+
710+
Assert.NotNull(parameterBindingMetadata);
711+
Assert.Equal(typeof(string[]), parameterBindingMetadata.ParameterInfo.ParameterType);
712+
Assert.Equal(new StringValues(new[] { "1", "2", "3" }), httpContext.Items["form"]!);
713+
}
714+
687715
[Fact]
688716
public async Task RequestDelegateCanAwaitValueTasksThatAreNotImmediatelyCompleted()
689717
{

0 commit comments

Comments
 (0)