Skip to content

Added string arrays to simple binding #63072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public static partial class RequestDelegateFactory
private static readonly string[] FormFileContentType = new[] { "multipart/form-data" };
private static readonly string[] FormContentType = new[] { "multipart/form-data", "application/x-www-form-urlencoded" };
private static readonly string[] PlaintextContentType = new[] { "text/plain" };
private static readonly Type[] StringTypes = new[] {typeof(string), typeof(StringValues), typeof(StringValues?) };

/// <summary>
/// Returns metadata inferred automatically for the <see cref="RequestDelegate"/> created by <see cref="Create(Delegate, RequestDelegateFactoryOptions?, RequestDelegateMetadataResult?)"/>.
Expand Down Expand Up @@ -791,11 +792,11 @@ private static Expression CreateArgument(ParameterInfo parameter, RequestDelegat
// For complex types, leverage the shared form binding infrastructure. For example,
// shared form binding does not currently only supports types that implement IParsable
// while RDF's binding implementation supports all TryParse implementations.
var useSimpleBinding = parameter.ParameterType == typeof(string) ||
parameter.ParameterType == typeof(StringValues) ||
parameter.ParameterType == typeof(StringValues?) ||
var useSimpleBinding = StringTypes.Contains(parameter.ParameterType) ||
ParameterBindingMethodCache.Instance.HasTryParseMethod(parameter.ParameterType) ||
(parameter.ParameterType.IsArray && ParameterBindingMethodCache.Instance.HasTryParseMethod(parameter.ParameterType.GetElementType()!));
(parameter.ParameterType.IsArray &&
(StringTypes.Contains(parameter.ParameterType.GetElementType()) ||
ParameterBindingMethodCache.Instance.HasTryParseMethod(parameter.ParameterType.GetElementType()!)));
hasTryParse = useSimpleBinding;
return useSimpleBinding
? BindParameterFromFormItem(parameter, formAttribute.Name ?? parameter.Name, factoryContext)
Expand Down
22 changes: 22 additions & 0 deletions src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,28 @@ public async Task RequestDelegateHandlesNullableStringValuesFromExplicitQueryStr
Assert.Null(httpContext.Items["form"]);
}

[Fact]
public async Task RequestDelegateHandlesFromFormStringArrayParameter()
{
var httpContext = CreateHttpContext();
httpContext.Request.Form = new FormCollection(new Dictionary<string, StringValues>
{
["form"] = new(new[] { "1", "2", "3" })
});

var factoryResult = RequestDelegateFactory.Create(
(HttpContext context, [FromForm(Name = "form")] string[] formValues) =>
{
context.Items["form"] = formValues;
});

var requestDelegate = factoryResult.RequestDelegate;

await requestDelegate(httpContext);

Assert.Equal(new StringValues(new[] { "1", "2", "3" }), httpContext.Items["form"]!);
}

[Fact]
public async Task RequestDelegateCanAwaitValueTasksThatAreNotImmediatelyCompleted()
{
Expand Down
Loading