Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f9aa66d

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

File tree

Expand file treeCollapse file tree

1 file changed

+20
-7
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+20
-7
lines changed

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

Copy file name to clipboardExpand all lines: src/AspNetCore/WebApi/src/Asp.Versioning.Http/ErrorObjectWriter.cs
+20-7Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.