diff --git a/identity-server/src/IdentityServer/Extensions/StringsExtensions.cs b/identity-server/src/IdentityServer/Extensions/StringsExtensions.cs index a92d2ff1b..3ecc90a9c 100644 --- a/identity-server/src/IdentityServer/Extensions/StringsExtensions.cs +++ b/identity-server/src/IdentityServer/Extensions/StringsExtensions.cs @@ -2,6 +2,7 @@ // See LICENSE in the project root for license information. +#nullable enable using Microsoft.AspNetCore.WebUtilities; using System; using System.Collections.Generic; @@ -16,7 +17,7 @@ namespace Duende.IdentityServer.Extensions; internal static class StringExtensions { [DebuggerStepThrough] - public static string ToSpaceSeparatedString(this IEnumerable list) + public static string ToSpaceSeparatedString(this IEnumerable? list) { if (list == null) { @@ -33,7 +34,7 @@ public static IEnumerable FromSpaceSeparatedString(this string input) return input.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(); } - public static List ParseScopesString(this string scopes) + public static List? ParseScopesString(this string scopes) { if (scopes.IsMissing()) { @@ -53,13 +54,13 @@ public static List ParseScopesString(this string scopes) } [DebuggerStepThrough] - public static bool IsMissing([NotNullWhen(false)]this string value) + public static bool IsMissing([NotNullWhen(false)] this string? value) { return string.IsNullOrWhiteSpace(value); } [DebuggerStepThrough] - public static bool IsMissingOrTooLong(this string value, int maxLength) + public static bool IsMissingOrTooLong(this string? value, int maxLength) { if (string.IsNullOrWhiteSpace(value)) { @@ -74,13 +75,14 @@ public static bool IsMissingOrTooLong(this string value, int maxLength) } [DebuggerStepThrough] - public static bool IsPresent([NotNullWhen(true)] this string value) + public static bool IsPresent([NotNullWhen(true)] this string? value) { return !string.IsNullOrWhiteSpace(value); } [DebuggerStepThrough] - public static string EnsureLeadingSlash(this string url) + [return: NotNullIfNotNull("url")] + public static string? EnsureLeadingSlash(this string? url) { if (url != null && !url.StartsWith('/')) { @@ -91,7 +93,8 @@ public static string EnsureLeadingSlash(this string url) } [DebuggerStepThrough] - public static string EnsureTrailingSlash(this string url) + [return: NotNullIfNotNull("url")] + public static string? EnsureTrailingSlash(this string? url) { if (url != null && !url.EndsWith('/')) { @@ -102,7 +105,8 @@ public static string EnsureTrailingSlash(this string url) } [DebuggerStepThrough] - public static string RemoveLeadingSlash(this string url) + [return: NotNullIfNotNull("url")] + public static string? RemoveLeadingSlash(this string? url) { if (url != null && url.StartsWith('/')) { @@ -113,7 +117,8 @@ public static string RemoveLeadingSlash(this string url) } [DebuggerStepThrough] - public static string RemoveTrailingSlash(this string url) + [return: NotNullIfNotNull("url")] + public static string? RemoveTrailingSlash(this string? url) { if (url != null && url.EndsWith('/')) { @@ -124,7 +129,7 @@ public static string RemoveTrailingSlash(this string url) } [DebuggerStepThrough] - public static string CleanUrlPath(this string url) + public static string CleanUrlPath(this string? url) { if (String.IsNullOrWhiteSpace(url)) url = "/"; @@ -137,7 +142,7 @@ public static string CleanUrlPath(this string url) } [DebuggerStepThrough] - public static bool IsLocalUrl(this string url) + public static bool IsLocalUrl([NotNullWhen(false)] this string? url) { // This implementation is a copy of a https://github.com/dotnet/aspnetcore/blob/3f1acb59718cadf111a0a796681e3d3509bb3381/src/Mvc/Mvc.Core/src/Routing/UrlHelperBase.cs#L315 // We originally copied that code to avoid a dependency, but we could potentially remove this entirely by switching to the Microsoft.NET.Sdk.Web sdk. @@ -248,7 +253,7 @@ public static string AddHashFragment(this string url, string query) } [DebuggerStepThrough] - public static NameValueCollection ReadQueryStringAsNameValueCollection(this string url) + public static NameValueCollection ReadQueryStringAsNameValueCollection(this string? url) { if (url != null) { @@ -267,7 +272,7 @@ public static NameValueCollection ReadQueryStringAsNameValueCollection(this stri return new NameValueCollection(); } - public static string GetOrigin(this string url) + public static string? GetOrigin(this string? url) { if (url != null) {