diff --git a/sdk/core/Azure.Core/src/RequestFailedException.cs b/sdk/core/Azure.Core/src/RequestFailedException.cs index ff079242e782..734b91784169 100644 --- a/sdk/core/Azure.Core/src/RequestFailedException.cs +++ b/sdk/core/Azure.Core/src/RequestFailedException.cs @@ -259,8 +259,14 @@ internal static bool TryExtractErrorContent(Response response, out ResponseError return false; } // Try the ErrorResponse format and fallback to the ResponseError format. + +#if NET6_0_OR_GREATER + error = System.Text.Json.JsonSerializer.Deserialize(content, ResponseErrorSourceGenerationContext.Default.ErrorResponse)?.Error; + error ??= System.Text.Json.JsonSerializer.Deserialize(content, ResponseErrorSourceGenerationContext.Default.ResponseError); +#else error = System.Text.Json.JsonSerializer.Deserialize(content)?.Error; error ??= System.Text.Json.JsonSerializer.Deserialize(content); +#endif } catch (Exception) { @@ -271,7 +277,8 @@ internal static bool TryExtractErrorContent(Response response, out ResponseError return error != null; } - private class ErrorResponse + // This class needs to be internal rather than private so that it can be used by the System.Text.Json source generator + internal class ErrorResponse { [System.Text.Json.Serialization.JsonPropertyName("error")] public ResponseError? Error { get; set; } diff --git a/sdk/core/Azure.Core/src/ResponseError.cs b/sdk/core/Azure.Core/src/ResponseError.cs index e000ca641fc9..9c41839beb57 100644 --- a/sdk/core/Azure.Core/src/ResponseError.cs +++ b/sdk/core/Azure.Core/src/ResponseError.cs @@ -75,7 +75,8 @@ internal ResponseError(string? code, string? message, string? target, JsonElemen /// internal IReadOnlyList Details { get; } - private class Converter : JsonConverter + // This class needs to be internal rather than private so that it can be used by the System.Text.Json source generator + internal class Converter : JsonConverter { public override ResponseError? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { diff --git a/sdk/core/Azure.Core/src/ResponseErrorSourceGenerationContext.cs b/sdk/core/Azure.Core/src/ResponseErrorSourceGenerationContext.cs new file mode 100644 index 000000000000..5d3ace3171a1 --- /dev/null +++ b/sdk/core/Azure.Core/src/ResponseErrorSourceGenerationContext.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using Azure.Core; + +namespace Azure +{ +#if NET6_0_OR_GREATER + [JsonSerializable(typeof(ResponseError))] + [JsonSerializable(typeof(RequestFailedException.ErrorResponse))] + [JsonSerializable(typeof(ResponseInnerError))] + [JsonSerializable(typeof(JsonElement))] + [JsonSerializable(typeof(JsonDocument))] + [JsonSerializable(typeof(JsonValueKind))] + [JsonSerializable(typeof(string))] + [JsonSerializable(typeof(bool))] + [JsonSerializable(typeof(List))] + internal partial class ResponseErrorSourceGenerationContext : JsonSerializerContext + { + } +#endif +}