-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathOpenApiSchemaReference.cs
More file actions
230 lines (219 loc) · 9.41 KB
/
OpenApiSchemaReference.cs
File metadata and controls
230 lines (219 loc) · 9.41 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
namespace Microsoft.OpenApi
{
/// <summary>
/// Schema reference object
/// </summary>
public class OpenApiSchemaReference : BaseOpenApiReferenceHolder<OpenApiSchema, IOpenApiSchema, JsonSchemaReference>, IOpenApiSchema, IOpenApiSchemaWithUnevaluatedProperties, IOpenApiExtensible
{
/// <summary>
/// Constructor initializing the reference object.
/// </summary>
/// <param name="referenceId">The reference Id.</param>
/// <param name="hostDocument">The host OpenAPI document.</param>
/// <param name="externalResource">Optional: External resource in the reference.
/// It may be:
/// 1. a absolute/relative file path, for example: ../commons/pet.json
/// 2. a Url, for example: http://localhost/pet.json
/// </param>
public OpenApiSchemaReference(string referenceId, OpenApiDocument? hostDocument = null, string? externalResource = null) : base(referenceId, hostDocument, ReferenceType.Schema, externalResource)
{
}
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="schema">The schema reference to copy</param>
private OpenApiSchemaReference(OpenApiSchemaReference schema) : base(schema)
{
}
/// <inheritdoc/>
public string? Description
{
get => string.IsNullOrEmpty(Reference.Description) ? Target?.Description : Reference.Description;
set => Reference.Description = value;
}
/// <inheritdoc/>
public string? Title
{
get => string.IsNullOrEmpty(Reference.Title) ? Target?.Title : Reference.Title;
set => Reference.Title = value;
}
/// <inheritdoc/>
public Uri? Schema { get => Target?.Schema; }
/// <inheritdoc/>
public string? Id { get => Target?.Id; }
/// <inheritdoc/>
public string? Comment { get => Target?.Comment; }
/// <inheritdoc/>
public IDictionary<string, bool>? Vocabulary { get => Target?.Vocabulary; }
/// <inheritdoc/>
public string? DynamicRef { get => Target?.DynamicRef; }
/// <inheritdoc/>
public string? DynamicAnchor { get => Target?.DynamicAnchor; }
/// <inheritdoc/>
public IDictionary<string, IOpenApiSchema>? Definitions { get => Target?.Definitions; }
/// <inheritdoc/>
public string? ExclusiveMaximum { get => Target?.ExclusiveMaximum; }
/// <inheritdoc/>
public string? ExclusiveMinimum { get => Target?.ExclusiveMinimum; }
/// <inheritdoc/>
public JsonSchemaType? Type { get => Target?.Type; }
/// <inheritdoc/>
public string? Const { get => Target?.Const; }
/// <inheritdoc/>
public string? Format { get => Target?.Format; }
/// <inheritdoc/>
public string? Maximum { get => Target?.Maximum; }
/// <inheritdoc/>
public string? Minimum { get => Target?.Minimum; }
/// <inheritdoc/>
public int? MaxLength { get => Target?.MaxLength; }
/// <inheritdoc/>
public int? MinLength { get => Target?.MinLength; }
/// <inheritdoc/>
public string? Pattern { get => Target?.Pattern; }
/// <inheritdoc/>
public decimal? MultipleOf { get => Target?.MultipleOf; }
/// <inheritdoc/>
public JsonNode? Default
{
get => Reference.Default ?? Target?.Default;
set => Reference.Default = value;
}
/// <inheritdoc/>
public bool ReadOnly
{
get => Reference.ReadOnly ?? Target?.ReadOnly ?? false;
set => Reference.ReadOnly = value;
}
/// <inheritdoc/>
public bool WriteOnly
{
get => Reference.WriteOnly ?? Target?.WriteOnly ?? false;
set => Reference.WriteOnly = value;
}
/// <inheritdoc/>
public IList<IOpenApiSchema>? AllOf { get => Target?.AllOf; }
/// <inheritdoc/>
public IList<IOpenApiSchema>? OneOf { get => Target?.OneOf; }
/// <inheritdoc/>
public IList<IOpenApiSchema>? AnyOf { get => Target?.AnyOf; }
/// <inheritdoc/>
public IOpenApiSchema? Not { get => Target?.Not; }
/// <inheritdoc/>
public ISet<string>? Required { get => Target?.Required; }
/// <inheritdoc/>
public IOpenApiSchema? Items { get => Target?.Items; }
/// <inheritdoc/>
public int? MaxItems { get => Target?.MaxItems; }
/// <inheritdoc/>
public int? MinItems { get => Target?.MinItems; }
/// <inheritdoc/>
public bool? UniqueItems { get => Target?.UniqueItems; }
/// <inheritdoc/>
public IDictionary<string, IOpenApiSchema>? Properties { get => Target?.Properties; }
/// <inheritdoc/>
public IDictionary<string, IOpenApiSchema>? PatternProperties { get => Target?.PatternProperties; }
/// <inheritdoc/>
public int? MaxProperties { get => Target?.MaxProperties; }
/// <inheritdoc/>
public int? MinProperties { get => Target?.MinProperties; }
/// <inheritdoc/>
public bool AdditionalPropertiesAllowed { get => Target?.AdditionalPropertiesAllowed ?? true; }
/// <inheritdoc/>
public IOpenApiSchema? AdditionalProperties { get => Target?.AdditionalProperties; }
/// <inheritdoc/>
public OpenApiDiscriminator? Discriminator { get => Target?.Discriminator; }
/// <inheritdoc/>
public JsonNode? Example { get => Target?.Example; }
/// <inheritdoc/>
public IList<JsonNode>? Examples
{
get => Reference.Examples ?? Target?.Examples;
set => Reference.Examples = value;
}
/// <inheritdoc/>
public IList<JsonNode>? Enum { get => Target?.Enum; }
/// <inheritdoc/>
public bool UnevaluatedProperties { get => Target?.UnevaluatedProperties ?? true; }
/// <inheritdoc/>
public IOpenApiSchema? UnevaluatedPropertiesSchema { get => (Target as IOpenApiSchemaWithUnevaluatedProperties)?.UnevaluatedPropertiesSchema; }
/// <inheritdoc/>
public OpenApiExternalDocs? ExternalDocs { get => Target?.ExternalDocs; }
/// <inheritdoc/>
public bool Deprecated
{
get => Reference.Deprecated ?? Target?.Deprecated ?? false;
set => Reference.Deprecated = value;
}
/// <inheritdoc/>
public OpenApiXml? Xml { get => Target?.Xml; }
/// <inheritdoc/>
public IDictionary<string, IOpenApiExtension>? Extensions
{
get => Reference.Extensions ?? Target?.Extensions;
set => Reference.Extensions = value;
}
/// <inheritdoc/>
public IDictionary<string, JsonNode>? UnrecognizedKeywords { get => Target?.UnrecognizedKeywords; }
/// <inheritdoc/>
public IDictionary<string, HashSet<string>>? DependentRequired { get => Target?.DependentRequired; }
/// <inheritdoc/>
public override void SerializeAsV31(IOpenApiWriter writer)
{
SerializeAsWithoutLoops(writer, (w, element) => (element is IOpenApiSchema s ? CopyReferenceAsTargetElementWithOverrides(s) : element).SerializeAsV31(w));
}
/// <inheritdoc/>
public override void SerializeAsV32(IOpenApiWriter writer)
{
SerializeAsWithoutLoops(writer, (w, element) => (element is IOpenApiSchema s ? CopyReferenceAsTargetElementWithOverrides(s) : element).SerializeAsV32(w));
}
/// <inheritdoc/>
public override void SerializeAsV3(IOpenApiWriter writer)
{
SerializeAsWithoutLoops(writer, (w, element) => element.SerializeAsV3(w));
}
/// <inheritdoc/>
public override void SerializeAsV2(IOpenApiWriter writer)
{
SerializeAsWithoutLoops(writer, (w, element) => element.SerializeAsV2(w));
}
private void SerializeAsWithoutLoops(IOpenApiWriter writer, Action<IOpenApiWriter, IOpenApiSerializable> action)
{
if (!writer.GetSettings().ShouldInlineReference(Reference))
{
action(writer, Reference);
}
// If Loop is detected then just Serialize as a reference.
else if (!writer.GetSettings().LoopDetector.PushLoop<IOpenApiSchema>(this))
{
writer.GetSettings().LoopDetector.SaveLoop<IOpenApiSchema>(this);
action(writer, Reference);
}
else
{
SerializeInternal(writer, (w, element) => action(w, element));
writer.GetSettings().LoopDetector.PopLoop<IOpenApiSchema>();
}
}
/// <inheritdoc/>
public override IOpenApiSchema CopyReferenceAsTargetElementWithOverrides(IOpenApiSchema source)
{
return source is OpenApiSchema ? new OpenApiSchema(this) : source;
}
/// <inheritdoc/>
public IOpenApiSchema CreateShallowCopy()
{
return new OpenApiSchemaReference(this);
}
/// <inheritdoc/>
protected override JsonSchemaReference CopyReference(JsonSchemaReference sourceReference)
{
return new JsonSchemaReference(sourceReference);
}
}
}