This repository was archived by the owner on Nov 11, 2025. It is now read-only.
forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonSchemaReference.cs
More file actions
148 lines (129 loc) · 5.64 KB
/
JsonSchemaReference.cs
File metadata and controls
148 lines (129 loc) · 5.64 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
namespace Microsoft.OpenApi;
/// <summary>
/// Schema reference information that includes metadata annotations from JSON Schema 2020-12.
/// This class extends OpenApiReference to provide schema-specific metadata override capabilities.
/// </summary>
public class JsonSchemaReference : OpenApiReferenceWithDescription
{
/// <summary>
/// A default value which by default SHOULD override that of the referenced component.
/// If the referenced object-type does not allow a default field, then this field has no effect.
/// </summary>
public JsonNode? Default { get; set; }
/// <summary>
/// A title which by default SHOULD override that of the referenced component.
/// If the referenced object-type does not allow a title field, then this field has no effect.
/// </summary>
public string? Title { get; set; }
/// <summary>
/// Indicates whether the referenced component is deprecated.
/// If the referenced object-type does not allow a deprecated field, then this field has no effect.
/// </summary>
public bool? Deprecated { get; set; }
/// <summary>
/// Indicates whether the referenced component is read-only.
/// If the referenced object-type does not allow a readOnly field, then this field has no effect.
/// </summary>
public bool? ReadOnly { get; set; }
/// <summary>
/// Indicates whether the referenced component is write-only.
/// If the referenced object-type does not allow a writeOnly field, then this field has no effect.
/// </summary>
public bool? WriteOnly { get; set; }
/// <summary>
/// Example values which by default SHOULD override those of the referenced component.
/// If the referenced object-type does not allow examples, then this field has no effect.
/// </summary>
public IList<JsonNode>? Examples { get; set; }
/// <summary>
/// Parameterless constructor
/// </summary>
public JsonSchemaReference() { }
/// <summary>
/// Initializes a copy instance of the <see cref="JsonSchemaReference"/> object
/// </summary>
public JsonSchemaReference(JsonSchemaReference reference) : base(reference)
{
Utils.CheckArgumentNull(reference);
Default = reference.Default;
Title = reference.Title;
Deprecated = reference.Deprecated;
ReadOnly = reference.ReadOnly;
WriteOnly = reference.WriteOnly;
Examples = reference.Examples;
}
/// <inheritdoc/>
protected override void SerializeAdditionalV31Properties(IOpenApiWriter writer)
{
SerializeAdditionalV3XProperties(writer, base.SerializeAdditionalV31Properties);
}
/// <inheritdoc/>
protected override void SerializeAdditionalV32Properties(IOpenApiWriter writer)
{
SerializeAdditionalV3XProperties(writer, base.SerializeAdditionalV32Properties);
}
private void SerializeAdditionalV3XProperties(IOpenApiWriter writer, Action<IOpenApiWriter> baseSerializer)
{
if (Type != ReferenceType.Schema) throw new InvalidOperationException(
$"JsonSchemaReference can only be serialized for ReferenceType.Schema, but was {Type}.");
baseSerializer(writer);
// Additional schema metadata annotations in 3.1
writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d));
writer.WriteProperty(OpenApiConstants.Title, Title);
if (Deprecated.HasValue)
{
writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated.Value, false);
}
if (ReadOnly.HasValue)
{
writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly.Value, false);
}
if (WriteOnly.HasValue)
{
writer.WriteProperty(OpenApiConstants.WriteOnly, WriteOnly.Value, false);
}
if (Examples != null && Examples.Any())
{
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (w, e) => w.WriteAny(e));
}
}
/// <inheritdoc/>
protected override void SetAdditional31MetadataFromMapNode(JsonObject jsonObject)
{
base.SetAdditional31MetadataFromMapNode(jsonObject);
var title = GetPropertyValueFromNode(jsonObject, OpenApiConstants.Title);
if (!string.IsNullOrEmpty(title))
{
Title = title;
}
// Boolean properties
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Deprecated, out var deprecatedNode) && deprecatedNode is JsonValue deprecatedValue && deprecatedValue.TryGetValue<bool>(out var deprecated))
{
Deprecated = deprecated;
}
if (jsonObject.TryGetPropertyValue(OpenApiConstants.ReadOnly, out var readOnlyNode) && readOnlyNode is JsonValue readOnlyValue && readOnlyValue.TryGetValue<bool>(out var readOnly))
{
ReadOnly = readOnly;
}
if (jsonObject.TryGetPropertyValue(OpenApiConstants.WriteOnly, out var writeOnlyNode) && writeOnlyNode is JsonValue writeOnlyValue && writeOnlyValue.TryGetValue<bool>(out var writeOnly))
{
WriteOnly = writeOnly;
}
// Default value
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Default, out var defaultNode))
{
Default = defaultNode;
}
// Examples
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Examples, out var examplesNode) && examplesNode is JsonArray examplesArray)
{
Examples = examplesArray.OfType<JsonNode>().ToList();
}
}
}