Skip to content

Commit

Permalink
Expose JsonSerializerContext for extenders. Related to #1072
Browse files Browse the repository at this point in the history
  • Loading branch information
commonsensesoftware committed Mar 24, 2024
1 parent 59ff9e6 commit 3823d43
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/AspNetCore/WebApi/src/Asp.Versioning.Http/ErrorObjectWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.

// Ignore Spelling: Serializer
namespace Asp.Versioning;

using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -30,6 +31,19 @@ public partial class ErrorObjectWriter : IProblemDetailsWriter
public ErrorObjectWriter( IOptions<JsonOptions> options ) =>
this.options = ( options ?? throw new ArgumentNullException( nameof( options ) ) ).Value.SerializerOptions;

/// <summary>
/// Gets the associated, default <see cref="JsonSerializerContext"/>.
/// </summary>
/// <value>The associated, default <see cref="JsonSerializerContext"/>.</value>
public static JsonSerializerContext DefaultJsonSerializerContext => ErrorObjectJsonContext.Default;

/// <summary>
/// Creates and returns a new <see cref="JsonSerializerContext"/> associated with the writer.
/// </summary>
/// <param name="options">The <see cref="JsonSerializerOptions">JSON serializer options</see> to use.</param>
/// <returns>A new <see cref="JsonSerializerContext"/>.</returns>
public static JsonSerializerContext NewJsonSerializerContext( JsonSerializerOptions options ) => new ErrorObjectJsonContext( options );

/// <inheritdoc />
public virtual bool CanWrite( ProblemDetailsContext context )
{
Expand Down Expand Up @@ -89,6 +103,7 @@ internal ErrorObject( ProblemDetails problemDetails ) =>
/// </summary>
protected internal readonly partial struct ErrorDetail
{
private const string CodeProperty = "code";
private readonly ProblemDetails problemDetails;
private readonly InnerError? innerError;
private readonly Dictionary<string, object> extensions = [];
Expand All @@ -103,23 +118,21 @@ internal ErrorDetail( ProblemDetails problemDetails )
/// Gets or sets one of a server-defined set of error codes.
/// </summary>
/// <value>A server-defined error code.</value>
[JsonPropertyName( "code" )]
[JsonPropertyName( CodeProperty )]
[JsonIgnore( Condition = WhenWritingNull )]
public string? Code
{
get => problemDetails.Extensions.TryGetValue( "code", out var value ) &&
value is string code ?
code :
default;
get => problemDetails.Extensions.TryGetValue( CodeProperty, out var value ) &&
value is string code ? code : default;
set
{
if ( value is null )
{
problemDetails.Extensions.Remove( "code" );
problemDetails.Extensions.Remove( CodeProperty );
}
else
{
problemDetails.Extensions["code"] = value;
problemDetails.Extensions[CodeProperty] = value;
}
}
}
Expand Down

0 comments on commit 3823d43

Please sign in to comment.