Skip to content

Commit 4fbdb1d

Browse files
Fix key comparison for more than 10 elements in AdaptiveCapacityDictionary (#35705)
Co-authored-by: Brennan <[email protected]>
1 parent a5f72e7 commit 4fbdb1d

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/Http/Http/test/Features/QueryFeatureTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,39 @@ public void ParseQueryWithEncodedKeyEncodedValuesWorks()
220220
Assert.Single(queryCollection);
221221
Assert.Equal(new[] { "[ 1 ]", "[ 2 ]" }, queryCollection["fields [todoItems]"]);
222222
}
223+
224+
[Fact]
225+
public void CaseInsensitiveWithManyKeys()
226+
{
227+
// need to use over 10 keys to test dictionary storage code path
228+
var features = new FeatureCollection();
229+
features[typeof(IHttpRequestFeature)] = new HttpRequestFeature
230+
{
231+
QueryString = "?a=0&b=0&c=1&d=2&e=3&f=4&g=5&h=6&i=7&j=8&k=9&" +
232+
"key=1&Key=2&key=3&Key=4&KEy=5&KEY=6&kEY=7&KeY=8&kEy=9&keY=10"
233+
};
234+
235+
var provider = new QueryFeature(features);
236+
237+
var queryCollection = provider.Query;
238+
239+
Assert.Equal(12, queryCollection.Count);
240+
Assert.Equal(new[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }, queryCollection["KEY"]);
241+
}
242+
243+
[Fact]
244+
public void CaseInsensitiveWithFewKeys()
245+
{
246+
// need to use less than 10 keys to test array storage code path
247+
var features = new FeatureCollection();
248+
features[typeof(IHttpRequestFeature)] = new HttpRequestFeature { QueryString = "?key=1&Key=2&key=3&Key=4&KEy=5" };
249+
250+
var provider = new QueryFeature(features);
251+
252+
var queryCollection = provider.Query;
253+
254+
Assert.Equal(1, queryCollection.Count);
255+
Assert.Equal(new[] { "1", "2", "3", "4", "5" }, queryCollection["KEY"]);
256+
}
223257
}
224258
}

src/Shared/Dictionary/AdaptiveCapacityDictionary.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public AdaptiveCapacityDictionary(int capacity, IEqualityComparer<TKey> comparer
7676
}
7777
else
7878
{
79-
_dictionaryStorage = new Dictionary<TKey, TValue>(capacity);
79+
_dictionaryStorage = new Dictionary<TKey, TValue>(capacity, _comparer);
8080
}
8181
}
8282

@@ -519,7 +519,7 @@ private void EnsureCapacitySlow(int capacity)
519519

520520
if (capacity > DefaultArrayThreshold)
521521
{
522-
_dictionaryStorage = new Dictionary<TKey, TValue>(capacity);
522+
_dictionaryStorage = new Dictionary<TKey, TValue>(capacity, _comparer);
523523
foreach (var item in _arrayStorage)
524524
{
525525
_dictionaryStorage[item.Key] = item.Value;

src/Shared/test/Shared.Tests/AdaptiveCapacityDictionaryTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,42 @@ public void ListStorage_RemoveAt_RearrangesInnerArray()
12791279
Assert.Equal("value3", storage[1].Value);
12801280
}
12811281

1282+
[Fact]
1283+
public void UpgradeToDictionary_KeepsComparer()
1284+
{
1285+
// Arrange
1286+
var comparer = StringComparer.OrdinalIgnoreCase;
1287+
var dict = new AdaptiveCapacityDictionary<string, object>(StringComparer.OrdinalIgnoreCase);
1288+
for (var i = 0; i < 11; i++)
1289+
{
1290+
dict[i.ToString()] = i;
1291+
}
1292+
1293+
Assert.NotNull(dict._dictionaryStorage);
1294+
Assert.Equal(comparer, dict._dictionaryStorage.Comparer);
1295+
1296+
dict["K"] = 1;
1297+
dict["k"] = 2;
1298+
1299+
Assert.Equal(2, dict["K"]);
1300+
}
1301+
1302+
[Fact]
1303+
public void StartAsDictionary_UsesComparer()
1304+
{
1305+
// Arrange
1306+
var comparer = StringComparer.OrdinalIgnoreCase;
1307+
var dict = new AdaptiveCapacityDictionary<string, object>(11, StringComparer.OrdinalIgnoreCase);
1308+
1309+
Assert.NotNull(dict._dictionaryStorage);
1310+
Assert.Equal(comparer, dict._dictionaryStorage.Comparer);
1311+
1312+
dict["K"] = 1;
1313+
dict["k"] = 2;
1314+
1315+
Assert.Equal(2, dict["K"]);
1316+
}
1317+
12821318
private void AssertEmptyArrayStorage(AdaptiveCapacityDictionary<string, string> value)
12831319
{
12841320
Assert.Same(Array.Empty<KeyValuePair<string, object?>>(), value._arrayStorage);

0 commit comments

Comments
 (0)