forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParsingContext.cs
More file actions
282 lines (244 loc) · 10.2 KB
/
ParsingContext.cs
File metadata and controls
282 lines (244 loc) · 10.2 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// 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;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader.ParseNodes;
using Microsoft.OpenApi.Reader.V2;
using Microsoft.OpenApi.Reader.V3;
using Microsoft.OpenApi.Reader.V31;
namespace Microsoft.OpenApi.Reader
{
/// <summary>
/// The Parsing Context holds temporary state needed whilst parsing an OpenAPI Document
/// </summary>
public class ParsingContext
{
private readonly Stack<string> _currentLocation = new();
private readonly Dictionary<string, object> _tempStorage = new();
private readonly Dictionary<object, Dictionary<string, object>> _scopedTempStorage = new();
private readonly Dictionary<string, Stack<string>> _loopStacks = new();
/// <summary>
/// Extension parsers
/// </summary>
public Dictionary<string, Func<JsonNode, OpenApiSpecVersion, IOpenApiExtension>> ExtensionParsers { get; set; } =
new();
internal RootNode RootNode { get; set; }
internal List<OpenApiTag> Tags { get; private set; } = new();
/// <summary>
/// The base url for the document
/// </summary>
public Uri BaseUrl { get; set; }
/// <summary>
/// Default content type for a response object
/// </summary>
public List<string> DefaultContentType { get; set; }
/// <summary>
/// Diagnostic object that returns metadata about the parsing process.
/// </summary>
public OpenApiDiagnostic Diagnostic { get; }
/// <summary>
/// Create Parsing Context
/// </summary>
/// <param name="diagnostic">Provide instance for diagnostic object for collecting and accessing information about the parsing.</param>
public ParsingContext(OpenApiDiagnostic diagnostic)
{
Diagnostic = diagnostic;
}
/// <summary>
/// Initiates the parsing process. Not thread safe and should only be called once on a parsing context
/// </summary>
/// <param name="jsonNode">Set of Json nodes to parse.</param>
/// <param name="location">Location of where the document that is getting loaded is saved</param>
/// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
public OpenApiDocument Parse(JsonNode jsonNode, Uri location)
{
RootNode = new RootNode(this, jsonNode);
var inputVersion = GetVersion(RootNode);
OpenApiDocument doc;
switch (inputVersion)
{
case string version when version.is2_0():
VersionService = new OpenApiV2VersionService(Diagnostic);
doc = VersionService.LoadDocument(RootNode, location);
this.Diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
ValidateRequiredFields(doc, version);
break;
case string version when version.is3_0():
VersionService = new OpenApiV3VersionService(Diagnostic);
doc = VersionService.LoadDocument(RootNode, location);
this.Diagnostic.SpecificationVersion = version.is3_1() ? OpenApiSpecVersion.OpenApi3_1 : OpenApiSpecVersion.OpenApi3_0;
ValidateRequiredFields(doc, version);
break;
case string version when version.is3_1():
VersionService = new OpenApiV31VersionService(Diagnostic);
doc = VersionService.LoadDocument(RootNode, location);
this.Diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_1;
ValidateRequiredFields(doc, version);
break;
default:
throw new OpenApiUnsupportedSpecVersionException(inputVersion);
}
return doc;
}
/// <summary>
/// Initiates the parsing process of a fragment. Not thread safe and should only be called once on a parsing context
/// </summary>
/// <param name="jsonNode"></param>
/// <param name="version">OpenAPI version of the fragment</param>
/// <param name="openApiDocument">The OpenApiDocument object to which the fragment belongs, used to lookup references.</param>
/// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
public T ParseFragment<T>(JsonNode jsonNode, OpenApiSpecVersion version, OpenApiDocument openApiDocument) where T : IOpenApiElement
{
var node = ParseNode.Create(this, jsonNode);
var element = default(T);
switch (version)
{
case OpenApiSpecVersion.OpenApi2_0:
VersionService = new OpenApiV2VersionService(Diagnostic);
element = this.VersionService.LoadElement<T>(node, openApiDocument);
break;
case OpenApiSpecVersion.OpenApi3_0:
this.VersionService = new OpenApiV3VersionService(Diagnostic);
element = this.VersionService.LoadElement<T>(node, openApiDocument);
break;
case OpenApiSpecVersion.OpenApi3_1:
this.VersionService = new OpenApiV31VersionService(Diagnostic);
element = this.VersionService.LoadElement<T>(node, openApiDocument);
break;
}
return element;
}
/// <summary>
/// Gets the version of the Open API document.
/// </summary>
private static string GetVersion(RootNode rootNode)
{
var versionNode = rootNode.Find(new("/openapi"));
if (versionNode != null)
{
return versionNode.GetScalarValue().Replace("\"", string.Empty);
}
versionNode = rootNode.Find(new("/swagger"));
return versionNode?.GetScalarValue().Replace("\"", string.Empty);
}
/// <summary>
/// Service providing all Version specific conversion functions
/// </summary>
internal IOpenApiVersionService VersionService { get; set; }
/// <summary>
/// End the current object.
/// </summary>
public void EndObject()
{
_currentLocation.Pop();
}
/// <summary>
/// Get the current location as string representing JSON pointer.
/// </summary>
public string GetLocation()
{
return "#/" + string.Join("/", _currentLocation.Reverse().Select(s => s.Replace("~", "~0").Replace("/", "~1")).ToArray());
}
/// <summary>
/// Gets the value from the temporary storage matching the given key.
/// </summary>
public T GetFromTempStorage<T>(string key, object scope = null)
{
Dictionary<string, object> storage;
if (scope == null)
{
storage = _tempStorage;
}
else if (!_scopedTempStorage.TryGetValue(scope, out storage))
{
return default;
}
return storage.TryGetValue(key, out var value) ? (T)value : default;
}
/// <summary>
/// Sets the temporary storage for this key and value.
/// </summary>
public void SetTempStorage(string key, object value, object scope = null)
{
Dictionary<string, object> storage;
if (scope == null)
{
storage = _tempStorage;
}
else if (!_scopedTempStorage.TryGetValue(scope, out storage))
{
storage = _scopedTempStorage[scope] = new();
}
if (value == null)
{
storage.Remove(key);
}
else
{
storage[key] = value;
}
}
/// <summary>
/// Starts an object with the given object name.
/// </summary>
public void StartObject(string objectName)
{
_currentLocation.Push(objectName);
}
/// <summary>
/// Maintain history of traversals to avoid stack overflows from cycles
/// </summary>
/// <param name="loopId">Any unique identifier for a stack.</param>
/// <param name="key">Identifier used for current context.</param>
/// <returns>If method returns false a loop was detected and the key is not added.</returns>
public bool PushLoop(string loopId, string key)
{
if (!_loopStacks.TryGetValue(loopId, out var stack))
{
stack = new();
_loopStacks.Add(loopId, stack);
}
if (!stack.Contains(key))
{
stack.Push(key);
return true;
}
else
{
return false; // Loop detected
}
}
/// <summary>
/// Reset loop tracking stack
/// </summary>
/// <param name="loopid">Identifier of loop to clear</param>
internal void ClearLoop(string loopid)
{
_loopStacks[loopid].Clear();
}
/// <summary>
/// Exit from the context in cycle detection
/// </summary>
/// <param name="loopid">Identifier of loop</param>
public void PopLoop(string loopid)
{
if (_loopStacks[loopid].Count > 0)
{
_loopStacks[loopid].Pop();
}
}
private void ValidateRequiredFields(OpenApiDocument doc, string version)
{
if ((version.is2_0() || version.is3_0()) && (doc.Paths == null))
{
// paths is a required field in OpenAPI 2.0 and 3.0 but optional in 3.1
RootNode.Context.Diagnostic.Errors.Add(new OpenApiError("", $"Paths is a REQUIRED field at {RootNode.Context.GetLocation()}"));
}
}
}
}