Skip to content

Commit 533eaed

Browse files
committed
Remove null extra features
1 parent ec12b83 commit 533eaed

File tree

5 files changed

+119
-38
lines changed

5 files changed

+119
-38
lines changed

src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.Generated.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,39 @@ internal void ResetFeatureCollection()
106106
return null;
107107
}
108108

109-
private void ExtraFeatureSet(Type key, object value)
109+
private void ExtraFeatureSet(Type key, object? value)
110110
{
111-
if (MaybeExtra == null)
111+
if (value == null)
112112
{
113-
MaybeExtra = new List<KeyValuePair<Type, object>>(2);
113+
if (MaybeExtra == null)
114+
{
115+
return;
116+
}
117+
for (var i = 0; i < MaybeExtra.Count; i++)
118+
{
119+
if (MaybeExtra[i].Key == key)
120+
{
121+
MaybeExtra.RemoveAt(i);
122+
return;
123+
}
124+
}
114125
}
115-
116-
for (var i = 0; i < MaybeExtra.Count; i++)
126+
else
117127
{
118-
if (MaybeExtra[i].Key == key)
128+
if (MaybeExtra == null)
119129
{
120-
MaybeExtra[i] = new KeyValuePair<Type, object>(key, value);
121-
return;
130+
MaybeExtra = new List<KeyValuePair<Type, object>>(2);
131+
}
132+
for (var i = 0; i < MaybeExtra.Count; i++)
133+
{
134+
if (MaybeExtra[i].Key == key)
135+
{
136+
MaybeExtra[i] = new KeyValuePair<Type, object>(key, value);
137+
return;
138+
}
122139
}
140+
MaybeExtra.Add(new KeyValuePair<Type, object>(key, value));
123141
}
124-
MaybeExtra.Add(new KeyValuePair<Type, object>(key, value));
125142
}
126143

127144
bool IFeatureCollection.IsReadOnly => false;

src/Servers/Kestrel/Core/test/Http1HttpProtocolFeatureCollectionTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ public void Http1HasIHttpMinResponseDataRateFeature()
159159
Assert.NotNull(_collection.Get<IHttpMinResponseDataRateFeature>());
160160
}
161161

162+
[Fact]
163+
public void SetExtraFeatureAsNull()
164+
{
165+
_collection[typeof(string)] = null;
166+
Assert.Equal(0, _collection.Count(kv => kv.Key == typeof(string)));
167+
168+
_collection[typeof(string)] = "A string";
169+
Assert.Equal(1, _collection.Count(kv => kv.Key == typeof(string)));
170+
171+
_collection[typeof(string)] = null;
172+
Assert.Equal(0, _collection.Count(kv => kv.Key == typeof(string)));
173+
}
174+
162175
private void CompareGenericGetterToIndexer()
163176
{
164177
Assert.Same(_collection.Get<IHttpRequestFeature>(), _collection[typeof(IHttpRequestFeature)]);

src/Servers/Kestrel/shared/TransportConnection.Generated.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,39 @@ internal void ResetFeatureCollection()
5959
return null;
6060
}
6161

62-
private void ExtraFeatureSet(Type key, object value)
62+
private void ExtraFeatureSet(Type key, object? value)
6363
{
64-
if (MaybeExtra == null)
64+
if (value == null)
6565
{
66-
MaybeExtra = new List<KeyValuePair<Type, object>>(2);
66+
if (MaybeExtra == null)
67+
{
68+
return;
69+
}
70+
for (var i = 0; i < MaybeExtra.Count; i++)
71+
{
72+
if (MaybeExtra[i].Key == key)
73+
{
74+
MaybeExtra.RemoveAt(i);
75+
return;
76+
}
77+
}
6778
}
68-
69-
for (var i = 0; i < MaybeExtra.Count; i++)
79+
else
7080
{
71-
if (MaybeExtra[i].Key == key)
81+
if (MaybeExtra == null)
7282
{
73-
MaybeExtra[i] = new KeyValuePair<Type, object>(key, value);
74-
return;
83+
MaybeExtra = new List<KeyValuePair<Type, object>>(2);
84+
}
85+
for (var i = 0; i < MaybeExtra.Count; i++)
86+
{
87+
if (MaybeExtra[i].Key == key)
88+
{
89+
MaybeExtra[i] = new KeyValuePair<Type, object>(key, value);
90+
return;
91+
}
7592
}
93+
MaybeExtra.Add(new KeyValuePair<Type, object>(key, value));
7694
}
77-
MaybeExtra.Add(new KeyValuePair<Type, object>(key, value));
7895
}
7996

