-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiSchemaReference.cs
More file actions
206 lines (196 loc) · 8.63 KB
/
OpenApiSchemaReference.cs
File metadata and controls
206 lines (196 loc) · 8.63 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Writers;
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
namespace Microsoft.OpenApi.Models.References
{
/// <summary>
/// Schema reference object
/// </summary>
public class OpenApiSchemaReference : BaseOpenApiReferenceHolder<OpenApiSchema, IOpenApiSchema>, IOpenApiSchema
{
/// <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
{
if (Reference is not null)
{
Reference.Description = value;
}
}
}
/// <inheritdoc/>
public string Title { get => Target?.Title; }
/// <inheritdoc/>
public string 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 decimal? V31ExclusiveMaximum { get => Target?.V31ExclusiveMaximum; }
/// <inheritdoc/>
public decimal? V31ExclusiveMinimum { get => Target?.V31ExclusiveMinimum; }
/// <inheritdoc/>
public bool UnEvaluatedProperties { get => Target?.UnEvaluatedProperties ?? false; }
/// <inheritdoc/>
public JsonSchemaType? Type { get => Target?.Type; }
/// <inheritdoc/>
public string Const { get => Target?.Const; }
/// <inheritdoc/>
public string Format { get => Target?.Format; }
/// <inheritdoc/>
public decimal? Maximum { get => Target?.Maximum; }
/// <inheritdoc/>
public bool? ExclusiveMaximum { get => Target?.ExclusiveMaximum; }
/// <inheritdoc/>
public decimal? Minimum { get => Target?.Minimum; }
/// <inheritdoc/>
public bool? ExclusiveMinimum { get => Target?.ExclusiveMinimum; }
/// <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 => Target?.Default; }
/// <inheritdoc/>
public bool ReadOnly { get => Target?.ReadOnly ?? false; }
/// <inheritdoc/>
public bool WriteOnly { get => Target?.WriteOnly ?? false; }
/// <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 => Target?.Examples; }
/// <inheritdoc/>
public IList<JsonNode> Enum { get => Target?.Enum; }
/// <inheritdoc/>
public bool UnevaluatedProperties { get => Target?.UnevaluatedProperties ?? false; }
/// <inheritdoc/>
public OpenApiExternalDocs ExternalDocs { get => Target?.ExternalDocs; }
/// <inheritdoc/>
public bool Deprecated { get => Target?.Deprecated ?? false; }
/// <inheritdoc/>
public OpenApiXml Xml { get => Target?.Xml; }
/// <inheritdoc/>
public IDictionary<string, IOpenApiExtension> Extensions { get => Target?.Extensions; }
/// <inheritdoc/>
public IDictionary<string, JsonNode> UnrecognizedKeywords { get => Target?.UnrecognizedKeywords; }
/// <inheritdoc/>
public IDictionary<string, object> Annotations { get => Target?.Annotations; }
/// <inheritdoc/>
public IDictionary<string, ISet<string>> DependentRequired { get => Target?.DependentRequired; }
/// <inheritdoc/>
public override void SerializeAsV31(IOpenApiWriter writer)
{
SerializeAsWithoutLoops(writer, (w, element) => (element is IOpenApiSchema s ? CopyReferenceAsTargetElementWithOverrides(s) : element).SerializeAsV3(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);
}
}
}