Skip to content

Commit f269428

Browse files
authored
Turn on nullability for Routing (#24238)
* Turn on nullability for Routing We previously only had annotations enabled which resulted in incorrect nullability. This change enables nullability. Fixes #24042
1 parent 64b5ea9 commit f269428

File tree

100 files changed

+344
-287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+344
-287
lines changed

src/Http/Http.Abstractions/src/Routing/Endpoint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class Endpoint
2020
/// </param>
2121
public Endpoint(
2222
RequestDelegate requestDelegate,
23-
EndpointMetadataCollection metadata,
24-
string displayName)
23+
EndpointMetadataCollection? metadata,
24+
string? displayName)
2525
{
2626
// All are allowed to be null
2727
RequestDelegate = requestDelegate;
@@ -32,7 +32,7 @@ public Endpoint(
3232
/// <summary>
3333
/// Gets the informational display name of this endpoint.
3434
/// </summary>
35-
public string DisplayName { get; }
35+
public string? DisplayName { get; }
3636

3737
/// <summary>
3838
/// Gets the collection of metadata associated with this endpoint.

src/Http/Routing.Abstractions/src/IRouteConstraint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface IRouteConstraint : IParameterPolicy
2525
/// <returns><c>true</c> if the URL parameter contains a valid value; otherwise, <c>false</c>.</returns>
2626
bool Match(
2727
HttpContext? httpContext,
28-
IRouter route,
28+
IRouter? route,
2929
string routeKey,
3030
RouteValueDictionary values,
3131
RouteDirection routeDirection);

src/Http/Routing/src/ArrayBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
// See https://github.com/dotnet/corefx/blob/143df51926f2ad397fef9c9ca7ede88e2721e801/src/Common/src/System/Collections/Generic/ArrayBuilder.cs
88

9+
#nullable disable
910

1011
using System;
1112
using System.Diagnostics;

src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static void VerifyEndpointRoutingMiddlewareIsRegistered(IApplicationBuil
128128
}
129129

130130
// If someone messes with this, just let it crash.
131-
endpointRouteBuilder = (DefaultEndpointRouteBuilder)obj;
131+
endpointRouteBuilder = (DefaultEndpointRouteBuilder)obj!;
132132

133133
// This check handles the case where Map or something else that forks the pipeline is called between the two
134134
// routing middleware.

src/Http/Routing/src/CompositeEndpointDataSource.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
77
using System.Collections.Specialized;
88
using System.Diagnostics;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.Linq;
1011
using System.Text;
1112
using System.Threading;
@@ -21,8 +22,8 @@ namespace Microsoft.AspNetCore.Routing
2122
public sealed class CompositeEndpointDataSource : EndpointDataSource
2223
{
2324
private readonly object _lock;
24-
private readonly ICollection<EndpointDataSource> _dataSources;
25-
private IReadOnlyList<Endpoint> _endpoints;
25+
private readonly ICollection<EndpointDataSource> _dataSources = default!;
26+
private IReadOnlyList<Endpoint> _endpoints = default!;
2627
private IChangeToken _consumerChangeToken;
2728
private CancellationTokenSource _cts;
2829

@@ -49,7 +50,7 @@ public CompositeEndpointDataSource(IEnumerable<EndpointDataSource> endpointDataS
4950
}
5051
}
5152

52-
private void OnDataSourcesChanged(object sender, NotifyCollectionChangedEventArgs e)
53+
private void OnDataSourcesChanged(object? sender, NotifyCollectionChangedEventArgs e)
5354
{
5455
lock (_lock)
5556
{
@@ -140,6 +141,7 @@ private void HandleChange()
140141
}
141142
}
142143

144+
[MemberNotNull(nameof(_cts), nameof(_consumerChangeToken))]
143145
private void CreateChangeToken()
144146
{
145147
_cts = new CancellationTokenSource();
@@ -198,7 +200,7 @@ private string DebuggerDisplayString
198200
}
199201
return sb.ToString();
200202

201-
IEnumerable<string> FormatValues(IEnumerable<KeyValuePair<string, object>> values)
203+
IEnumerable<string> FormatValues(IEnumerable<KeyValuePair<string, object?>> values)
202204
{
203205
return values.Select(
204206
kvp =>

src/Http/Routing/src/Constraints/BoolRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class BoolRouteConstraint : IRouteConstraint
1414
{
1515
/// <inheritdoc />
1616
public bool Match(
17-
HttpContext httpContext,
18-
IRouter route,
17+
HttpContext? httpContext,
18+
IRouter? route,
1919
string routeKey,
2020
RouteValueDictionary values,
2121
RouteDirection routeDirection)
@@ -44,4 +44,4 @@ public bool Match(
4444
return false;
4545
}
4646
}
47-
}
47+
}

src/Http/Routing/src/Constraints/CompositeRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public CompositeRouteConstraint(IEnumerable<IRouteConstraint> constraints)
3333

3434
/// <inheritdoc />
3535
public bool Match(
36-
HttpContext httpContext,
37-
IRouter route,
36+
HttpContext? httpContext,
37+
IRouter? route,
3838
string routeKey,
3939
RouteValueDictionary values,
4040
RouteDirection routeDirection)
@@ -60,4 +60,4 @@ public bool Match(
6060
return true;
6161
}
6262
}
63-
}
63+
}