8097
bool IFeatureCollection.IsReadOnly => false;

src/Servers/Kestrel/shared/TransportMultiplexedConnection.Generated.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,39 @@ internal void ResetFeatureCollection()
5959
return null;
6060
}
6161

62-
private void ExtraFeatureSet(Type key, object value)
62+
private void ExtraFeatureSet(Type key, object? value)
6363
{
64-
if (MaybeExtra == null)
64+
if (value == null)
6565
{
66-
MaybeExtra = new List<KeyValuePair<Type, object>>(2);
66+
if (MaybeExtra == null)
67+
{
68+
return;
69+
}
70+
for (var i = 0; i < MaybeExtra.Count; i++)
71+
{
72+
if (MaybeExtra[i].Key == key)
73+
{
74+
MaybeExtra.RemoveAt(i);
75+
return;
76+
}
77+
}
6778
}
68-
69-
for (var i = 0; i < MaybeExtra.Count; i++)
79+
else
7080
{
71-
if (MaybeExtra[i].Key == key)
81+
if (MaybeExtra == null)
7282
{
73-
MaybeExtra[i] = new KeyValuePair<Type, object>(key, value);
74-
return;
83+
MaybeExtra = new List<KeyValuePair<Type, object>>(2);
84+
}
85+
for (var i = 0; i < MaybeExtra.Count; i++)
86+
{
87+
if (MaybeExtra[i].Key == key)
88+
{
89+
MaybeExtra[i] = new KeyValuePair<Type, object>(key, value);
90+
return;
91+
}
7592
}
93+
MaybeExtra.Add(new KeyValuePair<Type, object>(key, value));
7694
}
77-
MaybeExtra.Add(new KeyValuePair<Type, object>(key, value));
7895
}
7996

8097
bool IFeatureCollection.IsReadOnly => false;

src/Servers/Kestrel/tools/CodeGenerator/FeatureCollectionGenerator.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,39 @@ internal void ResetFeatureCollection()
7171
return null;
7272
}}
7373

74-
private void ExtraFeatureSet(Type key, object value)
74+
private void ExtraFeatureSet(Type key, object? value)
7575
{{
76-
if (MaybeExtra == null)
76+
if (value == null)
7777
{{
78-
MaybeExtra = new List<KeyValuePair<Type, object>>(2);
78+
if (MaybeExtra == null)
79+
{{
80+
return;
81+
}}
82+
for (var i = 0; i < MaybeExtra.Count; i++)
83+
{{
84+
if (MaybeExtra[i].Key == key)
85+
{{
86+
MaybeExtra.RemoveAt(i);
87+
return;
88+
}}
89+
}}
7990
}}
80-
81-
for (var i = 0; i < MaybeExtra.Count; i++)
91+
else
8292
{{
83-
if (MaybeExtra[i].Key == key)
93+
if (MaybeExtra == null)
8494
{{
85-
MaybeExtra[i] = new KeyValuePair<Type, object>(key, value);
86-
return;
95+
MaybeExtra = new List<KeyValuePair<Type, object>>(2);
96+
}}
97+
for (var i = 0; i < MaybeExtra.Count; i++)
98+
{{
99+
if (MaybeExtra[i].Key == key)
100+
{{
101+
MaybeExtra[i] = new KeyValuePair<Type, object>(key, value);
102+
return;
103+
}}
87104
}}
105+
MaybeExtra.Add(new KeyValuePair<Type, object>(key, value));
88106
}}
89-
MaybeExtra.Add(new KeyValuePair<Type, object>(key, value));
90107
}}
91108

92109
bool IFeatureCollection.IsReadOnly => false;
@@ -120,7 +137,7 @@ private void ExtraFeatureSet(Type key, object value)
120137
}}")}
121138
else
122139
{{
123-
ExtraFeatureSet(key, value!); // TODO: What happens if you set an extra feature with a null value?
140+
ExtraFeatureSet(key, value);
124141
}}
125142
}}
126143
}}
@@ -154,7 +171,7 @@ void IFeatureCollection.Set<TFeature>(TFeature feature)
154171
}}")}
155172
else
156173
{{
157-
ExtraFeatureSet(typeof(TFeature), feature!); // TODO: What happens if you set an extra feature with a null value?
174+
ExtraFeatureSet(typeof(TFeature), feature);
158175
}}
159176
}}
160177

0 commit comments

Comments
 (0)