-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathParsingContext.cs
More file actions
263 lines (228 loc) · 9.58 KB
/
ParsingContext.cs
File metadata and controls
263 lines (228 loc) · 9.58 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.Exceptions;
using Microsoft.OpenApi.Readers.Interface;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V2;
using Microsoft.OpenApi.Readers.V3;
using SharpYaml.Serialization;
namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// The Parsing Context holds temporary state needed whilst parsing an OpenAPI Document
/// </summary>
public class ParsingContext
{
private readonly Stack<string> _currentLocation = new Stack<string>();
private readonly Dictionary<string, object> _tempStorage = new Dictionary<string, object>();
private readonly Dictionary<object, Dictionary<string, object>> _scopedTempStorage = new Dictionary<object, Dictionary<string, object>>();
private readonly Dictionary<string, Stack<string>> _loopStacks = new Dictionary<string, Stack<string>>();
internal Dictionary<string, Func<IOpenApiAny, OpenApiSpecVersion, IOpenApiExtension>> ExtensionParsers { get; set; } = new Dictionary<string, Func<IOpenApiAny, OpenApiSpecVersion, IOpenApiExtension>>();
internal RootNode RootNode { get; set; }
internal List<OpenApiTag> Tags { get; private set; } = new List<OpenApiTag>();
internal Uri BaseUrl { 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 diagnotic 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="yamlDocument">Yaml document to parse.</param>
/// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
internal OpenApiDocument Parse(YamlDocument yamlDocument)
{
RootNode = new RootNode(this, yamlDocument);
var inputVersion = GetVersion(RootNode);
OpenApiDocument doc;
switch (inputVersion)
{
case string version when version.is2_0():
VersionService = new OpenApiV2VersionService(Diagnostic);
doc = VersionService.LoadDocument(RootNode);
this.Diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
ValidateRequiredFields(doc, version);
break;
case string version when version.is3_0() || version.is3_1():
VersionService = new OpenApiV3VersionService(Diagnostic);
doc = VersionService.LoadDocument(RootNode);
this.Diagnostic.SpecificationVersion = version.is3_1() ? OpenApiSpecVersion.OpenApi3_1 : OpenApiSpecVersion.OpenApi3_0;
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="yamlDocument"></param>
/// <param name="version">OpenAPI version of the fragment</param>
/// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
internal T ParseFragment<T>(YamlDocument yamlDocument, OpenApiSpecVersion version) where T : IOpenApiElement
{
var node = ParseNode.Create(this, yamlDocument.RootNode);
T element = default(T);
switch (version)
{
case OpenApiSpecVersion.OpenApi2_0:
VersionService = new OpenApiV2VersionService(Diagnostic);
element = this.VersionService.LoadElement<T>(node);
break;
case OpenApiSpecVersion.OpenApi3_0:
this.VersionService = new OpenApiV3VersionService(Diagnostic);
element = this.VersionService.LoadElement<T>(node);
break;
}
return element;
}
/// <summary>
/// Gets the version of the Open API document.
/// </summary>
private static string GetVersion(RootNode rootNode)
{
var versionNode = rootNode.Find(new JsonPointer("/openapi"));
if (versionNode != null)
{
return versionNode.GetScalarValue();
}
versionNode = rootNode.Find(new JsonPointer("/swagger"));
return versionNode?.GetScalarValue();
}
/// <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(T);
}
return storage.TryGetValue(key, out var value) ? (T)value : default(T);
}
/// <summary>
/// Sets the temporary storge 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 Dictionary<string, object>();
}
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)
{
Stack<string> stack;
if (!_loopStacks.TryGetValue(loopId, out stack))
{
stack = new Stack<string>();
_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 || !doc.Paths.Any()))
{
// paths is a required field in OpenAPI 3.0 but optional in 3.1
RootNode.Context.Diagnostic.Errors.Add(new OpenApiError("", $"Paths is a REQUIRED field at {RootNode.Context.GetLocation()}"));
}
else if (version.is3_1() && (doc.Paths == null || !doc.Paths.Any()) && (doc.Webhooks == null || !doc.Webhooks.Any()))
{
RootNode.Context.Diagnostic.Errors.Add(new OpenApiError(
"", $"The document MUST contain either a Paths or Webhooks field at {RootNode.Context.GetLocation()}"));
}
}
}
}