Skip to content

Commit 8a7cf13

Browse files
committed
1 parent 870eec2 commit 8a7cf13

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,8 +1955,18 @@ private static Expression BindParameterFromExpression(
19551955
Expression.Convert(Expression.Constant(parameter.DefaultValue), parameter.ParameterType)));
19561956
}
19571957

1958-
private static Expression BindParameterFromProperty(ParameterInfo parameter, MemberExpression property, PropertyInfo itemProperty, string key, RequestDelegateFactoryContext factoryContext, string source) =>
1959-
BindParameterFromValue(parameter, GetValueFromProperty(property, itemProperty, key, GetExpressionType(parameter.ParameterType)), factoryContext, source);
1958+
private static Expression BindParameterFromProperty(ParameterInfo parameter, MemberExpression property, PropertyInfo itemProperty, string key, RequestDelegateFactoryContext factoryContext, string source)
1959+
{
1960+
var expressionType = GetExpressionType(parameter.ParameterType);
1961+
var valueExpression = (source == "header" && expressionType == typeof(string[]))
1962+
? Expression.Call(
1963+
typeof(ParsingHelpers).GetMethod(nameof(ParsingHelpers.GetHeaderSplit), BindingFlags.Public | BindingFlags.Static, [typeof(IHeaderDictionary), typeof(string)])!,
1964+
property,
1965+
Expression.Constant(key))
1966+
: GetValueFromProperty(property, itemProperty, key, expressionType);
1967+
1968+
return BindParameterFromValue(parameter, valueExpression, factoryContext, source);
1969+
}
19601970

19611971
private static Type? GetExpressionType(Type type) =>
19621972
type.IsArray ? typeof(string[]) :

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,6 +3156,55 @@ static void TestAction([AsParameters] ParameterListRequiredNullableStringFromDif
31563156
Assert.Null(httpContext.Items["RequiredHeaderParam"]);
31573157
}
31583158

3159+
private class ParameterListFromHeaderCommaSeparatedValues
3160+
{
3161+
[FromHeader(Name = "q")]
3162+
public required StringValues? BoundToStringValues { get; set; }
3163+
3164+
[FromHeader(Name = "q")]
3165+
public string? BoundToString { get; set; }
3166+
3167+
[FromHeader(Name = "q")]
3168+
public string[]? BoundToStringArray { get; set; }
3169+
3170+
[FromHeader(Name = "q")]
3171+
public int[]? BoundToIntArray { get; set; }
3172+
}
3173+
3174+
[Theory]
3175+
[InlineData("", new string[] { }, new int[] {})]
3176+
[InlineData(" ", new string[] { }, new int[] { })]
3177+
[InlineData(",", new string[] { }, new int[] { })]
3178+
[InlineData("100", new string[] { "100" }, new int[] { 100 })]
3179+
[InlineData("1,2", new string[] { "1", "2" }, new int[] { 1, 2 })]
3180+
[InlineData("1, 2 , 3", new string[] { "1", "2", "3" }, new int[] { 1, 2, 3 })]
3181+
public async Task RequestDelegateFactory_FromHeader_CommaSeparatedValues(string headerValue, string[] expectedStringArray, int[] expectedIntArray)
3182+
{
3183+
// Arrange
3184+
var httpContext = CreateHttpContext();
3185+
httpContext.Request.Headers["q"] = headerValue;
3186+
3187+
void TestAction([AsParameters] ParameterListFromHeaderCommaSeparatedValues args)
3188+
{
3189+
httpContext.Items[nameof(args.BoundToStringValues)] = args.BoundToStringValues;
3190+
httpContext.Items[nameof(args.BoundToString)] = args.BoundToString;
3191+
httpContext.Items[nameof(args.BoundToStringArray)] = args.BoundToStringArray;
3192+
httpContext.Items[nameof(args.BoundToIntArray)] = args.BoundToIntArray;
3193+
}
3194+
3195+
var factoryResult = RequestDelegateFactory.Create(TestAction);
3196+
var requestDelegate = factoryResult.RequestDelegate;
3197+
3198+
// Act
3199+
await requestDelegate(httpContext);
3200+
3201+
// Assert
3202+
Assert.Equal(headerValue, httpContext.Items[nameof(ParameterListFromHeaderCommaSeparatedValues.BoundToString)]);
3203+
Assert.Equal(new StringValues(headerValue), httpContext.Items[nameof(ParameterListFromHeaderCommaSeparatedValues.BoundToStringValues)]);
3204+
Assert.Equal(expectedStringArray, httpContext.Items[nameof(ParameterListFromHeaderCommaSeparatedValues.BoundToStringArray)]);
3205+
Assert.Equal(expectedIntArray, httpContext.Items[nameof(ParameterListFromHeaderCommaSeparatedValues.BoundToIntArray)]);
3206+
}
3207+
31593208
#nullable disable
31603209
private class ParameterListMixedRequiredStringsFromDifferentSources
31613210
{

0 commit comments

Comments
 (0)