From f26942805dba614e6c9e2d7aa7da8fe3edaff827 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Sat, 25 Jul 2020 07:34:05 -0700 Subject: [PATCH] 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 https://github.com/dotnet/aspnetcore/issues/24042 --- .../Http.Abstractions/src/Routing/Endpoint.cs | 6 +- .../src/IRouteConstraint.cs | 2 +- src/Http/Routing/src/ArrayBuilder.cs | 1 + ...ointRoutingApplicationBuilderExtensions.cs | 2 +- .../src/CompositeEndpointDataSource.cs | 12 ++-- .../src/Constraints/BoolRouteConstraint.cs | 6 +- .../Constraints/CompositeRouteConstraint.cs | 6 +- .../Constraints/DateTimeRouteConstraint.cs | 6 +- .../src/Constraints/DecimalRouteConstraint.cs | 6 +- .../src/Constraints/DoubleRouteConstraint.cs | 6 +- .../Constraints/FileNameRouteConstraint.cs | 4 +- .../src/Constraints/FloatRouteConstraint.cs | 6 +- .../src/Constraints/GuidRouteConstraint.cs | 6 +- .../Constraints/HttpMethodRouteConstraint.cs | 4 +- .../src/Constraints/IntRouteConstraint.cs | 6 +- .../src/Constraints/LengthRouteConstraint.cs | 8 +-- .../src/Constraints/LongRouteConstraint.cs | 6 +- .../Constraints/MaxLengthRouteConstraint.cs | 8 +-- .../src/Constraints/MaxRouteConstraint.cs | 6 +- .../Constraints/MinLengthRouteConstraint.cs | 8 +-- .../src/Constraints/MinRouteConstraint.cs | 6 +- .../Constraints/NonFileNameRouteConstraint.cs | 4 +- .../src/Constraints/NullRouteConstraint.cs | 4 +- .../Constraints/OptionalRouteConstraint.cs | 6 +- .../src/Constraints/RangeRouteConstraint.cs | 6 +- .../src/Constraints/RegexRouteConstraint.cs | 6 +- .../Constraints/RequiredRouteConstraint.cs | 6 +- .../src/Constraints/StringRouteConstraint.cs | 8 +-- .../Routing/src/DataSourceDependentCache.cs | 2 + .../src/DecisionTree/DecisionCriterion.cs | 4 +- .../src/DecisionTree/DecisionTreeNode.cs | 4 +- .../src/DecisionTree/ItemDescriptor.cs | 4 +- src/Http/Routing/src/DefaultLinkGenerator.cs | 2 +- src/Http/Routing/src/DefaultLinkParser.cs | 6 +- .../src/DefaultParameterPolicyFactory.cs | 4 +- src/Http/Routing/src/EndpointMiddleware.cs | 1 + .../Routing/src/EndpointNameAddressScheme.cs | 2 +- .../Routing/src/EndpointRoutingMiddleware.cs | 3 +- .../Routing/src/InlineRouteParameterParser.cs | 2 +- ...kGeneratorEndpointNameAddressExtensions.cs | 28 ++++---- ...nkGeneratorRouteValuesAddressExtensions.cs | 38 +++++------ src/Http/Routing/src/LinkParser.cs | 2 +- ...LinkParserEndpointNameAddressExtensions.cs | 2 +- .../RouteConstraintMatcherExtensions.cs | 2 +- .../RouterMiddlewareLoggerExtensions.cs | 2 +- .../src/Logging/TreeRouterLoggerExtensions.cs | 2 +- .../src/MapRouteRouteBuilderExtensions.cs | 2 +- src/Http/Routing/src/Matching/CandidateSet.cs | 13 ++-- .../Routing/src/Matching/CandidateState.cs | 6 +- .../Matching/DataSourceDependentMatcher.cs | 4 +- .../src/Matching/DefaultEndpointSelector.cs | 8 +-- src/Http/Routing/src/Matching/DfaMatcher.cs | 13 ++-- .../Routing/src/Matching/DfaMatcherBuilder.cs | 2 + src/Http/Routing/src/Matching/DfaNode.cs | 4 +- .../Routing/src/Matching/EndpointComparer.cs | 8 +-- .../Routing/src/Matching/HostMatcherPolicy.cs | 14 ++-- .../src/Matching/HttpMethodMatcherPolicy.cs | 26 +++---- .../Routing/src/Matching/ILEmitTrieFactory.cs | 4 +- .../src/Matching/ILEmitTrieJumpTable.cs | 4 +- src/Http/Routing/src/Matching/PathSegment.cs | 4 +- .../src/Microsoft.AspNetCore.Routing.csproj | 2 +- src/Http/Routing/src/NullRouter.cs | 4 +- .../Routing/src/ParameterPolicyActivator.cs | 3 +- .../Routing/src/ParameterPolicyFactory.cs | 4 +- src/Http/Routing/src/PathTokenizer.cs | 2 + .../DefaultRoutePatternTransformer.cs | 2 + .../src/Patterns/RouteParameterParser.cs | 2 +- src/Http/Routing/src/Patterns/RoutePattern.cs | 16 ++--- .../src/Patterns/RoutePatternException.cs | 4 +- .../src/Patterns/RoutePatternFactory.cs | 68 +++++++++---------- .../src/Patterns/RoutePatternMatcher.cs | 2 + .../src/Patterns/RoutePatternParameterPart.cs | 8 +-- .../RoutePatternParameterPolicyReference.cs | 10 +-- .../src/Patterns/RoutePatternParser.cs | 2 + .../src/Patterns/RoutePatternTransformer.cs | 4 +- .../RequestDelegateRouteBuilderExtensions.cs | 2 +- src/Http/Routing/src/Route.cs | 4 +- src/Http/Routing/src/RouteBase.cs | 16 ++--- .../Routing/src/RouteConstraintMatcher.cs | 2 +- src/Http/Routing/src/RouteEndpoint.cs | 6 +- ...dpointModel.cs => RouteEndpointBuilder.cs} | 6 ++ src/Http/Routing/src/RouteOptions.cs | 2 +- .../Routing/src/RouteValueEqualityComparer.cs | 6 +- .../Routing/src/RouteValuesAddressScheme.cs | 6 +- .../Routing/src/Template/InlineConstraint.cs | 4 +- .../Routing/src/Template/RouteTemplate.cs | 2 +- .../Routing/src/Template/TemplateBinder.cs | 2 - .../Routing/src/Template/TemplateMatcher.cs | 2 +- src/Http/Routing/src/Template/TemplatePart.cs | 23 ++++--- .../src/Template/TemplateValuesResult.cs | 6 +- src/Http/Routing/src/Tree/InboundMatch.cs | 4 +- .../Routing/src/Tree/InboundRouteEntry.cs | 2 + .../src/Tree/LinkGenerationDecisionTree.cs | 2 + src/Http/Routing/src/Tree/OutboundMatch.cs | 4 +- .../Routing/src/Tree/OutboundRouteEntry.cs | 4 +- src/Http/Routing/src/Tree/TreeEnumerator.cs | 2 + src/Http/Routing/src/Tree/TreeRouteBuilder.cs | 2 + src/Http/Routing/src/Tree/TreeRouter.cs | 2 + src/Http/Routing/src/Tree/UrlMatchingNode.cs | 2 + src/Http/Routing/src/Tree/UrlMatchingTree.cs | 2 +- 100 files changed, 344 insertions(+), 287 deletions(-) rename src/Http/Routing/src/{RouteEndpointModel.cs => RouteEndpointBuilder.cs} (82%) diff --git a/src/Http/Http.Abstractions/src/Routing/Endpoint.cs b/src/Http/Http.Abstractions/src/Routing/Endpoint.cs index 4733177674..1f795f74ad 100644 --- a/src/Http/Http.Abstractions/src/Routing/Endpoint.cs +++ b/src/Http/Http.Abstractions/src/Routing/Endpoint.cs @@ -20,8 +20,8 @@ namespace Microsoft.AspNetCore.Http /// public Endpoint( RequestDelegate requestDelegate, - EndpointMetadataCollection metadata, - string displayName) + EndpointMetadataCollection? metadata, + string? displayName) { // All are allowed to be null RequestDelegate = requestDelegate; @@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Http /// /// Gets the informational display name of this endpoint. /// - public string DisplayName { get; } + public string? DisplayName { get; } /// /// Gets the collection of metadata associated with this endpoint. diff --git a/src/Http/Routing.Abstractions/src/IRouteConstraint.cs b/src/Http/Routing.Abstractions/src/IRouteConstraint.cs index 076706dcd9..37ea18c672 100644 --- a/src/Http/Routing.Abstractions/src/IRouteConstraint.cs +++ b/src/Http/Routing.Abstractions/src/IRouteConstraint.cs @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Routing /// true if the URL parameter contains a valid value; otherwise, false. bool Match( HttpContext? httpContext, - IRouter route, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection); diff --git a/src/Http/Routing/src/ArrayBuilder.cs b/src/Http/Routing/src/ArrayBuilder.cs index ff20c2e64d..ede0f990b6 100644 --- a/src/Http/Routing/src/ArrayBuilder.cs +++ b/src/Http/Routing/src/ArrayBuilder.cs @@ -6,6 +6,7 @@ // // See https://github.com/dotnet/corefx/blob/143df51926f2ad397fef9c9ca7ede88e2721e801/src/Common/src/System/Collections/Generic/ArrayBuilder.cs +#nullable disable using System; using System.Diagnostics; diff --git a/src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs b/src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs index d1d3617210..dbc3432107 100644 --- a/src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs +++ b/src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs @@ -128,7 +128,7 @@ namespace Microsoft.AspNetCore.Builder } // If someone messes with this, just let it crash. - endpointRouteBuilder = (DefaultEndpointRouteBuilder)obj; + endpointRouteBuilder = (DefaultEndpointRouteBuilder)obj!; // This check handles the case where Map or something else that forks the pipeline is called between the two // routing middleware. diff --git a/src/Http/Routing/src/CompositeEndpointDataSource.cs b/src/Http/Routing/src/CompositeEndpointDataSource.cs index 008ad09a9c..9d0a97c832 100644 --- a/src/Http/Routing/src/CompositeEndpointDataSource.cs +++ b/src/Http/Routing/src/CompositeEndpointDataSource.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading; @@ -21,8 +22,8 @@ namespace Microsoft.AspNetCore.Routing public sealed class CompositeEndpointDataSource : EndpointDataSource { private readonly object _lock; - private readonly ICollection _dataSources; - private IReadOnlyList _endpoints; + private readonly ICollection _dataSources = default!; + private IReadOnlyList _endpoints = default!; private IChangeToken _consumerChangeToken; private CancellationTokenSource _cts; @@ -49,7 +50,7 @@ namespace Microsoft.AspNetCore.Routing } } - private void OnDataSourcesChanged(object sender, NotifyCollectionChangedEventArgs e) + private void OnDataSourcesChanged(object? sender, NotifyCollectionChangedEventArgs e) { lock (_lock) { @@ -140,6 +141,7 @@ namespace Microsoft.AspNetCore.Routing } } + [MemberNotNull(nameof(_cts), nameof(_consumerChangeToken))] private void CreateChangeToken() { _cts = new CancellationTokenSource(); @@ -198,7 +200,7 @@ namespace Microsoft.AspNetCore.Routing } return sb.ToString(); - IEnumerable FormatValues(IEnumerable> values) + IEnumerable FormatValues(IEnumerable> values) { return values.Select( kvp => diff --git a/src/Http/Routing/src/Constraints/BoolRouteConstraint.cs b/src/Http/Routing/src/Constraints/BoolRouteConstraint.cs index d19000f9de..26a37c2e80 100644 --- a/src/Http/Routing/src/Constraints/BoolRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/BoolRouteConstraint.cs @@ -14,8 +14,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -44,4 +44,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/CompositeRouteConstraint.cs b/src/Http/Routing/src/Constraints/CompositeRouteConstraint.cs index 468699ce8a..023a32eae8 100644 --- a/src/Http/Routing/src/Constraints/CompositeRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/CompositeRouteConstraint.cs @@ -33,8 +33,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -60,4 +60,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return true; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/DateTimeRouteConstraint.cs b/src/Http/Routing/src/Constraints/DateTimeRouteConstraint.cs index a912a84f52..2d7fdd1bc2 100644 --- a/src/Http/Routing/src/Constraints/DateTimeRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/DateTimeRouteConstraint.cs @@ -20,8 +20,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -50,4 +50,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/DecimalRouteConstraint.cs b/src/Http/Routing/src/Constraints/DecimalRouteConstraint.cs index 9fcc80a9f1..b08d13d515 100644 --- a/src/Http/Routing/src/Constraints/DecimalRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/DecimalRouteConstraint.cs @@ -14,8 +14,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -44,4 +44,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/DoubleRouteConstraint.cs b/src/Http/Routing/src/Constraints/DoubleRouteConstraint.cs index 405e46cb0d..e3c351d3e8 100644 --- a/src/Http/Routing/src/Constraints/DoubleRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/DoubleRouteConstraint.cs @@ -14,8 +14,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -48,4 +48,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/FileNameRouteConstraint.cs b/src/Http/Routing/src/Constraints/FileNameRouteConstraint.cs index 50d85eb86f..0051ef3fb1 100644 --- a/src/Http/Routing/src/Constraints/FileNameRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/FileNameRouteConstraint.cs @@ -85,8 +85,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) diff --git a/src/Http/Routing/src/Constraints/FloatRouteConstraint.cs b/src/Http/Routing/src/Constraints/FloatRouteConstraint.cs index fbdd3c87d6..5108da1bcb 100644 --- a/src/Http/Routing/src/Constraints/FloatRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/FloatRouteConstraint.cs @@ -14,8 +14,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -48,4 +48,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/GuidRouteConstraint.cs b/src/Http/Routing/src/Constraints/GuidRouteConstraint.cs index b87069df98..101621c068 100644 --- a/src/Http/Routing/src/Constraints/GuidRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/GuidRouteConstraint.cs @@ -16,8 +16,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -46,4 +46,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/HttpMethodRouteConstraint.cs b/src/Http/Routing/src/Constraints/HttpMethodRouteConstraint.cs index ea20e81419..b2ad0eff33 100644 --- a/src/Http/Routing/src/Constraints/HttpMethodRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/HttpMethodRouteConstraint.cs @@ -35,8 +35,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints /// public virtual bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) diff --git a/src/Http/Routing/src/Constraints/IntRouteConstraint.cs b/src/Http/Routing/src/Constraints/IntRouteConstraint.cs index 34ff02331e..6190180858 100644 --- a/src/Http/Routing/src/Constraints/IntRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/IntRouteConstraint.cs @@ -14,8 +14,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -44,4 +44,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/LengthRouteConstraint.cs b/src/Http/Routing/src/Constraints/LengthRouteConstraint.cs index 8964f2549e..52fecb9d42 100644 --- a/src/Http/Routing/src/Constraints/LengthRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/LengthRouteConstraint.cs @@ -71,8 +71,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -89,7 +89,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints if (values.TryGetValue(routeKey, out var value) && value != null) { - var valueString = Convert.ToString(value, CultureInfo.InvariantCulture); + var valueString = Convert.ToString(value, CultureInfo.InvariantCulture)!; var length = valueString.Length; return length >= MinLength && length <= MaxLength; } @@ -97,4 +97,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/LongRouteConstraint.cs b/src/Http/Routing/src/Constraints/LongRouteConstraint.cs index 8ed5cccc46..493be38eb1 100644 --- a/src/Http/Routing/src/Constraints/LongRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/LongRouteConstraint.cs @@ -14,8 +14,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -44,4 +44,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/MaxLengthRouteConstraint.cs b/src/Http/Routing/src/Constraints/MaxLengthRouteConstraint.cs index 4005a041aa..470f9110d8 100644 --- a/src/Http/Routing/src/Constraints/MaxLengthRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/MaxLengthRouteConstraint.cs @@ -34,8 +34,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -52,11 +52,11 @@ namespace Microsoft.AspNetCore.Routing.Constraints if (values.TryGetValue(routeKey, out var value) && value != null) { - var valueString = Convert.ToString(value, CultureInfo.InvariantCulture); + var valueString = Convert.ToString(value, CultureInfo.InvariantCulture)!; return valueString.Length <= MaxLength; } return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/MaxRouteConstraint.cs b/src/Http/Routing/src/Constraints/MaxRouteConstraint.cs index dab22f5075..c7d5e91dfe 100644 --- a/src/Http/Routing/src/Constraints/MaxRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/MaxRouteConstraint.cs @@ -28,8 +28,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -56,4 +56,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/MinLengthRouteConstraint.cs b/src/Http/Routing/src/Constraints/MinLengthRouteConstraint.cs index c58bd0b30d..9c7b54704b 100644 --- a/src/Http/Routing/src/Constraints/MinLengthRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/MinLengthRouteConstraint.cs @@ -34,8 +34,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -52,11 +52,11 @@ namespace Microsoft.AspNetCore.Routing.Constraints if (values.TryGetValue(routeKey, out var value) && value != null) { - var valueString = Convert.ToString(value, CultureInfo.InvariantCulture); + var valueString = Convert.ToString(value, CultureInfo.InvariantCulture)!; return valueString.Length >= MinLength; } return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/MinRouteConstraint.cs b/src/Http/Routing/src/Constraints/MinRouteConstraint.cs index 450ed46fbf..ef504f446d 100644 --- a/src/Http/Routing/src/Constraints/MinRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/MinRouteConstraint.cs @@ -28,8 +28,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -56,4 +56,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/NonFileNameRouteConstraint.cs b/src/Http/Routing/src/Constraints/NonFileNameRouteConstraint.cs index c6867b6e05..9d58423b9a 100644 --- a/src/Http/Routing/src/Constraints/NonFileNameRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/NonFileNameRouteConstraint.cs @@ -81,8 +81,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) diff --git a/src/Http/Routing/src/Constraints/NullRouteConstraint.cs b/src/Http/Routing/src/Constraints/NullRouteConstraint.cs index f61e740419..7accfcb166 100644 --- a/src/Http/Routing/src/Constraints/NullRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/NullRouteConstraint.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Http; @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints { } - public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) + public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) { return true; } diff --git a/src/Http/Routing/src/Constraints/OptionalRouteConstraint.cs b/src/Http/Routing/src/Constraints/OptionalRouteConstraint.cs index 6b7cf8a1a5..f66bdd67de 100644 --- a/src/Http/Routing/src/Constraints/OptionalRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/OptionalRouteConstraint.cs @@ -24,8 +24,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints public IRouteConstraint InnerConstraint { get; } public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -52,4 +52,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return true; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/RangeRouteConstraint.cs b/src/Http/Routing/src/Constraints/RangeRouteConstraint.cs index 34c946fa66..3e7dde78cb 100644 --- a/src/Http/Routing/src/Constraints/RangeRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/RangeRouteConstraint.cs @@ -42,8 +42,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -70,4 +70,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/RegexRouteConstraint.cs b/src/Http/Routing/src/Constraints/RegexRouteConstraint.cs index 9269de51a3..4d17f5d7ea 100644 --- a/src/Http/Routing/src/Constraints/RegexRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/RegexRouteConstraint.cs @@ -38,8 +38,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints public Regex Constraint { get; private set; } public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints if (values.TryGetValue(routeKey, out var routeValue) && routeValue != null) { - var parameterValueString = Convert.ToString(routeValue, CultureInfo.InvariantCulture); + var parameterValueString = Convert.ToString(routeValue, CultureInfo.InvariantCulture)!; return Constraint.IsMatch(parameterValueString); } diff --git a/src/Http/Routing/src/Constraints/RequiredRouteConstraint.cs b/src/Http/Routing/src/Constraints/RequiredRouteConstraint.cs index e33e1e23a6..71b594a07b 100644 --- a/src/Http/Routing/src/Constraints/RequiredRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/RequiredRouteConstraint.cs @@ -18,8 +18,8 @@ namespace Microsoft.AspNetCore.Routing.Constraints { /// public bool Match( - HttpContext httpContext, - IRouter route, + HttpContext? httpContext, + IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) @@ -44,4 +44,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Constraints/StringRouteConstraint.cs b/src/Http/Routing/src/Constraints/StringRouteConstraint.cs index fb6a3568c3..737cf22812 100644 --- a/src/Http/Routing/src/Constraints/StringRouteConstraint.cs +++ b/src/Http/Routing/src/Constraints/StringRouteConstraint.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints } /// - public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) + public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) { if (routeKey == null) { @@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints if (values.TryGetValue(routeKey, out var routeValue) && routeValue != null) { - var parameterValueString = Convert.ToString(routeValue, CultureInfo.InvariantCulture); + var parameterValueString = Convert.ToString(routeValue, CultureInfo.InvariantCulture)!; return parameterValueString.Equals(_value, StringComparison.OrdinalIgnoreCase); } @@ -52,4 +52,4 @@ namespace Microsoft.AspNetCore.Routing.Constraints return false; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/DataSourceDependentCache.cs b/src/Http/Routing/src/DataSourceDependentCache.cs index f31807e171..53b8572694 100644 --- a/src/Http/Routing/src/DataSourceDependentCache.cs +++ b/src/Http/Routing/src/DataSourceDependentCache.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Threading; diff --git a/src/Http/Routing/src/DecisionTree/DecisionCriterion.cs b/src/Http/Routing/src/DecisionTree/DecisionCriterion.cs index efc9c742d2..ce8ec11498 100644 --- a/src/Http/Routing/src/DecisionTree/DecisionCriterion.cs +++ b/src/Http/Routing/src/DecisionTree/DecisionCriterion.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System.Collections.Generic; namespace Microsoft.AspNetCore.Routing.DecisionTree @@ -11,4 +13,4 @@ namespace Microsoft.AspNetCore.Routing.DecisionTree public Dictionary> Branches { get; set; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/DecisionTree/DecisionTreeNode.cs b/src/Http/Routing/src/DecisionTree/DecisionTreeNode.cs index 1be3064c62..df28097786 100644 --- a/src/Http/Routing/src/DecisionTree/DecisionTreeNode.cs +++ b/src/Http/Routing/src/DecisionTree/DecisionTreeNode.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System.Collections.Generic; namespace Microsoft.AspNetCore.Routing.DecisionTree @@ -17,4 +19,4 @@ namespace Microsoft.AspNetCore.Routing.DecisionTree // matching the input data. public IList> Criteria { get; set; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/DecisionTree/ItemDescriptor.cs b/src/Http/Routing/src/DecisionTree/ItemDescriptor.cs index 84a6279c27..e5146f98c4 100644 --- a/src/Http/Routing/src/DecisionTree/ItemDescriptor.cs +++ b/src/Http/Routing/src/DecisionTree/ItemDescriptor.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System.Collections.Generic; namespace Microsoft.AspNetCore.Routing.DecisionTree @@ -13,4 +15,4 @@ namespace Microsoft.AspNetCore.Routing.DecisionTree public TItem Item { get; set; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/DefaultLinkGenerator.cs b/src/Http/Routing/src/DefaultLinkGenerator.cs index 2dda332858..e080aaec50 100644 --- a/src/Http/Routing/src/DefaultLinkGenerator.cs +++ b/src/Http/Routing/src/DefaultLinkGenerator.cs @@ -325,7 +325,7 @@ namespace Microsoft.AspNetCore.Routing } // Also called from DefaultLinkGenerationTemplate - public static RouteValueDictionary? GetAmbientValues(HttpContext httpContext) + public static RouteValueDictionary? GetAmbientValues(HttpContext? httpContext) { return httpContext?.Features.Get()?.RouteValues; } diff --git a/src/Http/Routing/src/DefaultLinkParser.cs b/src/Http/Routing/src/DefaultLinkParser.cs index 8c922e2668..07aaacbb03 100644 --- a/src/Http/Routing/src/DefaultLinkParser.cs +++ b/src/Http/Routing/src/DefaultLinkParser.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; @@ -46,7 +47,7 @@ namespace Microsoft.AspNetCore.Routing _createMatcher = CreateRoutePatternMatcher; } - public override RouteValueDictionary ParsePathByAddress(TAddress address, PathString path) + public override RouteValueDictionary? ParsePathByAddress(TAddress address, PathString path) { var endpoints = GetEndpoints(address); if (endpoints.Count == 0) @@ -117,7 +118,7 @@ namespace Microsoft.AspNetCore.Routing internal MatcherState GetMatcherState(RouteEndpoint endpoint) => _matcherCache.EnsureInitialized().GetOrAdd(endpoint, _createMatcher); // Internal for testing - internal bool TryParse(RouteEndpoint endpoint, PathString path, out RouteValueDictionary values) + internal bool TryParse(RouteEndpoint endpoint, PathString path, [NotNullWhen(true)] out RouteValueDictionary? values) { var (matcher, constraints) = GetMatcherState(endpoint); @@ -168,6 +169,7 @@ namespace Microsoft.AspNetCore.Routing } } +#nullable disable private static class Log { public static class EventIds diff --git a/src/Http/Routing/src/DefaultParameterPolicyFactory.cs b/src/Http/Routing/src/DefaultParameterPolicyFactory.cs index 1982d75518..059c4bb31c 100644 --- a/src/Http/Routing/src/DefaultParameterPolicyFactory.cs +++ b/src/Http/Routing/src/DefaultParameterPolicyFactory.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Routing _serviceProvider = serviceProvider; } - public override IParameterPolicy Create(RoutePatternParameterPart parameter, IParameterPolicy parameterPolicy) + public override IParameterPolicy Create(RoutePatternParameterPart? parameter, IParameterPolicy parameterPolicy) { if (parameterPolicy == null) { @@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Routing return parameterPolicy; } - public override IParameterPolicy Create(RoutePatternParameterPart parameter, string inlineText) + public override IParameterPolicy Create(RoutePatternParameterPart? parameter, string inlineText) { if (inlineText == null) { diff --git a/src/Http/Routing/src/EndpointMiddleware.cs b/src/Http/Routing/src/EndpointMiddleware.cs index 0e11e560b7..f5c2b0912f 100644 --- a/src/Http/Routing/src/EndpointMiddleware.cs +++ b/src/Http/Routing/src/EndpointMiddleware.cs @@ -101,6 +101,7 @@ namespace Microsoft.AspNetCore.Routing "Configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code. The call to app.UseCors() must appear between app.UseRouting() and app.UseEndpoints(...)."); } +#nullable disable private static class Log { private static readonly Action _executingEndpoint = LoggerMessage.Define( diff --git a/src/Http/Routing/src/EndpointNameAddressScheme.cs b/src/Http/Routing/src/EndpointNameAddressScheme.cs index 9d2753cf52..24799d2d39 100644 --- a/src/Http/Routing/src/EndpointNameAddressScheme.cs +++ b/src/Http/Routing/src/EndpointNameAddressScheme.cs @@ -92,7 +92,7 @@ namespace Microsoft.AspNetCore.Routing throw new InvalidOperationException(builder.ToString()); - string GetEndpointName(Endpoint endpoint) + string? GetEndpointName(Endpoint endpoint) { if (endpoint.Metadata.GetMetadata()?.SuppressLinkGeneration == true) { diff --git a/src/Http/Routing/src/EndpointRoutingMiddleware.cs b/src/Http/Routing/src/EndpointRoutingMiddleware.cs index 482e86bb6d..82c841941c 100644 --- a/src/Http/Routing/src/EndpointRoutingMiddleware.cs +++ b/src/Http/Routing/src/EndpointRoutingMiddleware.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Routing private readonly DiagnosticListener _diagnosticListener; private readonly RequestDelegate _next; - private Task _initializationTask; + private Task? _initializationTask; public EndpointRoutingMiddleware( MatcherFactory matcherFactory, @@ -165,6 +165,7 @@ namespace Microsoft.AspNetCore.Routing } } +#nullable disable private static class Log { private static readonly Action _matchSuccess = LoggerMessage.Define( diff --git a/src/Http/Routing/src/InlineRouteParameterParser.cs b/src/Http/Routing/src/InlineRouteParameterParser.cs index 33c70558f2..49dd434d37 100644 --- a/src/Http/Routing/src/InlineRouteParameterParser.cs +++ b/src/Http/Routing/src/InlineRouteParameterParser.cs @@ -75,7 +75,7 @@ namespace Microsoft.AspNetCore.Routing var parseResults = ParseConstraints(routeParameter, currentIndex, endIndex); currentIndex = parseResults.CurrentIndex; - string defaultValue = null; + string? defaultValue = null; if (currentIndex <= endIndex && routeParameter[currentIndex] == '=') { diff --git a/src/Http/Routing/src/LinkGeneratorEndpointNameAddressExtensions.cs b/src/Http/Routing/src/LinkGeneratorEndpointNameAddressExtensions.cs index dd0e462c20..bc6ca2fc24 100644 --- a/src/Http/Routing/src/LinkGeneratorEndpointNameAddressExtensions.cs +++ b/src/Http/Routing/src/LinkGeneratorEndpointNameAddressExtensions.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Http; @@ -27,14 +27,14 @@ namespace Microsoft.AspNetCore.Routing /// names from RouteOptions. /// /// A URI with an absolute path, or null. - public static string GetPathByName( + public static string? GetPathByName( this LinkGenerator generator, HttpContext httpContext, string endpointName, - object values, + object? values, PathString? pathBase = default, FragmentString fragment = default, - LinkOptions options = default) + LinkOptions? options = default) { if (generator == null) { @@ -74,13 +74,13 @@ namespace Microsoft.AspNetCore.Routing /// names from RouteOptions. /// /// A URI with an absolute path, or null. - public static string GetPathByName( + public static string? GetPathByName( this LinkGenerator generator, string endpointName, - object values, + object? values, PathString pathBase = default, FragmentString fragment = default, - LinkOptions options = default) + LinkOptions? options = default) { if (generator == null) { @@ -126,16 +126,16 @@ namespace Microsoft.AspNetCore.Routing /// your deployment environment. /// /// - public static string GetUriByName( + public static string? GetUriByName( this LinkGenerator generator, HttpContext httpContext, string endpointName, - object values, - string scheme = default, + object? values, + string? scheme = default, HostString? host = default, PathString? pathBase = default, FragmentString fragment = default, - LinkOptions options = default) + LinkOptions? options = default) { if (generator == null) { @@ -190,15 +190,15 @@ namespace Microsoft.AspNetCore.Routing /// your deployment environment. /// /// - public static string GetUriByName( + public static string? GetUriByName( this LinkGenerator generator, string endpointName, - object values, + object? values, string scheme, HostString host, PathString pathBase = default, FragmentString fragment = default, - LinkOptions options = default) + LinkOptions? options = default) { if (generator == null) { diff --git a/src/Http/Routing/src/LinkGeneratorRouteValuesAddressExtensions.cs b/src/Http/Routing/src/LinkGeneratorRouteValuesAddressExtensions.cs index f683b4d3ca..ddf4bb9e91 100644 --- a/src/Http/Routing/src/LinkGeneratorRouteValuesAddressExtensions.cs +++ b/src/Http/Routing/src/LinkGeneratorRouteValuesAddressExtensions.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Http; @@ -27,14 +27,14 @@ namespace Microsoft.AspNetCore.Routing /// names from RouteOptions. /// /// A URI with an absolute path, or null. - public static string GetPathByRouteValues( + public static string? GetPathByRouteValues( this LinkGenerator generator, HttpContext httpContext, - string routeName, - object values, + string? routeName, + object? values, PathString? pathBase = default, FragmentString fragment = default, - LinkOptions options = default) + LinkOptions? options = default) { if (generator == null) { @@ -70,13 +70,13 @@ namespace Microsoft.AspNetCore.Routing /// names from RouteOptions. /// /// A URI with an absolute path, or null. - public static string GetPathByRouteValues( + public static string? GetPathByRouteValues( this LinkGenerator generator, - string routeName, - object values, + string? routeName, + object? values, PathString pathBase = default, FragmentString fragment = default, - LinkOptions options = default) + LinkOptions? options = default) { if (generator == null) { @@ -118,16 +118,16 @@ namespace Microsoft.AspNetCore.Routing /// your deployment environment. /// /// - public static string GetUriByRouteValues( + public static string? GetUriByRouteValues( this LinkGenerator generator, HttpContext httpContext, - string routeName, - object values, - string scheme = default, + string? routeName, + object? values, + string? scheme = default, HostString? host = default, PathString? pathBase = default, FragmentString fragment = default, - LinkOptions options = default) + LinkOptions? options = default) { if (generator == null) { @@ -178,15 +178,15 @@ namespace Microsoft.AspNetCore.Routing /// your deployment environment. /// /// - public static string GetUriByRouteValues( + public static string? GetUriByRouteValues( this LinkGenerator generator, - string routeName, - object values, + string? routeName, + object? values, string scheme, HostString host, PathString pathBase = default, FragmentString fragment = default, - LinkOptions options = default) + LinkOptions? options = default) { if (generator == null) { @@ -197,7 +197,7 @@ namespace Microsoft.AspNetCore.Routing return generator.GetUriByAddress(address, address.ExplicitValues, scheme, host, pathBase, fragment, options); } - private static RouteValuesAddress CreateAddress(HttpContext httpContext, string routeName, object values) + private static RouteValuesAddress CreateAddress(HttpContext? httpContext, string? routeName, object? values) { return new RouteValuesAddress() { diff --git a/src/Http/Routing/src/LinkParser.cs b/src/Http/Routing/src/LinkParser.cs index b5135b3f01..88ed8c8131 100644 --- a/src/Http/Routing/src/LinkParser.cs +++ b/src/Http/Routing/src/LinkParser.cs @@ -32,6 +32,6 @@ namespace Microsoft.AspNetCore.Routing /// of the route patterns match the provided URI path. /// /// - public abstract RouteValueDictionary ParsePathByAddress(TAddress address, PathString path); + public abstract RouteValueDictionary? ParsePathByAddress(TAddress address, PathString path); } } diff --git a/src/Http/Routing/src/LinkParserEndpointNameAddressExtensions.cs b/src/Http/Routing/src/LinkParserEndpointNameAddressExtensions.cs index 904dc0b885..2217a8eb45 100644 --- a/src/Http/Routing/src/LinkParserEndpointNameAddressExtensions.cs +++ b/src/Http/Routing/src/LinkParserEndpointNameAddressExtensions.cs @@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.Routing /// of the route patterns match the provided URI path. /// /// - public static RouteValueDictionary ParsePathByEndpointName( + public static RouteValueDictionary? ParsePathByEndpointName( this LinkParser parser, string endpointName, PathString path) diff --git a/src/Http/Routing/src/Logging/RouteConstraintMatcherExtensions.cs b/src/Http/Routing/src/Logging/RouteConstraintMatcherExtensions.cs index afa7337e75..9415831bdc 100644 --- a/src/Http/Routing/src/Logging/RouteConstraintMatcherExtensions.cs +++ b/src/Http/Routing/src/Logging/RouteConstraintMatcherExtensions.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Routing.Logging { internal static class RouteConstraintMatcherExtensions { - private static readonly Action _constraintNotMatched; + private static readonly Action _constraintNotMatched; static RouteConstraintMatcherExtensions() { diff --git a/src/Http/Routing/src/Logging/RouterMiddlewareLoggerExtensions.cs b/src/Http/Routing/src/Logging/RouterMiddlewareLoggerExtensions.cs index cdbd644775..76f569c150 100644 --- a/src/Http/Routing/src/Logging/RouterMiddlewareLoggerExtensions.cs +++ b/src/Http/Routing/src/Logging/RouterMiddlewareLoggerExtensions.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Routing.Logging { internal static class RouterMiddlewareLoggerExtensions { - private static readonly Action _requestNotMatched; + private static readonly Action _requestNotMatched; static RouterMiddlewareLoggerExtensions() { diff --git a/src/Http/Routing/src/Logging/TreeRouterLoggerExtensions.cs b/src/Http/Routing/src/Logging/TreeRouterLoggerExtensions.cs index e6733263f9..bcb0f343a7 100644 --- a/src/Http/Routing/src/Logging/TreeRouterLoggerExtensions.cs +++ b/src/Http/Routing/src/Logging/TreeRouterLoggerExtensions.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Routing.Logging { internal static class TreeRouterLoggerExtensions { - private static readonly Action _requestMatchedRoute; + private static readonly Action _requestMatchedRoute; static TreeRouterLoggerExtensions() { diff --git a/src/Http/Routing/src/MapRouteRouteBuilderExtensions.cs b/src/Http/Routing/src/MapRouteRouteBuilderExtensions.cs index 23f4536b9b..56f91c4050 100644 --- a/src/Http/Routing/src/MapRouteRouteBuilderExtensions.cs +++ b/src/Http/Routing/src/MapRouteRouteBuilderExtensions.cs @@ -154,7 +154,7 @@ namespace Microsoft.AspNetCore.Builder return routeConstraint; } - var parameterPolicy = _parameterPolicyFactory.Create(null, inlineConstraint); + var parameterPolicy = _parameterPolicyFactory.Create(null!, inlineConstraint); if (parameterPolicy != null) { // Logic inside Route will skip adding NullRouteConstraint diff --git a/src/Http/Routing/src/Matching/CandidateSet.cs b/src/Http/Routing/src/Matching/CandidateSet.cs index f90183897d..0d07dc5940 100644 --- a/src/Http/Routing/src/Matching/CandidateSet.cs +++ b/src/Http/Routing/src/Matching/CandidateSet.cs @@ -7,6 +7,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Runtime.CompilerServices; using Microsoft.AspNetCore.Http; @@ -176,15 +177,17 @@ namespace Microsoft.AspNetCore.Routing.Matching /// The to replace the original at /// the . /// - public void ReplaceEndpoint(int index, Endpoint endpoint, RouteValueDictionary values) + public void ReplaceEndpoint(int index, Endpoint? endpoint, RouteValueDictionary? values) { // Friendliness for inlining if ((uint)index >= Count) { ThrowIndexArgumentOutOfRangeException(); } - - Candidates[index] = new CandidateState(endpoint, values, Candidates[index].Score); + + // CandidateState allows a null-valued endpoint. However a validate candidate should never have a null endpoint + // We'll make lives easier for matcher policies by declaring it as non-null. + Candidates[index] = new CandidateState(endpoint!, values, Candidates[index].Score); if (endpoint == null) { @@ -354,7 +357,7 @@ namespace Microsoft.AspNetCore.Routing.Matching { if (GetOriginalScore(i) == score) { - duplicates.Add(candidates[i].Endpoint); + duplicates.Add(candidates[i].Endpoint!); } } @@ -366,11 +369,13 @@ namespace Microsoft.AspNetCore.Routing.Matching } } + [DoesNotReturn] private static void ThrowIndexArgumentOutOfRangeException() { throw new ArgumentOutOfRangeException("index"); } + [DoesNotReturn] private static void ThrowArgumentNullException(string parameter) { throw new ArgumentNullException(parameter); diff --git a/src/Http/Routing/src/Matching/CandidateState.cs b/src/Http/Routing/src/Matching/CandidateState.cs index 5b198beb8e..14b8591c68 100644 --- a/src/Http/Routing/src/Matching/CandidateState.cs +++ b/src/Http/Routing/src/Matching/CandidateState.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Http; @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Values = null; } - internal CandidateState(Endpoint endpoint, RouteValueDictionary values, int score) + internal CandidateState(Endpoint endpoint, RouteValueDictionary? values, int score) { Endpoint = endpoint; Values = values; @@ -50,6 +50,6 @@ namespace Microsoft.AspNetCore.Routing.Matching /// Gets associated with the /// and the current request. /// - public RouteValueDictionary Values { get; internal set; } + public RouteValueDictionary? Values { get; internal set; } } } diff --git a/src/Http/Routing/src/Matching/DataSourceDependentMatcher.cs b/src/Http/Routing/src/Matching/DataSourceDependentMatcher.cs index c757b1646e..bf6c02102e 100644 --- a/src/Http/Routing/src/Matching/DataSourceDependentMatcher.cs +++ b/src/Http/Routing/src/Matching/DataSourceDependentMatcher.cs @@ -57,10 +57,10 @@ namespace Microsoft.AspNetCore.Routing.Matching public sealed class Lifetime : IDisposable { private readonly object _lock = new object(); - private DataSourceDependentCache _cache; + private DataSourceDependentCache? _cache; private bool _disposed; - public DataSourceDependentCache Cache + public DataSourceDependentCache? Cache { get => _cache; set diff --git a/src/Http/Routing/src/Matching/DefaultEndpointSelector.cs b/src/Http/Routing/src/Matching/DefaultEndpointSelector.cs index cc455d4eb7..31c863a5a9 100644 --- a/src/Http/Routing/src/Matching/DefaultEndpointSelector.cs +++ b/src/Http/Routing/src/Matching/DefaultEndpointSelector.cs @@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.Routing.Matching if (CandidateSet.IsValidCandidate(ref state)) { httpContext.SetEndpoint(state.Endpoint); - httpContext.Request.RouteValues = state.Values; + httpContext.Request.RouteValues = state.Values!; } break; @@ -67,8 +67,8 @@ namespace Microsoft.AspNetCore.Routing.Matching HttpContext httpContext, CandidateState[] candidateState) { - Endpoint endpoint = null; - RouteValueDictionary values = null; + Endpoint? endpoint = null; + RouteValueDictionary? values = null; int? foundScore = null; for (var i = 0; i < candidateState.Length; i++) { @@ -110,7 +110,7 @@ namespace Microsoft.AspNetCore.Routing.Matching if (endpoint != null) { httpContext.SetEndpoint(endpoint); - httpContext.Request.RouteValues = values; + httpContext.Request.RouteValues = values!; } } diff --git a/src/Http/Routing/src/Matching/DfaMatcher.cs b/src/Http/Routing/src/Matching/DfaMatcher.cs index d79f13e2ab..e40e21e252 100644 --- a/src/Http/Routing/src/Matching/DfaMatcher.cs +++ b/src/Http/Routing/src/Matching/DfaMatcher.cs @@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Routing.Matching // The sequence of actions we take is optimized to avoid doing expensive work // like creating substrings, creating route value dictionaries, and calling // into policies like versioning. - var path = httpContext.Request.Path.Value; + var path = httpContext.Request.Path.Value!; // First tokenize the path into series of segments. Span buffer = stackalloc PathSegment[_maxSegmentCount]; @@ -119,7 +119,7 @@ namespace Microsoft.AspNetCore.Routing.Matching // We want to create a new array for the route values based on Slots // as a prototype. var prototype = candidate.Slots; - var slots = new KeyValuePair[prototype.Length]; + var slots = new KeyValuePair[prototype.Length]; if ((flags & Candidate.CandidateFlags.HasDefaults) != 0) { @@ -221,7 +221,7 @@ namespace Microsoft.AspNetCore.Routing.Matching } private void ProcessCaptures( - KeyValuePair[] slots, + KeyValuePair[] slots, (string parameterName, int segmentIndex, int slotIndex)[] captures, string path, ReadOnlySpan segments) @@ -235,7 +235,7 @@ namespace Microsoft.AspNetCore.Routing.Matching var segment = segments[segmentIndex]; if (parameterName != null && segment.Length > 0) { - slots[slotIndex] = new KeyValuePair( + slots[slotIndex] = new KeyValuePair( parameterName, path.Substring(segment.Start, segment.Length)); } @@ -244,7 +244,7 @@ namespace Microsoft.AspNetCore.Routing.Matching } private void ProcessCatchAll( - KeyValuePair[] slots, + KeyValuePair[] slots, in (string parameterName, int segmentIndex, int slotIndex) catchAll, string path, ReadOnlySpan segments) @@ -255,7 +255,7 @@ namespace Microsoft.AspNetCore.Routing.Matching if ((uint)segmentIndex < (uint)segments.Length) { var segment = segments[segmentIndex]; - slots[catchAll.slotIndex] = new KeyValuePair( + slots[catchAll.slotIndex] = new KeyValuePair( catchAll.parameterName, path.Substring(segment.Start)); } @@ -333,6 +333,7 @@ namespace Microsoft.AspNetCore.Routing.Matching public static readonly EventId CandidateValid = new EventId(1005, "CandiateValid"); } +#nullable disable private static class Logger { private static readonly Action _candidatesNotFound = LoggerMessage.Define( diff --git a/src/Http/Routing/src/Matching/DfaMatcherBuilder.cs b/src/Http/Routing/src/Matching/DfaMatcherBuilder.cs index 2782f8a3ae..c10c34d12b 100644 --- a/src/Http/Routing/src/Matching/DfaMatcherBuilder.cs +++ b/src/Http/Routing/src/Matching/DfaMatcherBuilder.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Linq; diff --git a/src/Http/Routing/src/Matching/DfaNode.cs b/src/Http/Routing/src/Matching/DfaNode.cs index 602a48a0e2..b0ba4b9887 100644 --- a/src/Http/Routing/src/Matching/DfaNode.cs +++ b/src/Http/Routing/src/Matching/DfaNode.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Diagnostics; diff --git a/src/Http/Routing/src/Matching/EndpointComparer.cs b/src/Http/Routing/src/Matching/EndpointComparer.cs index 7df5de8ea4..b76802f2e6 100644 --- a/src/Http/Routing/src/Matching/EndpointComparer.cs +++ b/src/Http/Routing/src/Matching/EndpointComparer.cs @@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Routing.Matching } } - public int Compare(Endpoint x, Endpoint y) + public int Compare(Endpoint? x, Endpoint? y) { // We don't expose this publicly, and we should never call it on // a null endpoint. @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Routing.Matching return 0; } - public bool Equals(Endpoint x, Endpoint y) + public bool Equals(Endpoint? x, Endpoint? y) { // We don't expose this publicly, and we should never call it on // a null endpoint. @@ -109,7 +109,7 @@ namespace Microsoft.AspNetCore.Routing.Matching { public static readonly IComparer Instance = new OrderComparer(); - public int Compare(Endpoint x, Endpoint y) + public int Compare(Endpoint? x, Endpoint? y) { var routeEndpointX = x as RouteEndpoint; var routeEndpointY = y as RouteEndpoint; @@ -136,7 +136,7 @@ namespace Microsoft.AspNetCore.Routing.Matching { public static readonly IComparer Instance = new PrecedenceComparer(); - public int Compare(Endpoint x, Endpoint y) + public int Compare(Endpoint? x, Endpoint? y) { var routeEndpointX = x as RouteEndpoint; var routeEndpointY = y as RouteEndpoint; diff --git a/src/Http/Routing/src/Matching/HostMatcherPolicy.cs b/src/Http/Routing/src/Matching/HostMatcherPolicy.cs index be6f5bbf84..d0f29612e4 100644 --- a/src/Http/Routing/src/Matching/HostMatcherPolicy.cs +++ b/src/Http/Routing/src/Matching/HostMatcherPolicy.cs @@ -342,7 +342,7 @@ namespace Microsoft.AspNetCore.Routing.Matching private class HostMetadataEndpointComparer : EndpointMetadataComparer { - protected override int CompareMetadata(IHostMetadata x, IHostMetadata y) + protected override int CompareMetadata(IHostMetadata? x, IHostMetadata? y) { // Ignore the metadata if it has an empty list of hosts. return base.CompareMetadata( @@ -391,9 +391,9 @@ namespace Microsoft.AspNetCore.Routing.Matching public readonly int? Port; public readonly string Host; - private readonly string _wildcardEndsWith; + private readonly string? _wildcardEndsWith; - public EdgeKey(string host, int? port) + public EdgeKey(string? host, int? port) { Host = host ?? WildcardHost; Port = port; @@ -421,9 +421,9 @@ namespace Microsoft.AspNetCore.Routing.Matching return Comparer.Default.Compare(Port, other.Port); } - public int CompareTo(object obj) + public int CompareTo(object? obj) { - return CompareTo((EdgeKey)obj); + return CompareTo((EdgeKey)obj!); } public bool Equals(EdgeKey other) @@ -437,7 +437,7 @@ namespace Microsoft.AspNetCore.Routing.Matching { if (HasHostWildcard) { - return host.EndsWith(_wildcardEndsWith, StringComparison.OrdinalIgnoreCase); + return host.EndsWith(_wildcardEndsWith!, StringComparison.OrdinalIgnoreCase); } else { @@ -454,7 +454,7 @@ namespace Microsoft.AspNetCore.Routing.Matching return (Host?.GetHashCode() ?? 0) ^ (Port?.GetHashCode() ?? 0); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (obj is EdgeKey key) { diff --git a/src/Http/Routing/src/Matching/HttpMethodMatcherPolicy.cs b/src/Http/Routing/src/Matching/HttpMethodMatcherPolicy.cs index 2dfdefcf02..039d889fed 100644 --- a/src/Http/Routing/src/Matching/HttpMethodMatcherPolicy.cs +++ b/src/Http/Routing/src/Matching/HttpMethodMatcherPolicy.cs @@ -108,7 +108,7 @@ namespace Microsoft.AspNetCore.Routing.Matching // We want to return a 405 iff we eliminated ALL of the currently valid endpoints due to HTTP method // mismatch. bool? needs405Endpoint = null; - HashSet methods = null; + HashSet? methods = null; for (var i = 0; i < candidates.Count; i++) { @@ -168,8 +168,8 @@ namespace Microsoft.AspNetCore.Routing.Matching if (needs405Endpoint == true) { // We saw some endpoints coming in, and we eliminated them all. - httpContext.SetEndpoint(CreateRejectionEndpoint(methods.OrderBy(m => m, StringComparer.OrdinalIgnoreCase))); - httpContext.Request.RouteValues = null; + httpContext.SetEndpoint(CreateRejectionEndpoint(methods!.OrderBy(m => m, StringComparer.OrdinalIgnoreCase))); + httpContext.Request.RouteValues = null!; } return Task.CompletedTask; @@ -329,8 +329,8 @@ namespace Microsoft.AspNetCore.Routing.Matching /// public PolicyJumpTable BuildJumpTable(int exitDestination, IReadOnlyList edges) { - Dictionary destinations = null; - Dictionary corsPreflightDestinations = null; + Dictionary? destinations = null; + Dictionary? corsPreflightDestinations = null; for (var i = 0; i < edges.Count; i++) { // We create this data, so it's safe to cast it. @@ -421,17 +421,17 @@ namespace Microsoft.AspNetCore.Routing.Matching private class HttpMethodPolicyJumpTable : PolicyJumpTable { private readonly int _exitDestination; - private readonly Dictionary _destinations; + private readonly Dictionary? _destinations; private readonly int _corsPreflightExitDestination; - private readonly Dictionary _corsPreflightDestinations; + private readonly Dictionary? _corsPreflightDestinations; private readonly bool _supportsCorsPreflight; public HttpMethodPolicyJumpTable( int exitDestination, - Dictionary destinations, + Dictionary? destinations, int corsPreflightExitDestination, - Dictionary corsPreflightDestinations) + Dictionary? corsPreflightDestinations) { _exitDestination = exitDestination; _destinations = destinations; @@ -466,7 +466,7 @@ namespace Microsoft.AspNetCore.Routing.Matching private class HttpMethodMetadataEndpointComparer : EndpointMetadataComparer { - protected override int CompareMetadata(IHttpMethodMetadata x, IHttpMethodMetadata y) + protected override int CompareMetadata(IHttpMethodMetadata? x, IHttpMethodMetadata? y) { // Ignore the metadata if it has an empty list of HTTP methods. return base.CompareMetadata( @@ -501,9 +501,9 @@ namespace Microsoft.AspNetCore.Routing.Matching return IsCorsPreflightRequest.CompareTo(other.IsCorsPreflightRequest); } - public int CompareTo(object obj) + public int CompareTo(object? obj) { - return CompareTo((EdgeKey)obj); + return CompareTo((EdgeKey)obj!); } public bool Equals(EdgeKey other) @@ -513,7 +513,7 @@ namespace Microsoft.AspNetCore.Routing.Matching HttpMethods.Equals(HttpMethod, other.HttpMethod); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { var other = obj as EdgeKey?; return other == null ? false : Equals(other.Value); diff --git a/src/Http/Routing/src/Matching/ILEmitTrieFactory.cs b/src/Http/Routing/src/Matching/ILEmitTrieFactory.cs index 3cb240c2fa..18a60c4e0d 100644 --- a/src/Http/Routing/src/Matching/ILEmitTrieFactory.cs +++ b/src/Http/Routing/src/Matching/ILEmitTrieFactory.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Diagnostics; using System.Linq; diff --git a/src/Http/Routing/src/Matching/ILEmitTrieJumpTable.cs b/src/Http/Routing/src/Matching/ILEmitTrieJumpTable.cs index e59f283435..18bdd2bc35 100644 --- a/src/Http/Routing/src/Matching/ILEmitTrieJumpTable.cs +++ b/src/Http/Routing/src/Matching/ILEmitTrieJumpTable.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Threading; using System.Threading.Tasks; diff --git a/src/Http/Routing/src/Matching/PathSegment.cs b/src/Http/Routing/src/Matching/PathSegment.cs index a5f570f8b5..fd8d8a9223 100644 --- a/src/Http/Routing/src/Matching/PathSegment.cs +++ b/src/Http/Routing/src/Matching/PathSegment.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Length = length; } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj is PathSegment segment ? Equals(segment) : false; } diff --git a/src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj b/src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj index 1937aae7f6..9ff3925225 100644 --- a/src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj +++ b/src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj @@ -12,7 +12,7 @@ Microsoft.AspNetCore.Routing.RouteCollection aspnetcore;routing true false - annotations + enable diff --git a/src/Http/Routing/src/NullRouter.cs b/src/Http/Routing/src/NullRouter.cs index 9fd1908942..2823067b5e 100644 --- a/src/Http/Routing/src/NullRouter.cs +++ b/src/Http/Routing/src/NullRouter.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Threading.Tasks; @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Routing { } - public VirtualPathData GetVirtualPath(VirtualPathContext context) + public VirtualPathData? GetVirtualPath(VirtualPathContext context) { return null; } diff --git a/src/Http/Routing/src/ParameterPolicyActivator.cs b/src/Http/Routing/src/ParameterPolicyActivator.cs index b83c5c186b..81da4d49e9 100644 --- a/src/Http/Routing/src/ParameterPolicyActivator.cs +++ b/src/Http/Routing/src/ParameterPolicyActivator.cs @@ -1,12 +1,13 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; -using System.Text; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Routing diff --git a/src/Http/Routing/src/ParameterPolicyFactory.cs b/src/Http/Routing/src/ParameterPolicyFactory.cs index 2d8e665b47..95212e1263 100644 --- a/src/Http/Routing/src/ParameterPolicyFactory.cs +++ b/src/Http/Routing/src/ParameterPolicyFactory.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Routing /// The parameter the parameter policy is being created for. /// An existing parameter policy. /// The for the parameter. - public abstract IParameterPolicy Create(RoutePatternParameterPart parameter, IParameterPolicy parameterPolicy); + public abstract IParameterPolicy Create(RoutePatternParameterPart? parameter, IParameterPolicy parameterPolicy); /// /// Creates a parameter policy. @@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.Routing /// The parameter the parameter policy is being created for. /// The reference to resolve. /// The for the parameter. - public IParameterPolicy Create(RoutePatternParameterPart parameter, RoutePatternParameterPolicyReference reference) + public IParameterPolicy Create(RoutePatternParameterPart? parameter, RoutePatternParameterPolicyReference reference) { if (reference == null) { diff --git a/src/Http/Routing/src/PathTokenizer.cs b/src/Http/Routing/src/PathTokenizer.cs index 307dfb1159..79f971e497 100644 --- a/src/Http/Routing/src/PathTokenizer.cs +++ b/src/Http/Routing/src/PathTokenizer.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections; using System.Collections.Generic; diff --git a/src/Http/Routing/src/Patterns/DefaultRoutePatternTransformer.cs b/src/Http/Routing/src/Patterns/DefaultRoutePatternTransformer.cs index 39adf3a4d0..c79946c728 100644 --- a/src/Http/Routing/src/Patterns/DefaultRoutePatternTransformer.cs +++ b/src/Http/Routing/src/Patterns/DefaultRoutePatternTransformer.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; diff --git a/src/Http/Routing/src/Patterns/RouteParameterParser.cs b/src/Http/Routing/src/Patterns/RouteParameterParser.cs index 07c8d91bb9..072ab0d8fc 100644 --- a/src/Http/Routing/src/Patterns/RouteParameterParser.cs +++ b/src/Http/Routing/src/Patterns/RouteParameterParser.cs @@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns var parseResults = ParseConstraints(parameter, currentIndex, endIndex); currentIndex = parseResults.CurrentIndex; - string defaultValue = null; + string? defaultValue = null; if (currentIndex <= endIndex && parameter[currentIndex] == '=') { diff --git a/src/Http/Routing/src/Patterns/RoutePattern.cs b/src/Http/Routing/src/Patterns/RoutePattern.cs index 4cd881c8e2..055a062669 100644 --- a/src/Http/Routing/src/Patterns/RoutePattern.cs +++ b/src/Http/Routing/src/Patterns/RoutePattern.cs @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// public static readonly object RequiredValueAny = new RequiredValueAnySentinal(); - internal static bool IsRequiredValueAny(object value) + internal static bool IsRequiredValueAny(object? value) { return object.ReferenceEquals(RequiredValueAny, value); } @@ -35,10 +35,10 @@ namespace Microsoft.AspNetCore.Routing.Patterns private const string SeparatorString = "/"; internal RoutePattern( - string rawText, - IReadOnlyDictionary defaults, + string? rawText, + IReadOnlyDictionary defaults, IReadOnlyDictionary> parameterPolicies, - IReadOnlyDictionary requiredValues, + IReadOnlyDictionary requiredValues, IReadOnlyList parameters, IReadOnlyList pathSegments) { @@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// Gets the set of default values for the route pattern. /// The keys of are the route parameter names. /// - public IReadOnlyDictionary Defaults { get; } + public IReadOnlyDictionary Defaults { get; } /// /// Gets the set of parameter policy references for the route pattern. @@ -92,7 +92,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// /// /// - public IReadOnlyDictionary RequiredValues { get; } + public IReadOnlyDictionary RequiredValues { get; } /// /// Gets the precedence value of the route pattern for URL matching. @@ -115,7 +115,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// /// Gets the raw text supplied when parsing the route pattern. May be null. /// - public string RawText { get; } + public string? RawText { get; } /// /// Gets the list of route parameters. @@ -132,7 +132,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// /// The name of the parameter to match. /// The matching parameter or null if no parameter matches the given name. - public RoutePatternParameterPart GetParameter(string name) + public RoutePatternParameterPart? GetParameter(string name) { if (name == null) { diff --git a/src/Http/Routing/src/Patterns/RoutePatternException.cs b/src/Http/Routing/src/Patterns/RoutePatternException.cs index 7b21ac0ac5..c7c95f068c 100644 --- a/src/Http/Routing/src/Patterns/RoutePatternException.cs +++ b/src/Http/Routing/src/Patterns/RoutePatternException.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns private RoutePatternException(SerializationInfo info, StreamingContext context) : base(info, context) { - Pattern = (string)info.GetValue(nameof(Pattern), typeof(string)); + Pattern = (string)info.GetValue(nameof(Pattern), typeof(string))!; } /// diff --git a/src/Http/Routing/src/Patterns/RoutePatternFactory.cs b/src/Http/Routing/src/Patterns/RoutePatternFactory.cs index 91c8ae8b38..8832e94e59 100644 --- a/src/Http/Routing/src/Patterns/RoutePatternFactory.cs +++ b/src/Http/Routing/src/Patterns/RoutePatternFactory.cs @@ -17,8 +17,8 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// public static class RoutePatternFactory { - private static readonly IReadOnlyDictionary EmptyDictionary = - new ReadOnlyDictionary(new Dictionary()); + private static readonly IReadOnlyDictionary EmptyDictionary = + new ReadOnlyDictionary(new Dictionary()); private static readonly IReadOnlyDictionary> EmptyPoliciesDictionary = new ReadOnlyDictionary>(new Dictionary>()); @@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// Multiple policies can be specified for a key by providing a collection as the value. /// /// The . - public static RoutePattern Parse(string pattern, object defaults, object parameterPolicies) + public static RoutePattern Parse(string pattern, object? defaults, object? parameterPolicies) { if (pattern == null) { @@ -86,7 +86,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// Route values that can be substituted for parameters in the route pattern. See remarks on . /// /// The . - public static RoutePattern Parse(string pattern, object defaults, object parameterPolicies, object requiredValues) + public static RoutePattern Parse(string pattern, object? defaults, object? parameterPolicies, object? requiredValues) { if (pattern == null) { @@ -118,7 +118,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// The raw text to associate with the route pattern. May be null. /// The collection of segments. /// The . - public static RoutePattern Pattern(string rawText, IEnumerable segments) + public static RoutePattern Pattern(string? rawText, IEnumerable segments) { if (segments == null) { @@ -146,8 +146,8 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// The collection of segments. /// The . public static RoutePattern Pattern( - object defaults, - object parameterPolicies, + object? defaults, + object? parameterPolicies, IEnumerable segments) { if (segments == null) @@ -177,9 +177,9 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// The collection of segments. /// The . public static RoutePattern Pattern( - string rawText, - object defaults, - object parameterPolicies, + string? rawText, + object? defaults, + object? parameterPolicies, IEnumerable segments) { if (segments == null) @@ -239,8 +239,8 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// The collection of segments. /// The . public static RoutePattern Pattern( - object defaults, - object parameterPolicies, + object? defaults, + object? parameterPolicies, params RoutePatternPathSegment[] segments) { if (segments == null) @@ -270,9 +270,9 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// The collection of segments. /// The . public static RoutePattern Pattern( - string rawText, - object defaults, - object parameterPolicies, + string? rawText, + object? defaults, + object? parameterPolicies, params RoutePatternPathSegment[] segments) { if (segments == null) @@ -284,10 +284,10 @@ namespace Microsoft.AspNetCore.Routing.Patterns } private static RoutePattern PatternCore( - string rawText, - RouteValueDictionary defaults, - RouteValueDictionary parameterPolicies, - RouteValueDictionary requiredValues, + string? rawText, + RouteValueDictionary? defaults, + RouteValueDictionary? parameterPolicies, + RouteValueDictionary? requiredValues, IEnumerable segments) { // We want to merge the segment data with the 'out of line' defaults and parameter policies. @@ -301,10 +301,10 @@ namespace Microsoft.AspNetCore.Routing.Patterns // It's important that these two views of the data are consistent. We don't want // values specified out of line to have a different behavior. - Dictionary updatedDefaults = null; + Dictionary? updatedDefaults = null; if (defaults != null && defaults.Count > 0) { - updatedDefaults = new Dictionary(defaults.Count, StringComparer.OrdinalIgnoreCase); + updatedDefaults = new Dictionary(defaults.Count, StringComparer.OrdinalIgnoreCase); foreach (var kvp in defaults) { @@ -312,7 +312,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns } } - Dictionary> updatedParameterPolicies = null; + Dictionary>? updatedParameterPolicies = null; if (parameterPolicies != null && parameterPolicies.Count > 0) { updatedParameterPolicies = new Dictionary>(parameterPolicies.Count, StringComparer.OrdinalIgnoreCase); @@ -349,7 +349,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns } } - List parameters = null; + List? parameters = null; var updatedSegments = segments.ToArray(); for (var i = 0; i < updatedSegments.Length; i++) { @@ -420,12 +420,12 @@ namespace Microsoft.AspNetCore.Routing.Patterns ? updatedParameterPolicies.ToDictionary(kvp => kvp.Key, kvp => (IReadOnlyList)kvp.Value.ToArray()) : EmptyPoliciesDictionary, requiredValues ?? EmptyDictionary, - (IReadOnlyList)parameters ?? Array.Empty(), + (IReadOnlyList?)parameters ?? Array.Empty(), updatedSegments); RoutePatternPathSegment VisitSegment(RoutePatternPathSegment segment) { - RoutePatternPart[] updatedParts = null; + RoutePatternPart[]? updatedParts = null; for (var i = 0; i < segment.Parts.Count; i++) { var part = segment.Parts[i]; @@ -482,13 +482,13 @@ namespace Microsoft.AspNetCore.Routing.Patterns { if (updatedDefaults == null) { - updatedDefaults = new Dictionary(StringComparer.OrdinalIgnoreCase); + updatedDefaults = new Dictionary(StringComparer.OrdinalIgnoreCase); } updatedDefaults[parameter.Name] = parameter.Default; } - List parameterConstraints = null; + List? parameterConstraints = null; if ((updatedParameterPolicies == null || !updatedParameterPolicies.TryGetValue(parameter.Name, out parameterConstraints)) && parameter.ParameterPolicies.Count > 0) { @@ -503,7 +503,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns if (parameter.ParameterPolicies.Count > 0) { - parameterConstraints.AddRange(parameter.ParameterPolicies); + parameterConstraints!.AddRange(parameter.ParameterPolicies); } if (Equals(parameter.Default, @default) @@ -667,7 +667,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// The . public static RoutePatternParameterPart ParameterPart( string parameterName, - object @default, + object? @default, RoutePatternParameterKind parameterKind) { if (string.IsNullOrEmpty(parameterName)) @@ -703,7 +703,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// The . public static RoutePatternParameterPart ParameterPart( string parameterName, - object @default, + object? @default, RoutePatternParameterKind parameterKind, IEnumerable parameterPolicies) { @@ -745,7 +745,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// The . public static RoutePatternParameterPart ParameterPart( string parameterName, - object @default, + object? @default, RoutePatternParameterKind parameterKind, params RoutePatternParameterPolicyReference[] parameterPolicies) { @@ -778,7 +778,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns private static RoutePatternParameterPart ParameterPartCore( string parameterName, - object @default, + object? @default, RoutePatternParameterKind parameterKind, RoutePatternParameterPolicyReference[] parameterPolicies) { @@ -787,7 +787,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns private static RoutePatternParameterPart ParameterPartCore( string parameterName, - object @default, + object? @default, RoutePatternParameterKind parameterKind, RoutePatternParameterPolicyReference[] parameterPolicies, bool encodeSlashes) @@ -906,7 +906,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns return new RoutePatternParameterPolicyReference(parameterPolicy); } - private static RouteValueDictionary Wrap(object values) + private static RouteValueDictionary? Wrap(object? values) { return values == null ? null : new RouteValueDictionary(values); } diff --git a/src/Http/Routing/src/Patterns/RoutePatternMatcher.cs b/src/Http/Routing/src/Patterns/RoutePatternMatcher.cs index 15bc489a64..f82d860e83 100644 --- a/src/Http/Routing/src/Patterns/RoutePatternMatcher.cs +++ b/src/Http/Routing/src/Patterns/RoutePatternMatcher.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Diagnostics; using Microsoft.AspNetCore.Http; diff --git a/src/Http/Routing/src/Patterns/RoutePatternParameterPart.cs b/src/Http/Routing/src/Patterns/RoutePatternParameterPart.cs index 2b5f408268..30cb7db2da 100644 --- a/src/Http/Routing/src/Patterns/RoutePatternParameterPart.cs +++ b/src/Http/Routing/src/Patterns/RoutePatternParameterPart.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns { internal RoutePatternParameterPart( string parameterName, - object @default, + object? @default, RoutePatternParameterKind parameterKind, RoutePatternParameterPolicyReference[] parameterPolicies) : this(parameterName, @default, parameterKind, parameterPolicies, encodeSlashes: true) @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns internal RoutePatternParameterPart( string parameterName, - object @default, + object? @default, RoutePatternParameterKind parameterKind, RoutePatternParameterPolicyReference[] parameterPolicies, bool encodeSlashes) @@ -53,7 +53,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// /// Gets the default value of this route parameter. May be null. /// - public object Default { get; } + public object? Default { get; } /// /// Returns true if this part is a catch-all parameter. diff --git a/src/Http/Routing/src/Patterns/RoutePatternParameterPolicyReference.cs b/src/Http/Routing/src/Patterns/RoutePatternParameterPolicyReference.cs index 65f58087c1..3fb95983f4 100644 --- a/src/Http/Routing/src/Patterns/RoutePatternParameterPolicyReference.cs +++ b/src/Http/Routing/src/Patterns/RoutePatternParameterPolicyReference.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Diagnostics; @@ -26,16 +26,16 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// /// Gets the constraint text. /// - public string Content { get; } + public string? Content { get; } /// /// Gets a pre-existing that was used to construct this reference. /// - public IParameterPolicy ParameterPolicy { get; } + public IParameterPolicy? ParameterPolicy { get; } - private string DebuggerToString() + private string? DebuggerToString() { return Content; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Patterns/RoutePatternParser.cs b/src/Http/Routing/src/Patterns/RoutePatternParser.cs index 990d1854fc..8b8f181f52 100644 --- a/src/Http/Routing/src/Patterns/RoutePatternParser.cs +++ b/src/Http/Routing/src/Patterns/RoutePatternParser.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Diagnostics; diff --git a/src/Http/Routing/src/Patterns/RoutePatternTransformer.cs b/src/Http/Routing/src/Patterns/RoutePatternTransformer.cs index bea4c610fc..b16ab4cb0e 100644 --- a/src/Http/Routing/src/Patterns/RoutePatternTransformer.cs +++ b/src/Http/Routing/src/Patterns/RoutePatternTransformer.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. namespace Microsoft.AspNetCore.Routing.Patterns @@ -30,6 +30,6 @@ namespace Microsoft.AspNetCore.Routing.Patterns /// return null if any required value cannot be substituted. /// /// - public abstract RoutePattern SubstituteRequiredValues(RoutePattern original, object requiredValues); + public abstract RoutePattern? SubstituteRequiredValues(RoutePattern original, object requiredValues); } } diff --git a/src/Http/Routing/src/RequestDelegateRouteBuilderExtensions.cs b/src/Http/Routing/src/RequestDelegateRouteBuilderExtensions.cs index f376683c4d..8a9e228e46 100644 --- a/src/Http/Routing/src/RequestDelegateRouteBuilderExtensions.cs +++ b/src/Http/Routing/src/RequestDelegateRouteBuilderExtensions.cs @@ -259,7 +259,7 @@ namespace Microsoft.AspNetCore.Routing new RouteHandler(handler), template, defaults: null, - constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint(verb) }), + constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint(verb) })!, dataTokens: null, inlineConstraintResolver: GetConstraintResolver(builder)); diff --git a/src/Http/Routing/src/Route.cs b/src/Http/Routing/src/Route.cs index 0a6afd3b26..555c328127 100644 --- a/src/Http/Routing/src/Route.cs +++ b/src/Http/Routing/src/Route.cs @@ -1,8 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -#nullable enable - using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -62,7 +60,7 @@ namespace Microsoft.AspNetCore.Routing _target = target; } - public string RouteTemplate => ParsedTemplate.TemplateText; + public string? RouteTemplate => ParsedTemplate.TemplateText; protected override Task OnRouteMatched(RouteContext context) { diff --git a/src/Http/Routing/src/RouteBase.cs b/src/Http/Routing/src/RouteBase.cs index 2ba62fbcab..5c9db8f026 100644 --- a/src/Http/Routing/src/RouteBase.cs +++ b/src/Http/Routing/src/RouteBase.cs @@ -1,8 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -#nullable enable - using System; using System.Collections.Generic; using System.Diagnostics; @@ -110,7 +108,7 @@ namespace Microsoft.AspNetCore.Routing { return Task.CompletedTask; } - _logger.RequestMatchedRoute(Name!, ParsedTemplate.TemplateText); + _logger.RequestMatchedRoute(Name!, ParsedTemplate.TemplateText!); return OnRouteMatched(context); } @@ -175,7 +173,7 @@ namespace Microsoft.AspNetCore.Routing RouteTemplate parsedTemplate, IDictionary? constraints) { - var constraintBuilder = new RouteConstraintBuilder(inlineConstraintResolver, parsedTemplate.TemplateText); + var constraintBuilder = new RouteConstraintBuilder(inlineConstraintResolver, parsedTemplate.TemplateText!); if (constraints != null) { @@ -189,12 +187,12 @@ namespace Microsoft.AspNetCore.Routing { if (parameter.IsOptional) { - constraintBuilder.SetOptional(parameter.Name); + constraintBuilder.SetOptional(parameter.Name!); } foreach (var inlineConstraint in parameter.InlineConstraints) { - constraintBuilder.AddResolvedConstraint(parameter.Name, inlineConstraint.Constraint); + constraintBuilder.AddResolvedConstraint(parameter.Name!, inlineConstraint.Constraint); } } @@ -219,7 +217,7 @@ namespace Microsoft.AspNetCore.Routing parameter.Name)); } #else - if (result.ContainsKey(parameter.Name)) + if (result.ContainsKey(parameter.Name!)) { throw new InvalidOperationException( Resources.FormatTemplateRoute_CannotHaveDefaultValueSpecifiedInlineAndExplicitly( @@ -227,7 +225,7 @@ namespace Microsoft.AspNetCore.Routing } else { - result.Add(parameter.Name, parameter.DefaultValue); + result.Add(parameter.Name!, parameter.DefaultValue); } #endif } @@ -300,7 +298,7 @@ namespace Microsoft.AspNetCore.Routing public override string ToString() { - return ParsedTemplate.TemplateText; + return ParsedTemplate.TemplateText!; } } } diff --git a/src/Http/Routing/src/RouteConstraintMatcher.cs b/src/Http/Routing/src/RouteConstraintMatcher.cs index 26728e817a..268e4110cc 100644 --- a/src/Http/Routing/src/RouteConstraintMatcher.cs +++ b/src/Http/Routing/src/RouteConstraintMatcher.cs @@ -53,7 +53,7 @@ namespace Microsoft.AspNetCore.Routing { routeValues.TryGetValue(kvp.Key, out var routeValue); - logger.ConstraintNotMatched(routeValue, kvp.Key, kvp.Value); + logger.ConstraintNotMatched(routeValue!, kvp.Key, kvp.Value); } return false; diff --git a/src/Http/Routing/src/RouteEndpoint.cs b/src/Http/Routing/src/RouteEndpoint.cs index a6750a2ef6..db4f9d4d2b 100644 --- a/src/Http/Routing/src/RouteEndpoint.cs +++ b/src/Http/Routing/src/RouteEndpoint.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -28,8 +28,8 @@ namespace Microsoft.AspNetCore.Routing RequestDelegate requestDelegate, RoutePattern routePattern, int order, - EndpointMetadataCollection metadata, - string displayName) + EndpointMetadataCollection? metadata, + string? displayName) : base(requestDelegate, metadata, displayName) { if (requestDelegate == null) diff --git a/src/Http/Routing/src/RouteEndpointModel.cs b/src/Http/Routing/src/RouteEndpointBuilder.cs similarity index 82% rename from src/Http/Routing/src/RouteEndpointModel.cs rename to src/Http/Routing/src/RouteEndpointBuilder.cs index f06ccba0bf..4397c51cfc 100644 --- a/src/Http/Routing/src/RouteEndpointModel.cs +++ b/src/Http/Routing/src/RouteEndpointBuilder.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing.Patterns; @@ -25,6 +26,11 @@ namespace Microsoft.AspNetCore.Routing public override Endpoint Build() { + if (RequestDelegate is null) + { + throw new InvalidOperationException($"{nameof(RequestDelegate)} must be specified to construct a {nameof(RouteEndpoint)}."); + } + var routeEndpoint = new RouteEndpoint( RequestDelegate, RoutePattern, diff --git a/src/Http/Routing/src/RouteOptions.cs b/src/Http/Routing/src/RouteOptions.cs index dcff5482e7..796554dd58 100644 --- a/src/Http/Routing/src/RouteOptions.cs +++ b/src/Http/Routing/src/RouteOptions.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Routing public class RouteOptions { private IDictionary _constraintTypeMap = GetDefaultConstraintMap(); - private ICollection _endpointDataSources; + private ICollection _endpointDataSources = default!; /// /// Gets a collection of instances configured with routing. diff --git a/src/Http/Routing/src/RouteValueEqualityComparer.cs b/src/Http/Routing/src/RouteValueEqualityComparer.cs index f7cf0570c9..18ffc3070b 100644 --- a/src/Http/Routing/src/RouteValueEqualityComparer.cs +++ b/src/Http/Routing/src/RouteValueEqualityComparer.cs @@ -18,12 +18,12 @@ namespace Microsoft.AspNetCore.Routing /// /// strings are compared using . /// - public class RouteValueEqualityComparer : IEqualityComparer + public class RouteValueEqualityComparer : IEqualityComparer { public static readonly RouteValueEqualityComparer Default = new RouteValueEqualityComparer(); /// - public new bool Equals(object x, object y) + public new bool Equals(object? x, object? y) { var stringX = x as string ?? Convert.ToString(x, CultureInfo.InvariantCulture); var stringY = y as string ?? Convert.ToString(y, CultureInfo.InvariantCulture); @@ -52,4 +52,4 @@ namespace Microsoft.AspNetCore.Routing } } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/RouteValuesAddressScheme.cs b/src/Http/Routing/src/RouteValuesAddressScheme.cs index 3d0db2b0f9..80a74c19a4 100644 --- a/src/Http/Routing/src/RouteValuesAddressScheme.cs +++ b/src/Http/Routing/src/RouteValuesAddressScheme.cs @@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Routing var state = State; - IList matchResults = null; + IList? matchResults = null; if (string.IsNullOrEmpty(address.RouteName)) { matchResults = state.AllMatchesLinkGenerationTree.GetMatches( @@ -141,8 +141,8 @@ namespace Microsoft.AspNetCore.Routing private OutboundRouteEntry CreateOutboundRouteEntry( RouteEndpoint endpoint, - IReadOnlyDictionary requiredValues, - string routeName) + IReadOnlyDictionary requiredValues, + string? routeName) { var entry = new OutboundRouteEntry() { diff --git a/src/Http/Routing/src/Template/InlineConstraint.cs b/src/Http/Routing/src/Template/InlineConstraint.cs index a711ecb136..71be48e247 100644 --- a/src/Http/Routing/src/Template/InlineConstraint.cs +++ b/src/Http/Routing/src/Template/InlineConstraint.cs @@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Routing.Template throw new ArgumentNullException(nameof(other)); } - Constraint = other.Content; + Constraint = other.Content!; } /// @@ -40,4 +40,4 @@ namespace Microsoft.AspNetCore.Routing.Template /// public string Constraint { get; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Template/RouteTemplate.cs b/src/Http/Routing/src/Template/RouteTemplate.cs index ec5d5631f6..2b699e85cc 100644 --- a/src/Http/Routing/src/Template/RouteTemplate.cs +++ b/src/Http/Routing/src/Template/RouteTemplate.cs @@ -68,7 +68,7 @@ namespace Microsoft.AspNetCore.Routing.Template } } - public string TemplateText { get; } + public string? TemplateText { get; } public IList Parameters { get; } diff --git a/src/Http/Routing/src/Template/TemplateBinder.cs b/src/Http/Routing/src/Template/TemplateBinder.cs index 10af79ec2c..366a012b58 100644 --- a/src/Http/Routing/src/Template/TemplateBinder.cs +++ b/src/Http/Routing/src/Template/TemplateBinder.cs @@ -1,8 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -#nullable enable - using System; using System.Collections; using System.Collections.Generic; diff --git a/src/Http/Routing/src/Template/TemplateMatcher.cs b/src/Http/Routing/src/Template/TemplateMatcher.cs index 97dc3565dd..9587525859 100644 --- a/src/Http/Routing/src/Template/TemplateMatcher.cs +++ b/src/Http/Routing/src/Template/TemplateMatcher.cs @@ -51,7 +51,7 @@ namespace Microsoft.AspNetCore.Routing.Template continue; } - if (Defaults.TryGetValue(part.Name, out var value)) + if (Defaults.TryGetValue(part.Name!, out var value)) { _hasDefaultValue[i] = true; _defaultValues[i] = value; diff --git a/src/Http/Routing/src/Template/TemplatePart.cs b/src/Http/Routing/src/Template/TemplatePart.cs index 5d84969a9c..c7acbd1b57 100644 --- a/src/Http/Routing/src/Template/TemplatePart.cs +++ b/src/Http/Routing/src/Template/TemplatePart.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Microsoft.AspNetCore.Routing.Patterns; @@ -32,7 +33,7 @@ namespace Microsoft.AspNetCore.Routing.Template IsCatchAll = parameter.IsCatchAll; IsOptional = parameter.IsOptional; DefaultValue = parameter.Default; - InlineConstraints = parameter.ParameterPolicies?.Select(p => new InlineConstraint(p)); + InlineConstraints = parameter.ParameterPolicies?.Select(p => new InlineConstraint(p)) ?? Enumerable.Empty(); } else if (other.IsSeparator && other is RoutePatternSeparatorPart separator) { @@ -59,8 +60,8 @@ namespace Microsoft.AspNetCore.Routing.Template string name, bool isCatchAll, bool isOptional, - object defaultValue, - IEnumerable inlineConstraints) + object? defaultValue, + IEnumerable? inlineConstraints) { if (name == null) { @@ -83,12 +84,12 @@ namespace Microsoft.AspNetCore.Routing.Template public bool IsParameter { get; private set; } public bool IsOptional { get; private set; } public bool IsOptionalSeperator { get; set; } - public string Name { get; private set; } - public string Text { get; private set; } - public object DefaultValue { get; private set; } - public IEnumerable InlineConstraints { get; private set; } + public string? Name { get; private set; } + public string? Text { get; private set; } + public object? DefaultValue { get; private set; } + public IEnumerable InlineConstraints { get; private set; } = Enumerable.Empty(); - internal string DebuggerToString() + internal string? DebuggerToString() { if (IsParameter) { @@ -104,11 +105,11 @@ namespace Microsoft.AspNetCore.Routing.Template { if (IsLiteral && IsOptionalSeperator) { - return RoutePatternFactory.SeparatorPart(Text); + return RoutePatternFactory.SeparatorPart(Text!); } else if (IsLiteral) { - return RoutePatternFactory.LiteralPart(Text); + return RoutePatternFactory.LiteralPart(Text!); } else { @@ -119,7 +120,7 @@ namespace Microsoft.AspNetCore.Routing.Template RoutePatternParameterKind.Standard; var constraints = InlineConstraints.Select(c => new RoutePatternParameterPolicyReference(c.Constraint)); - return RoutePatternFactory.ParameterPart(Name, DefaultValue, kind, constraints); + return RoutePatternFactory.ParameterPart(Name!, DefaultValue, kind, constraints); } } } diff --git a/src/Http/Routing/src/Template/TemplateValuesResult.cs b/src/Http/Routing/src/Template/TemplateValuesResult.cs index 2a7c46398f..98489a93a8 100644 --- a/src/Http/Routing/src/Template/TemplateValuesResult.cs +++ b/src/Http/Routing/src/Template/TemplateValuesResult.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Routing.Template /// /// The set of values that will appear in the URL. /// - public RouteValueDictionary AcceptedValues { get; set; } + public RouteValueDictionary AcceptedValues { get; set; } = default!; /// /// The set of values that that were supplied for URL generation. @@ -24,6 +24,6 @@ namespace Microsoft.AspNetCore.Routing.Template /// Implicit (ambient) values which are invalidated due to changes in values lexically earlier in the /// route template are excluded from this set. /// - public RouteValueDictionary CombinedValues { get; set; } + public RouteValueDictionary CombinedValues { get; set; } = default!; } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Tree/InboundMatch.cs b/src/Http/Routing/src/Tree/InboundMatch.cs index 57f1b6db7b..9ddb321de0 100644 --- a/src/Http/Routing/src/Tree/InboundMatch.cs +++ b/src/Http/Routing/src/Tree/InboundMatch.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System.Diagnostics; using Microsoft.AspNetCore.Routing.Template; diff --git a/src/Http/Routing/src/Tree/InboundRouteEntry.cs b/src/Http/Routing/src/Tree/InboundRouteEntry.cs index 7c4a5f0abc..893a461d1c 100644 --- a/src/Http/Routing/src/Tree/InboundRouteEntry.cs +++ b/src/Http/Routing/src/Tree/InboundRouteEntry.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System.Collections.Generic; using Microsoft.AspNetCore.Routing.Template; diff --git a/src/Http/Routing/src/Tree/LinkGenerationDecisionTree.cs b/src/Http/Routing/src/Tree/LinkGenerationDecisionTree.cs index 6aa8b84867..47db9e345b 100644 --- a/src/Http/Routing/src/Tree/LinkGenerationDecisionTree.cs +++ b/src/Http/Routing/src/Tree/LinkGenerationDecisionTree.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Diagnostics; diff --git a/src/Http/Routing/src/Tree/OutboundMatch.cs b/src/Http/Routing/src/Tree/OutboundMatch.cs index 49980b9912..95653e3bb5 100644 --- a/src/Http/Routing/src/Tree/OutboundMatch.cs +++ b/src/Http/Routing/src/Tree/OutboundMatch.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using Microsoft.AspNetCore.Routing.Template; namespace Microsoft.AspNetCore.Routing.Tree diff --git a/src/Http/Routing/src/Tree/OutboundRouteEntry.cs b/src/Http/Routing/src/Tree/OutboundRouteEntry.cs index 9c5258df1a..4df5ae351a 100644 --- a/src/Http/Routing/src/Tree/OutboundRouteEntry.cs +++ b/src/Http/Routing/src/Tree/OutboundRouteEntry.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System.Collections.Generic; using Microsoft.AspNetCore.Routing.Template; @@ -64,4 +66,4 @@ namespace Microsoft.AspNetCore.Routing.Tree /// public object Data { get; set; } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/src/Tree/TreeEnumerator.cs b/src/Http/Routing/src/Tree/TreeEnumerator.cs index 5a88f5bc35..2f7d3dc94a 100644 --- a/src/Http/Routing/src/Tree/TreeEnumerator.cs +++ b/src/Http/Routing/src/Tree/TreeEnumerator.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System.Collections; using System.Collections.Generic; using System.Diagnostics; diff --git a/src/Http/Routing/src/Tree/TreeRouteBuilder.cs b/src/Http/Routing/src/Tree/TreeRouteBuilder.cs index 59e06d83c0..1f9e9d0313 100644 --- a/src/Http/Routing/src/Tree/TreeRouteBuilder.cs +++ b/src/Http/Routing/src/Tree/TreeRouteBuilder.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Linq; diff --git a/src/Http/Routing/src/Tree/TreeRouter.cs b/src/Http/Routing/src/Tree/TreeRouter.cs index 9dd6eb290b..5a27d237f3 100644 --- a/src/Http/Routing/src/Tree/TreeRouter.cs +++ b/src/Http/Routing/src/Tree/TreeRouter.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Text.Encodings.Web; diff --git a/src/Http/Routing/src/Tree/UrlMatchingNode.cs b/src/Http/Routing/src/Tree/UrlMatchingNode.cs index ffc387efe9..b829bc7036 100644 --- a/src/Http/Routing/src/Tree/UrlMatchingNode.cs +++ b/src/Http/Routing/src/Tree/UrlMatchingNode.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Diagnostics; diff --git a/src/Http/Routing/src/Tree/UrlMatchingTree.cs b/src/Http/Routing/src/Tree/UrlMatchingTree.cs index 570dce1b3a..3dec2b54bd 100644 --- a/src/Http/Routing/src/Tree/UrlMatchingTree.cs +++ b/src/Http/Routing/src/Tree/UrlMatchingTree.cs @@ -158,7 +158,7 @@ namespace Microsoft.AspNetCore.Routing.Tree current.Matches.Sort((x, y) => { var result = x.Entry.Precedence.CompareTo(y.Entry.Precedence); - return result == 0 ? x.Entry.RouteTemplate.TemplateText.CompareTo(y.Entry.RouteTemplate.TemplateText) : result; + return result == 0 ? x.Entry.RouteTemplate.TemplateText!.CompareTo(y.Entry.RouteTemplate.TemplateText) : result; }); }