From dc147458fa1d8a7870239cf6fdb7479392004bee Mon Sep 17 00:00:00 2001 From: Christopher Scott Date: Thu, 13 Apr 2023 14:19:28 -0500 Subject: [PATCH 1/5] Mark NullableResponseOfT.Value as nullable --- sdk/core/Azure.Core/src/NullableResponseOfT.cs | 2 +- sdk/core/Azure.Core/src/ResponseOfT.cs | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/sdk/core/Azure.Core/src/NullableResponseOfT.cs b/sdk/core/Azure.Core/src/NullableResponseOfT.cs index 39f94ef0637a6..d14cd7ac07ee1 100644 --- a/sdk/core/Azure.Core/src/NullableResponseOfT.cs +++ b/sdk/core/Azure.Core/src/NullableResponseOfT.cs @@ -23,7 +23,7 @@ public abstract class NullableResponse /// /// Gets the value returned by the service. Accessing this property will throw if is false. /// - public abstract T Value { get; } + public abstract T? Value { get; } /// /// Returns the HTTP response returned by the service. diff --git a/sdk/core/Azure.Core/src/ResponseOfT.cs b/sdk/core/Azure.Core/src/ResponseOfT.cs index 96586ee3c458d..602e2c84a38a9 100644 --- a/sdk/core/Azure.Core/src/ResponseOfT.cs +++ b/sdk/core/Azure.Core/src/ResponseOfT.cs @@ -20,7 +20,10 @@ public abstract class Response : NullableResponse { /// [EditorBrowsable(EditorBrowsableState.Never)] - public override bool HasValue => true; + public override bool HasValue => Value != null; + + /// + public override T Value => Value!; /// /// Returns the value of this object. @@ -28,14 +31,14 @@ public abstract class Response : NullableResponse /// The instance. public static implicit operator T(Response response) { - if (response == null) + if (response == null || !response.HasValue) { #pragma warning disable CA1065 // Don't throw from cast operators throw new ArgumentNullException(nameof(response), $"The implicit cast from Response<{typeof(T)}> to {typeof(T)} failed because the Response<{typeof(T)}> was null."); #pragma warning restore CA1065 } - return response.Value; + return response.Value!; } /// From 3657b23672993587e07a69d286fa44656ab7f1da Mon Sep 17 00:00:00 2001 From: Christopher Scott Date: Thu, 13 Apr 2023 14:43:44 -0500 Subject: [PATCH 2/5] export --- sdk/core/Azure.Core/api/Azure.Core.net461.cs | 3 ++- sdk/core/Azure.Core/api/Azure.Core.net5.0.cs | 3 ++- sdk/core/Azure.Core/api/Azure.Core.net6.0.cs | 3 ++- sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs | 3 ++- sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/sdk/core/Azure.Core/api/Azure.Core.net461.cs b/sdk/core/Azure.Core/api/Azure.Core.net461.cs index 4ccc966869bb5..6ebfae9688d75 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net461.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net461.cs @@ -117,7 +117,7 @@ public abstract partial class NullableResponse { protected NullableResponse() { } public abstract bool HasValue { get; } - public abstract T Value { get; } + public abstract T? Value { get; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] @@ -256,6 +256,7 @@ public abstract partial class Response : Azure.NullableResponse protected Response() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool HasValue { get { throw null; } } + public override T Value { get { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] diff --git a/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs b/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs index 0d35cd47af870..b5e961b765a3c 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs @@ -117,7 +117,7 @@ public abstract partial class NullableResponse { protected NullableResponse() { } public abstract bool HasValue { get; } - public abstract T Value { get; } + public abstract T? Value { get; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] @@ -256,6 +256,7 @@ public abstract partial class Response : Azure.NullableResponse protected Response() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool HasValue { get { throw null; } } + public override T Value { get { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] diff --git a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs index 0d35cd47af870..b5e961b765a3c 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs @@ -117,7 +117,7 @@ public abstract partial class NullableResponse { protected NullableResponse() { } public abstract bool HasValue { get; } - public abstract T Value { get; } + public abstract T? Value { get; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] @@ -256,6 +256,7 @@ public abstract partial class Response : Azure.NullableResponse protected Response() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool HasValue { get { throw null; } } + public override T Value { get { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] diff --git a/sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs b/sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs index 4ccc966869bb5..6ebfae9688d75 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs @@ -117,7 +117,7 @@ public abstract partial class NullableResponse { protected NullableResponse() { } public abstract bool HasValue { get; } - public abstract T Value { get; } + public abstract T? Value { get; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] @@ -256,6 +256,7 @@ public abstract partial class Response : Azure.NullableResponse protected Response() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool HasValue { get { throw null; } } + public override T Value { get { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] diff --git a/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs b/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs index 4ccc966869bb5..6ebfae9688d75 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs @@ -117,7 +117,7 @@ public abstract partial class NullableResponse { protected NullableResponse() { } public abstract bool HasValue { get; } - public abstract T Value { get; } + public abstract T? Value { get; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] @@ -256,6 +256,7 @@ public abstract partial class Response : Azure.NullableResponse protected Response() { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool HasValue { get { throw null; } } + public override T Value { get { throw null; } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] From 4982eb7d0621136462900cafe52e494e46207b80 Mon Sep 17 00:00:00 2001 From: Christopher Scott Date: Thu, 13 Apr 2023 15:09:13 -0500 Subject: [PATCH 3/5] revert implict cast check --- sdk/core/Azure.Core/src/ResponseOfT.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/Azure.Core/src/ResponseOfT.cs b/sdk/core/Azure.Core/src/ResponseOfT.cs index 602e2c84a38a9..e7ae31b97d56e 100644 --- a/sdk/core/Azure.Core/src/ResponseOfT.cs +++ b/sdk/core/Azure.Core/src/ResponseOfT.cs @@ -31,7 +31,7 @@ public abstract class Response : NullableResponse /// The instance. public static implicit operator T(Response response) { - if (response == null || !response.HasValue) + if (response == null) { #pragma warning disable CA1065 // Don't throw from cast operators throw new ArgumentNullException(nameof(response), $"The implicit cast from Response<{typeof(T)}> to {typeof(T)} failed because the Response<{typeof(T)}> was null."); From bf2573b2378c4b6293808e4fd4f8cbcc8d5a17f8 Mon Sep 17 00:00:00 2001 From: Christopher Scott Date: Mon, 26 Jun 2023 15:54:26 -0500 Subject: [PATCH 4/5] remove ! annotation for Value --- sdk/core/Azure.Core/src/ResponseOfT.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/Azure.Core/src/ResponseOfT.cs b/sdk/core/Azure.Core/src/ResponseOfT.cs index e7ae31b97d56e..78d48a5d0fb80 100644 --- a/sdk/core/Azure.Core/src/ResponseOfT.cs +++ b/sdk/core/Azure.Core/src/ResponseOfT.cs @@ -23,7 +23,7 @@ public abstract class Response : NullableResponse public override bool HasValue => Value != null; /// - public override T Value => Value!; + public override T Value => Value; /// /// Returns the value of this object. From f9c7aac6c68d6a6b6a334aa5faf03940eb352fa9 Mon Sep 17 00:00:00 2001 From: Christopher Scott Date: Fri, 28 Jul 2023 09:49:05 -0500 Subject: [PATCH 5/5] revert HasValue behavior change --- sdk/core/Azure.Core/src/ResponseOfT.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/Azure.Core/src/ResponseOfT.cs b/sdk/core/Azure.Core/src/ResponseOfT.cs index 78d48a5d0fb80..0dbded789a539 100644 --- a/sdk/core/Azure.Core/src/ResponseOfT.cs +++ b/sdk/core/Azure.Core/src/ResponseOfT.cs @@ -20,7 +20,7 @@ public abstract class Response : NullableResponse { /// [EditorBrowsable(EditorBrowsableState.Never)] - public override bool HasValue => Value != null; + public override bool HasValue => true; /// public override T Value => Value;