Skip to content

Commit

Permalink
Improve nullable annotations for IsMissing/IsPresent string extensions (
Browse files Browse the repository at this point in the history
IdentityModel#526)

By declaring `string? value` as nullable we can get rid of many null-forgiving operators throughout the code.
  • Loading branch information
0xced authored Sep 1, 2023
1 parent 88dda0f commit bcd6b41
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Client/Extensions/HttpClientDiscoveryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(th
public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(this HttpMessageInvoker client, DiscoveryDocumentRequest request, CancellationToken cancellationToken = default)
{
string address;
if (request.Address!.IsPresent())
if (request.Address.IsPresent())
{
address = request.Address!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static async Task<DynamicClientRegistrationResponse> RegisterClientAsync(
"application/json");
clone.Prepare();

if (request.Token!.IsPresent())
if (request.Token.IsPresent())
{
clone.SetBearerToken(request.Token!);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Extensions/HttpClientUserInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class HttpClientUserInfoExtensions
/// <returns></returns>
public static async Task<UserInfoResponse> GetUserInfoAsync(this HttpMessageInvoker client, UserInfoRequest request, CancellationToken cancellationToken = default)
{
if (request.Token!.IsMissing()) throw new ArgumentNullException(nameof(request.Token));
if (request.Token.IsMissing()) throw new ArgumentNullException(nameof(request.Token));

var clone = request.Clone();

Expand Down
2 changes: 1 addition & 1 deletion src/Client/Messages/AuthorizeResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public AuthorizeResponse(string raw)
/// <value>
/// <c>true</c> if the response is an error; otherwise, <c>false</c>.
/// </value>
public bool IsError => Error!.IsPresent();
public bool IsError => Error.IsPresent();

/// <summary>
/// Gets the expires in.
Expand Down
6 changes: 3 additions & 3 deletions src/Client/Messages/Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Parameters : List<KeyValuePair<string, string>>
foreach (var prop in values.GetType().GetRuntimeProperties())
{
var value = prop.GetValue(values) as string;
if (value!.IsPresent())
if (value.IsPresent())
{
dictionary.Add(prop.Name, value!);
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public void AddOptional(string key, string? value, bool allowDuplicates = false)
}
}

if (value!.IsPresent())
if (value.IsPresent())
{
Add(key, value!);
}
Expand Down Expand Up @@ -160,7 +160,7 @@ public void AddRequired(string key, string? value, bool allowDuplicates = false,
}
}

if (value!.IsPresent() || allowEmptyValue)
if (value.IsPresent() || allowEmptyValue)
{
Add(key, value!);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Messages/ProtocolRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ public void Prepare()
Parameters.AddOptional(OidcConstants.TokenRequest.ClientAssertion, ClientAssertion.Value);
}

if (Address!.IsPresent())
if (Address.IsPresent())
{
RequestUri = new Uri(Address!, UriKind.RelativeOrAbsolute);
}

if (DPoPProofToken!.IsPresent())
if (DPoPProofToken.IsPresent())
{
Headers.Add(OidcConstants.HttpHeaders.DPoP, DPoPProofToken);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Client/Messages/ProtocolResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ProtocolResponse
{
response.ErrorType = ResponseErrorType.Http;

if (content!.IsPresent())
if (content.IsPresent())
{
try
{
Expand All @@ -66,7 +66,7 @@ public class ProtocolResponse
// either 200 or 400 - both cases need a JSON response (if present), otherwise error
try
{
if (content!.IsPresent())
if (content.IsPresent())
{
response.Json = JsonDocument.Parse(content!).RootElement;
}
Expand Down Expand Up @@ -156,7 +156,7 @@ protected virtual Task InitializeAsync(object? initializationData = null)
/// <value>
/// <c>true</c> if an error occurred; otherwise, <c>false</c>.
/// </value>
public bool IsError => Error!.IsPresent() || ErrorType != ResponseErrorType.None;
public bool IsError => Error.IsPresent() || ErrorType != ResponseErrorType.None;

/// <summary>
/// Gets the type of the error.
Expand Down Expand Up @@ -200,7 +200,7 @@ public string? Error
{
get
{
if (ErrorMessage!.IsPresent())
if (ErrorMessage.IsPresent())
{
return ErrorMessage;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Internal/InternalStringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace IdentityModel.Internal;
internal static class InternalStringExtensions
{
[DebuggerStepThrough]
public static bool IsMissing(this string value)
public static bool IsMissing(this string? value)
{
return string.IsNullOrWhiteSpace(value);
}

[DebuggerStepThrough]
public static bool IsPresent(this string value)
public static bool IsPresent(this string? value)
{
return !(value.IsMissing());
}
Expand Down

0 comments on commit bcd6b41

Please sign in to comment.