src/Http/Routing/src/Constraints/DateTimeRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class DateTimeRouteConstraint : IRouteConstraint
2020
{
2121
/// <inheritdoc />
2222
public bool Match(
23-
HttpContext httpContext,
24-
IRouter route,
23+
HttpContext? httpContext,
24+
IRouter? route,
2525
string routeKey,
2626
RouteValueDictionary values,
2727
RouteDirection routeDirection)
@@ -50,4 +50,4 @@ public bool Match(
5050
return false;
5151
}
5252
}
53-
}
53+
}

src/Http/Routing/src/Constraints/DecimalRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class DecimalRouteConstraint : IRouteConstraint
1414
{
1515
/// <inheritdoc />
1616
public bool Match(
17-
HttpContext httpContext,
18-
IRouter route,
17+
HttpContext? httpContext,
18+
IRouter? route,
1919
string routeKey,
2020
RouteValueDictionary values,
2121
RouteDirection routeDirection)
@@ -44,4 +44,4 @@ public bool Match(
4444
return false;
4545
}
4646
}
47-
}
47+
}

src/Http/Routing/src/Constraints/DoubleRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class DoubleRouteConstraint : IRouteConstraint
1414
{
1515
/// <inheritdoc />
1616
public bool Match(
17-
HttpContext httpContext,
18-
IRouter route,
17+
HttpContext? httpContext,
18+
IRouter? route,
1919
string routeKey,
2020
RouteValueDictionary values,
2121
RouteDirection routeDirection)
@@ -48,4 +48,4 @@ public bool Match(
4848
return false;
4949
}
5050
}
51-
}
51+
}

src/Http/Routing/src/Constraints/FileNameRouteConstraint.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public class FileNameRouteConstraint : IRouteConstraint
8585
{
8686
/// <inheritdoc />
8787
public bool Match(
88-
HttpContext httpContext,
89-
IRouter route,
88+
HttpContext? httpContext,
89+
IRouter? route,
9090
string routeKey,
9191
RouteValueDictionary values,
9292
RouteDirection routeDirection)

src/Http/Routing/src/Constraints/FloatRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class FloatRouteConstraint : IRouteConstraint
1414
{
1515
/// <inheritdoc />
1616
public bool Match(
17-
HttpContext httpContext,
18-
IRouter route,
17+
HttpContext? httpContext,
18+
IRouter? route,
1919
string routeKey,
2020
RouteValueDictionary values,
2121
RouteDirection routeDirection)
@@ -48,4 +48,4 @@ public bool Match(
4848
return false;
4949
}
5050
}
51-
}
51+
}

src/Http/Routing/src/Constraints/GuidRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class GuidRouteConstraint : IRouteConstraint
1616
{
1717
/// <inheritdoc />
1818
public bool Match(
19-
HttpContext httpContext,
20-
IRouter route,
19+
HttpContext? httpContext,
20+
IRouter? route,
2121
string routeKey,
2222
RouteValueDictionary values,
2323
RouteDirection routeDirection)
@@ -46,4 +46,4 @@ public bool Match(
4646
return false;
4747
}
4848
}
49-
}
49+
}

src/Http/Routing/src/Constraints/HttpMethodRouteConstraint.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public HttpMethodRouteConstraint(params string[] allowedMethods)
3535

3636
/// <inheritdoc />
3737
public virtual bool Match(
38-
HttpContext httpContext,
39-
IRouter route,
38+
HttpContext? httpContext,
39+
IRouter? route,
4040
string routeKey,
4141
RouteValueDictionary values,
4242
RouteDirection routeDirection)

