Skip to content

Commit af9a739

Browse files
committed
Fix binder bug with key=value and array
1 parent 8bdc630 commit af9a739

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/Hosting/ResponseHeadersHelper.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ internal static class ResponseHeadersHelper
1313
nameof(BaseResponseHeadersConfigurationOptions.ContentType),
1414
nameof(BaseResponseHeadersConfigurationOptions.ContentTypeContain),
1515
nameof(BaseResponseHeadersConfigurationOptions.ContentTypeStartWith),
16-
nameof(BaseResponseHeadersConfigurationOptions.Headers),
16+
// nameof(BaseResponseHeadersConfigurationOptions.Headers), binder cause bug between array and key value
17+
"Headers",
1718
nameof(BaseResponseHeadersConfigurationOptions.StatusCode),
1819
nameof(ResponseHeadersConfigurationOptions.IsEnabled),
1920
"IsAnyContentType", // legacy value
@@ -67,9 +68,14 @@ private static ResponseHandlerEntry ParseHandler(BaseResponseHeadersConfiguratio
6768
{
6869
var headers = new Dictionary<string, string>();
6970

70-
foreach (var item in options.Headers ?? [])
71+
foreach (var item in configuration.GetSection("Headers").GetChildren())
7172
{
72-
if (TryParseKeyValue(item, out var key, out var value)
73+
if (item.Value is { } value1
74+
&& item.Key is { } key1
75+
&& !string.IsNullOrEmpty(key1)
76+
&& !string.IsNullOrEmpty(value1)
77+
&& int.TryParse(key1, out var _)
78+
&& TryParseKeyValue(value1, out var key, out var value)
7379
&& !string.IsNullOrEmpty(key)
7480
&& !string.IsNullOrEmpty(value))
7581
{
@@ -115,11 +121,15 @@ private static ResponseHandlerEntry ParseHandler(BaseResponseHeadersConfiguratio
115121
headers[key] = value;
116122
}
117123
}
118-
else if (nameof(BaseResponseHeadersConfigurationOptions.Headers).Equals(key, DefaultStringComparison))
124+
else if ("Headers".Equals(key, DefaultStringComparison)) // nameof(BaseResponseHeadersConfigurationOptions.Headers)
119125
{
120126
foreach (var item1 in item.GetChildren())
121127
{
122-
if (item1.Value is { } value1 && item1.Key is { } key1 && !string.IsNullOrEmpty(key1) && !string.IsNullOrEmpty(value1) && !int.TryParse(key1, out var _))
128+
if (item1.Value is { } value1
129+
&& item1.Key is { } key1
130+
&& !string.IsNullOrEmpty(key1)
131+
&& !string.IsNullOrEmpty(value1)
132+
&& !int.TryParse(key1, out var _))
123133
{
124134
headers[key1] = value1;
125135
}

src/Hosting/ResponseHeadersOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal class BaseResponseHeadersConfigurationOptions
88
public HashSet<string>? ContentType { get; set; }
99
public HashSet<string>? ContentTypeContain { get; set; }
1010
public HashSet<string>? ContentTypeStartWith { get; set; }
11-
public List<string>? Headers { get; set; }
11+
// public List<string>? Headers { get; set; } binder cause bug between array and key value
1212
}
1313

1414
internal class ResponseHeadersConfigurationOptions : BaseResponseHeadersConfigurationOptions

test/Hosting.Test/ResponseHeadersHelperTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,4 +656,31 @@ public void HeaderAllTest()
656656
["x-header25"] = "value26",
657657
}, handler.Headers);
658658
}
659+
660+
[Fact]
661+
public void HeaderKeyValueWithEqualTest()
662+
{
663+
var configuration = new ConfigurationManager();
664+
configuration.AddInMemoryCollection(new Dictionary<string, string?>
665+
{
666+
["ResponseHeaders:Headers:x-header28"] = "value29=hex",
667+
});
668+
669+
var options = Parse(configuration, "ResponseHeaders");
670+
671+
Assert.NotNull(options);
672+
Assert.NotNull(options.DefaultHandler);
673+
Assert.Empty(options.Handlers);
674+
675+
var handler = options.DefaultHandler;
676+
Assert.NotNull(handler);
677+
Assert.Empty(handler.ContentTypeMatchEq);
678+
Assert.Empty(handler.ContentTypeMatchContain);
679+
Assert.Empty(handler.ContentTypeMatchStartWith);
680+
Assert.Empty(handler.StatusCode);
681+
Assert.Equal(new Dictionary<string, StringValues>
682+
{
683+
["x-header28"] = "value29=hex",
684+
}, handler.Headers);
685+
}
659686
}

0 commit comments

Comments
 (0)