Skip to content

Commit 1902aa2

Browse files
authored
Enable nullable on Kestrel generated code (#28792)
1 parent 7e5588b commit 1902aa2

27 files changed

+282
-177
lines changed

src/Hosting/TestHost/src/ClientHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
184184
// Copy trailers to the response message when the response stream is complete
185185
contextBuilder.RegisterResponseReadCompleteCallback(context =>
186186
{
187-
var responseTrailersFeature = context.Features.Get<IHttpResponseTrailersFeature>();
187+
var responseTrailersFeature = context.Features.Get<IHttpResponseTrailersFeature>()!;
188188

189189
foreach (var trailer in responseTrailersFeature.Trailers)
190190
{
@@ -196,7 +196,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
196196
var httpContext = await contextBuilder.SendAsync(cancellationToken);
197197

198198
response.StatusCode = (HttpStatusCode)httpContext.Response.StatusCode;
199-
response.ReasonPhrase = httpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase;
199+
response.ReasonPhrase = httpContext.Features.Get<IHttpResponseFeature>()!.ReasonPhrase;
200200
response.RequestMessage = request;
201201
response.Version = request.Version;
202202

src/Hosting/TestHost/src/HttpContextBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ internal async Task ReturnResponseMessageAsync()
225225
{
226226
newFeatures[pair.Key] = pair.Value;
227227
}
228-
var serverResponseFeature = _httpContext.Features.Get<IHttpResponseFeature>();
228+
var serverResponseFeature = _httpContext.Features.Get<IHttpResponseFeature>()!;
229229
// The client gets a deep copy of this so they can interact with the body stream independently of the server.
230230
var clientResponseFeature = new HttpResponseFeature()
231231
{

src/Http/Http.Extensions/src/ResponseExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void Clear(this HttpResponse response)
1616
throw new InvalidOperationException("The response cannot be cleared, it has already started sending.");
1717
}
1818
response.StatusCode = 200;
19-
response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = null;
19+
response.HttpContext.Features.Get<IHttpResponseFeature>()!.ReasonPhrase = null;
2020
response.Headers.Clear();
2121
if (response.Body.CanSeek)
2222
{

src/Http/Http.Extensions/src/SendFileResponseExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private static async Task SendFileAsyncCore(HttpResponse response, string fileNa
143143
{
144144
var useRequestAborted = !cancellationToken.CanBeCanceled;
145145
var localCancel = useRequestAborted ? response.HttpContext.RequestAborted : cancellationToken;
146-
var sendFile = response.HttpContext.Features.Get<IHttpResponseBodyFeature>();
146+
var sendFile = response.HttpContext.Features.Get<IHttpResponseBodyFeature>()!;
147147

148148
try
149149
{

src/Http/Http.Features/src/FeatureCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public IEnumerator<KeyValuePair<Type, object>> GetEnumerator()
9898
return (TFeature?)this[typeof(TFeature)];
9999
}
100100

101-
public void Set<TFeature>(TFeature instance)
101+
public void Set<TFeature>(TFeature? instance)
102102
{
103103
this[typeof(TFeature)] = instance;
104104
}

src/Http/Http.Features/src/IFeatureCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ public interface IFeatureCollection : IEnumerable<KeyValuePair<Type, object>>
3333
/// </summary>
3434
/// <typeparam name="TFeature">The feature key.</typeparam>
3535
/// <returns>The requested feature, or null if it is not present.</returns>
36-
TFeature Get<TFeature>();
36+
TFeature? Get<TFeature>();
3737

3838
/// <summary>
3939
/// Sets the given feature in the collection.
4040
/// </summary>
4141
/// <typeparam name="TFeature">The feature key.</typeparam>
4242
/// <param name="instance">The feature value.</param>
43-
void Set<TFeature>(TFeature instance);
43+
void Set<TFeature>(TFeature? instance);
4444
}
4545
}

src/Http/Http.Features/src/PublicAPI.Shipped.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Microsoft.AspNetCore.Http.Features.FeatureCollection.FeatureCollection(Microsoft
2626
Microsoft.AspNetCore.Http.Features.FeatureCollection.Get<TFeature>() -> TFeature?
2727
Microsoft.AspNetCore.Http.Features.FeatureCollection.GetEnumerator() -> System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<System.Type!, object!>>!
2828
Microsoft.AspNetCore.Http.Features.FeatureCollection.IsReadOnly.get -> bool
29-
Microsoft.AspNetCore.Http.Features.FeatureCollection.Set<TFeature>(TFeature instance) -> void
29+
Microsoft.AspNetCore.Http.Features.FeatureCollection.Set<TFeature>(TFeature? instance) -> void
3030
Microsoft.AspNetCore.Http.Features.FeatureCollection.this[System.Type! key].get -> object?
3131
Microsoft.AspNetCore.Http.Features.FeatureCollection.this[System.Type! key].set -> void
3232
Microsoft.AspNetCore.Http.Features.FeatureReference<T>
@@ -46,10 +46,10 @@ Microsoft.AspNetCore.Http.Features.HttpsCompressionMode.Compress = 2 -> Microsof
4646
Microsoft.AspNetCore.Http.Features.HttpsCompressionMode.Default = 0 -> Microsoft.AspNetCore.Http.Features.HttpsCompressionMode
4747
Microsoft.AspNetCore.Http.Features.HttpsCompressionMode.DoNotCompress = 1 -> Microsoft.AspNetCore.Http.Features.HttpsCompressionMode
4848
Microsoft.AspNetCore.Http.Features.IFeatureCollection
49-
Microsoft.AspNetCore.Http.Features.IFeatureCollection.Get<TFeature>() -> TFeature
49+
Microsoft.AspNetCore.Http.Features.IFeatureCollection.Get<TFeature>() -> TFeature?
5050
Microsoft.AspNetCore.Http.Features.IFeatureCollection.IsReadOnly.get -> bool
5151
Microsoft.AspNetCore.Http.Features.IFeatureCollection.Revision.get -> int
52-
Microsoft.AspNetCore.Http.Features.IFeatureCollection.Set<TFeature>(TFeature instance) -> void
52+
Microsoft.AspNetCore.Http.Features.IFeatureCollection.Set<TFeature>(TFeature? instance) -> void
5353
Microsoft.AspNetCore.Http.Features.IFeatureCollection.this[System.Type! key].get -> object?
5454
Microsoft.AspNetCore.Http.Features.IFeatureCollection.this[System.Type! key].set -> void
5555
Microsoft.AspNetCore.Http.Features.IFormFeature

src/Http/Http/src/Internal/DefaultHttpResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public override Stream Body
6969
get { return HttpResponseBodyFeature.Stream; }
7070
set
7171
{
72-
var otherFeature = _features.Collection.Get<IHttpResponseBodyFeature>();
72+
var otherFeature = _features.Collection.Get<IHttpResponseBodyFeature>()!;
7373

7474
if (otherFeature is StreamResponseBodyFeature streamFeature
7575
&& streamFeature.PriorFeature != null

src/Http/Http/src/Internal/ResponseCookies.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal class ResponseCookies : IResponseCookies
2828
internal ResponseCookies(IFeatureCollection features)
2929
{
3030
_features = features;
31-
Headers = _features.Get<IHttpResponseFeature>().Headers;
31+
Headers = _features.Get<IHttpResponseFeature>()!.Headers;
3232
}
3333

3434
private IHeaderDictionary Headers { get; set; }

src/Middleware/ResponseCompression/src/ResponseCompressionMiddleware.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics;
56
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.Http;
78
using Microsoft.AspNetCore.Http.Features;
@@ -54,6 +55,8 @@ public async Task Invoke(HttpContext context)
5455
var originalBodyFeature = context.Features.Get<IHttpResponseBodyFeature>();
5556
var originalCompressionFeature = context.Features.Get<IHttpsCompressionFeature>();
5657

58+
Debug.Assert(originalBodyFeature != null);
59+
5760
var compressionBody = new ResponseCompressionBody(context, _provider, originalBodyFeature);
5861
context.Features.Set<IHttpResponseBodyFeature>(compressionBody);
5962
context.Features.Set<IHttpsCompressionFeature>(compressionBody);

src/Middleware/Rewrite/src/UrlActions/CustomResponseAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public override void ApplyAction(RewriteContext context, BackReferenceCollection
2626

2727
if (!string.IsNullOrEmpty(StatusReason))
2828
{
29-
context.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = StatusReason;
29+
context.HttpContext.Features.Get<IHttpResponseFeature>()!.ReasonPhrase = StatusReason;
3030
}
3131

3232
if (!string.IsNullOrEmpty(StatusDescription))

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using Microsoft.Net.Http.Headers;
1313
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
1414

15+
#nullable enable
16+
1517
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
1618
{
1719
internal enum KnownHeaderType
@@ -6712,7 +6714,7 @@ protected override bool CopyToFast(KeyValuePair<string, StringValues>[] array, i
67126714
array[arrayIndex] = new KeyValuePair<string, StringValues>(HeaderNames.ContentLength, HeaderUtilities.FormatNonNegativeInt64(_contentLength.Value));
67136715
++arrayIndex;
67146716
}
6715-
((ICollection<KeyValuePair<string, StringValues>>)MaybeUnknown)?.CopyTo(array, arrayIndex);
6717+
((ICollection<KeyValuePair<string, StringValues>>?)MaybeUnknown)?.CopyTo(array, arrayIndex);
67166718

67176719
return true;
67186720
}
@@ -12724,7 +12726,7 @@ protected override bool CopyToFast(KeyValuePair<string, StringValues>[] array, i
1272412726
array[arrayIndex] = new KeyValuePair<string, StringValues>(HeaderNames.ContentLength, HeaderUtilities.FormatNonNegativeInt64(_contentLength.Value));
1272512727
++arrayIndex;
1272612728
}
12727-
((ICollection<KeyValuePair<string, StringValues>>)MaybeUnknown)?.CopyTo(array, arrayIndex);
12729+
((ICollection<KeyValuePair<string, StringValues>>?)MaybeUnknown)?.CopyTo(array, arrayIndex);
1272812730

1272912731
return true;
1273012732
}
@@ -12816,7 +12818,7 @@ internal unsafe void CopyToFast(ref BufferWriter<PipeWriter> output)
1281612818
{
1281712819
tempBits ^= 0x8000000000000000L;
1281812820
output.Write(HeaderBytes.Slice(640, 18));
12819-
output.WriteNumeric((ulong)ContentLength.Value);
12821+
output.WriteNumeric((ulong)ContentLength.GetValueOrDefault());
1282012822
if (tempBits == 0)
1282112823
{
1282212824
return;
@@ -13266,10 +13268,10 @@ private struct HeaderReferences
1326613268
public StringValues _AccessControlExposeHeaders;
1326713269
public StringValues _AccessControlMaxAge;
1326813270

13269-
public byte[] _rawConnection;
13270-
public byte[] _rawDate;
13271-
public byte[] _rawTransferEncoding;
13272-
public byte[] _rawServer;
13271+
public byte[]? _rawConnection;
13272+
public byte[]? _rawDate;
13273+
public byte[]? _rawTransferEncoding;
13274+
public byte[]? _rawServer;
1327313275
}
1327413276

1327513277
public partial struct Enumerator
@@ -14149,7 +14151,7 @@ protected override bool CopyToFast(KeyValuePair<string, StringValues>[] array, i
1414914151
array[arrayIndex] = new KeyValuePair<string, StringValues>(HeaderNames.ContentLength, HeaderUtilities.FormatNonNegativeInt64(_contentLength.Value));
1415014152
++arrayIndex;
1415114153
}
14152-
((ICollection<KeyValuePair<string, StringValues>>)MaybeUnknown)?.CopyTo(array, arrayIndex);
14154+
((ICollection<KeyValuePair<string, StringValues>>?)MaybeUnknown)?.CopyTo(array, arrayIndex);
1415314155

1415414156
return true;
1415514157
}

0 commit comments

Comments
 (0)