src/Http/Routing/src/Constraints/IntRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class IntRouteConstraint : IRouteConstraint
1414
{
1515
/// <inheritdoc />
1616
public bool Match(
17-
HttpContext httpContext,
18-
IRouter route,
17+
HttpContext? httpContext,
18+
IRouter? route,
1919
string routeKey,
2020
RouteValueDictionary values,
2121
RouteDirection routeDirection)
@@ -44,4 +44,4 @@ public bool Match(
4444
return false;
4545
}
4646
}
47-
}
47+
}

src/Http/Routing/src/Constraints/LengthRouteConstraint.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public LengthRouteConstraint(int minLength, int maxLength)
7171

7272
/// <inheritdoc />
7373
public bool Match(
74-
HttpContext httpContext,
75-
IRouter route,
74+
HttpContext? httpContext,
75+
IRouter? route,
7676
string routeKey,
7777
RouteValueDictionary values,
7878
RouteDirection routeDirection)
@@ -89,12 +89,12 @@ public bool Match(
8989

9090
if (values.TryGetValue(routeKey, out var value) && value != null)
9191
{
92-
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
92+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture)!;
9393
var length = valueString.Length;
9494
return length >= MinLength && length <= MaxLength;
9595
}
9696

9797
return false;
9898
}
9999
}
100-
}
100+
}

src/Http/Routing/src/Constraints/LongRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class LongRouteConstraint : IRouteConstraint
1414
{
1515
/// <inheritdoc />
1616
public bool Match(
17-
HttpContext httpContext,
18-
IRouter route,
17+
HttpContext? httpContext,
18+
IRouter? route,
1919
string routeKey,
2020
RouteValueDictionary values,
2121
RouteDirection routeDirection)
@@ -44,4 +44,4 @@ public bool Match(
4444
return false;
4545
}
4646
}
47-
}
47+
}

src/Http/Routing/src/Constraints/MaxLengthRouteConstraint.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public MaxLengthRouteConstraint(int maxLength)
3434

3535
/// <inheritdoc />
3636
public bool Match(
37-
HttpContext httpContext,
38-
IRouter route,
37+
HttpContext? httpContext,
38+
IRouter? route,
3939
string routeKey,
4040
RouteValueDictionary values,
4141
RouteDirection routeDirection)
@@ -52,11 +52,11 @@ public bool Match(
5252

5353
if (values.TryGetValue(routeKey, out var value) && value != null)
5454
{
55-
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
55+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture)!;
5656
return valueString.Length <= MaxLength;
5757
}
5858

5959
return false;
6060
}
6161
}
62-
}
62+
}

src/Http/Routing/src/Constraints/MaxRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public MaxRouteConstraint(long max)
2828

2929
/// <inheritdoc />
3030
public bool Match(
31-
HttpContext httpContext,
32-
IRouter route,
31+
HttpContext? httpContext,
32+
IRouter? route,
3333
string routeKey,
3434
RouteValueDictionary values,
3535
RouteDirection routeDirection)
@@ -56,4 +56,4 @@ public bool Match(
5656
return false;
5757
}
5858
}
59-
}
59+
}

src/Http/Routing/src/Constraints/MinLengthRouteConstraint.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public MinLengthRouteConstraint(int minLength)
3434

3535
/// <inheritdoc />
3636
public bool Match(
37-
HttpContext httpContext,
38-
IRouter route,
37+
HttpContext? httpContext,
38+
IRouter? route,
3939
string routeKey,
4040
RouteValueDictionary values,
4141
RouteDirection routeDirection)
@@ -52,11 +52,11 @@ public bool Match(
5252

5353
if (values.TryGetValue(routeKey, out var value) && value != null)
5454
{
55-
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
55+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture)!;
5656
return valueString.Length >= MinLength;
5757
}
5858

5959
return false;
6060
}
6161
}
62-
}
62+
}

src/Http/Routing/src/Constraints/MinRouteConstraint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public MinRouteConstraint(long min)
2828

2929
/// <inheritdoc />
3030
public bool Match(
31-
HttpContext httpContext,
32-
IRouter route,
31+
HttpContext? httpContext,
32+
IRouter? route,
3333
string routeKey,
3434
RouteValueDictionary values,
3535
RouteDirection routeDirection)
@@ -56,4 +56,4 @@ public bool Match(
5656
return false;
5757
}
5858
}
59-
}
59+
}

0 commit comments

Comments
 (0)