Skip to content

Commit

Permalink
Issue 1671: Add MemberNotNullWhen attribute for Content property in I…
Browse files Browse the repository at this point in the history
…ApiResponse<T> (#1672)

* Issue 1671: Add MemberNotNullWhen attribute for Content property in IApiResponse<T> interface

* Fix missing IApiResponse implementation for ApiResponse<T> class

* Update APIResponse

Update APIResponse as MemberNotNullWhen needs to be in same interface as members

---------

Co-authored-by: Chris Pulman <chris.pulman@yahoo.com>
  • Loading branch information
sguryev and ChrisPulman committed May 26, 2024
1 parent aa78fc0 commit c0499cf
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions Refit/ApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static T Create<T, TBody>(
/// Implementation of <see cref="IApiResponse{T}"/> that provides additional functionalities.
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class ApiResponse<T> : IApiResponse<T>
public sealed class ApiResponse<T> : IApiResponse<T>, IApiResponse
{
readonly HttpResponseMessage response;
bool disposed;
Expand Down Expand Up @@ -60,6 +60,8 @@ public ApiResponse(
/// </summary>
public T? Content { get; }

object? IApiResponse.Content => Content;

/// <summary>
/// Refit settings used to send the request.
/// </summary>
Expand Down Expand Up @@ -163,14 +165,29 @@ public interface IApiResponse<out T> : IApiResponse
/// <summary>
/// Deserialized request content as <typeparamref name="T"/>.
/// </summary>
T? Content { get; }
new T? Content { get; }
}

/// <summary>
/// Base interface used to represent an API response.
/// </summary>
public interface IApiResponse : IDisposable
{
/// <summary>
/// Indicates whether the request was successful.
/// </summary>
#if NET6_0_OR_GREATER
[MemberNotNullWhen(true, nameof(ContentHeaders))]
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Content))]
#endif
bool IsSuccessStatusCode { get; }

/// <summary>
/// Deserialized request content as an object.
/// </summary>
object? Content { get; }

/// <summary>
/// HTTP response headers.
/// </summary>
Expand All @@ -181,15 +198,6 @@ public interface IApiResponse : IDisposable
/// </summary>
HttpContentHeaders? ContentHeaders { get; }

/// <summary>
/// Indicates whether the request was successful.
/// </summary>
#if NET6_0_OR_GREATER
[MemberNotNullWhen(true, nameof(ContentHeaders))]
[MemberNotNullWhen(false, nameof(Error))]
#endif
bool IsSuccessStatusCode { get; }

/// <summary>
/// HTTP response status code.
/// </summary>
Expand Down

0 comments on commit c0499cf

Please sign in to comment.