-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiPrimaryErrorMessageExtension.cs
More file actions
49 lines (43 loc) · 1.84 KB
/
OpenApiPrimaryErrorMessageExtension.cs
File metadata and controls
49 lines (43 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.MicrosoftExtensions;
/// <summary>
/// Extension element for OpenAPI to add tag the primary error message to use on error types. x-ms-primary-error-message
/// </summary>
public class OpenApiPrimaryErrorMessageExtension : IOpenApiExtension
{
/// <summary>
/// Name of the extension as used in the description.
/// </summary>
public static string Name => "x-ms-primary-error-message";
/// <inheritdoc />
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
if (writer is null) throw new ArgumentNullException(nameof(writer));
writer.WriteValue(IsPrimaryErrorMessage);
}
/// <summary>
/// Whether this property is the primary error message to use on error types.
/// </summary>
public bool IsPrimaryErrorMessage { get; set; }
/// <summary>
/// Parses the <see cref="JsonNodeExtension"/> to <see cref="OpenApiPrimaryErrorMessageExtension"/>.
/// </summary>
/// <param name="source">The source object.</param>
/// <returns>The <see cref="OpenApiPrimaryErrorMessageExtension"/>.</returns>
public static OpenApiPrimaryErrorMessageExtension Parse(JsonNode source)
{
if (source is not JsonValue rawObject) throw new ArgumentOutOfRangeException(nameof(source));
return new()
{
IsPrimaryErrorMessage = rawObject.TryGetValue<bool>(out var value) && value
};
}
}