Skip to content

Commit f9aa66d

Browse files
Expose JsonSerializerContext for extenders. Related to #1072
1 parent 0b50d45 commit f9aa66d

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/AspNetCore/WebApi/src/Asp.Versioning.Http/ErrorObjectWriter.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22

3+
// Ignore Spelling: Serializer
34
namespace Asp.Versioning;
45

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

34+
/// <summary>
35+
/// Gets the associated, default <see cref="JsonSerializerContext"/>.
36+
/// </summary>
37+
/// <value>The associated, default <see cref="JsonSerializerContext"/>.</value>
38+
public static JsonSerializerContext DefaultJsonSerializerContext => ErrorObjectJsonContext.Default;
39+
40+
/// <summary>
41+
/// Creates and returns a new <see cref="JsonSerializerContext"/> associated with the writer.
42+
/// </summary>
43+
/// <param name="options">The <see cref="JsonSerializerOptions">JSON serializer options</see> to use.</param>
44+
/// <returns>A new <see cref="JsonSerializerContext"/>.</returns>
45+
public static JsonSerializerContext NewJsonSerializerContext( JsonSerializerOptions options ) => new ErrorObjectJsonContext( options );
46+
3347
/// <inheritdoc />
3448
public virtual bool CanWrite( ProblemDetailsContext context )
3549
{
@@ -89,6 +103,7 @@ internal ErrorObject( ProblemDetails problemDetails ) =>
89103
/// </summary>
90104
protected internal readonly partial struct ErrorDetail
91105
{
106+
private const string CodeProperty = "code";
92107
private readonly ProblemDetails problemDetails;
93108
private readonly InnerError? innerError;
94109
private readonly Dictionary<string, object> extensions = [];
@@ -103,23 +118,21 @@ internal ErrorDetail( ProblemDetails problemDetails )
103118
/// Gets or sets one of a server-defined set of error codes.
104119
/// </summary>
105120
/// <value>A server-defined error code.</value>
106-
[JsonPropertyName( "code" )]
121+
[JsonPropertyName( CodeProperty )]
107122
[JsonIgnore( Condition = WhenWritingNull )]
108123
public string? Code
109124
{
110-
get => problemDetails.Extensions.TryGetValue( "code", out var value ) &&
111-
value is string code ?
112-
code :
113-
default;
125+
get => problemDetails.Extensions.TryGetValue( CodeProperty, out var value ) &&
126+
value is string code ? code : default;
114127
set
115128
{
116129
if ( value is null )
117130
{
118-
problemDetails.Extensions.Remove( "code" );
131+
problemDetails.Extensions.Remove( CodeProperty );
119132
}
120133
else
121134
{
122-
problemDetails.Extensions["code"] = value;
135+
problemDetails.Extensions[CodeProperty] = value;
123136
}
124137
}
125138
}

0 commit comments

Comments
 (0)