Skip to content

Commit 84dc3dc

Browse files
committed
Added context keys to visitor base
1 parent 1335035 commit 84dc3dc

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

src/Microsoft.OpenApi/Services/OpenApiVisitorBase.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@ namespace Microsoft.OpenApi.Services
1515
public abstract class OpenApiVisitorBase
1616
{
1717
private readonly Stack<string> _path = new Stack<string>();
18-
18+
private CurrentKeys _currentKeys = new CurrentKeys();
19+
20+
21+
22+
/// <summary>
23+
/// Properties available to identify context of where an object is within OpenAPI Document
24+
/// </summary>
25+
public CurrentKeys CurrentKeys
26+
{
27+
get
28+
{
29+
return _currentKeys;
30+
}
31+
}
32+
1933
/// <summary>
2034
/// Allow Rule to indicate validation error occured at a deeper context level.
2135
/// </summary>
@@ -44,6 +58,15 @@ public string PathString
4458
}
4559
}
4660

61+
internal void AttachCurrentKeys(CurrentKeys currentKeys)
62+
{
63+
64+
}
65+
66+
internal void ClearCurrentKeys()
67+
{
68+
69+
}
4770

4871

4972
/// <summary>

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class OpenApiWalker
1818
private readonly OpenApiVisitorBase _visitor;
1919
private readonly Stack<OpenApiSchema> _schemaLoop = new Stack<OpenApiSchema>();
2020
private readonly Stack<OpenApiPathItem> _pathItemLoop = new Stack<OpenApiPathItem>();
21-
private Dictionary<string, string> _currentKeys = new Dictionary<string, string>();
2221

2322
private bool _inComponents = false;
2423

@@ -55,6 +54,7 @@ public void Walk(OpenApiDocument doc)
5554
Walk(OpenApiConstants.ExternalDocs, () => Walk(doc.ExternalDocs));
5655
Walk(OpenApiConstants.Tags, () => Walk(doc.Tags));
5756
Walk(doc as IOpenApiExtensible);
57+
5858
}
5959

6060
/// <summary>
@@ -77,7 +77,6 @@ internal void Walk(IList<OpenApiTag> tags)
7777
Walk(i.ToString(), () => Walk(tags[i]));
7878
}
7979
}
80-
8180
}
8281

8382
/// <summary>
@@ -221,8 +220,10 @@ internal void Walk(OpenApiPaths paths)
221220
{
222221
foreach (var pathItem in paths)
223222
{
223+
_visitor.CurrentKeys.Path = pathItem.Key;
224224
Walk(pathItem.Key, () => Walk(pathItem.Value));// JSON Pointer uses ~1 as an escape character for /
225225
}
226+
_visitor.CurrentKeys.Path = null;
226227
}
227228
}
228229

@@ -282,8 +283,10 @@ internal void Walk(IOpenApiExtensible openApiExtensible)
282283
{
283284
foreach (var item in openApiExtensible.Extensions)
284285
{
286+
_visitor.CurrentKeys.Extension = item.Key;
285287
Walk(item.Key, () => Walk(item.Value));
286288
}
289+
_visitor.CurrentKeys.Extension = null;
287290
}
288291
}
289292

@@ -342,9 +345,11 @@ internal void Walk(OpenApiCallback callback)
342345
{
343346
foreach (var item in callback.PathItems)
344347
{
348+
_visitor.CurrentKeys.Callback = item.Key.ToString();
345349
var pathItem = item.Value;
346350
Walk(item.Key.ToString(), () => Walk(pathItem));
347351
}
352+
_visitor.CurrentKeys.Callback = null;
348353
}
349354
}
350355

@@ -394,8 +399,10 @@ internal void Walk(IDictionary<string,OpenApiServerVariable> serverVariables)
394399
{
395400
foreach (var variable in serverVariables)
396401
{
402+
_visitor.CurrentKeys.ServerVariable = variable.Key;
397403
Walk(variable.Key, () => Walk(variable.Value));
398404
}
405+
_visitor.CurrentKeys.ServerVariable = null;
399406
}
400407
}
401408

@@ -459,8 +466,10 @@ internal void Walk(IDictionary<OperationType, OpenApiOperation> operations)
459466
{
460467
foreach (var operation in operations)
461468
{
469+
_visitor.CurrentKeys.Operation = operation.Key;
462470
Walk(operation.Key.GetDisplayName(), () => Walk(operation.Value));
463471
}
472+
_visitor.CurrentKeys.Operation = null;
464473
}
465474
}
466475

@@ -563,8 +572,10 @@ internal void Walk(OpenApiResponses responses)
563572
{
564573
foreach (var response in responses)
565574
{
575+
_visitor.CurrentKeys.Response = response.Key;
566576
Walk(response.Key, () => Walk(response.Value));
567577
}
578+
_visitor.CurrentKeys.Response = null;
568579
}
569580
Walk(responses as IOpenApiExtensible);
570581
}
@@ -624,8 +635,10 @@ internal void Walk(IDictionary<string, OpenApiHeader> headers)
624635
{
625636
foreach (var header in headers)
626637
{
638+
_visitor.CurrentKeys.Header = header.Key;
627639
Walk(header.Key, () => Walk(header.Value));
628640
}
641+
_visitor.CurrentKeys.Header = null;
629642
}
630643
}
631644

@@ -642,10 +655,12 @@ internal void Walk(IDictionary<string, OpenApiCallback> callbacks)
642655
_visitor.Visit(callbacks);
643656
if (callbacks != null)
644657
{
645-
foreach (var header in callbacks)
658+
foreach (var callback in callbacks)
646659
{
647-
Walk(header.Key, () => Walk(header.Value));
660+
_visitor.CurrentKeys.Callback = callback.Key;
661+
Walk(callback.Key, () => Walk(callback.Value));
648662
}
663+
_visitor.CurrentKeys.Callback = null;
649664
}
650665
}
651666

@@ -664,8 +679,10 @@ internal void Walk(IDictionary<string, OpenApiMediaType> content)
664679
{
665680
foreach (var mediaType in content)
666681
{
682+
_visitor.CurrentKeys.Content = mediaType.Key;
667683
Walk(mediaType.Key, () => Walk(mediaType.Value));
668684
}
685+
_visitor.CurrentKeys.Content = null;
669686
}
670687
}
671688

@@ -703,8 +720,10 @@ internal void Walk(IDictionary<string, OpenApiEncoding> encodings)
703720
{
704721
foreach (var item in encodings)
705722
{
723+
_visitor.CurrentKeys.Encoding = item.Key;
706724
Walk(item.Key, () => Walk(item.Value));
707725
}
726+
_visitor.CurrentKeys.Encoding = null;
708727
}
709728
}
710729

@@ -789,6 +808,7 @@ internal void Walk(IDictionary<string,OpenApiExample> examples)
789808
{
790809
foreach (var example in examples)
791810
{
811+
_visitor.CurrentKeys.Example = example.Key;
792812
Walk(example.Key, () => Walk(example.Value));
793813
}
794814
}
@@ -906,8 +926,10 @@ internal void Walk(IDictionary<string,OpenApiLink> links)
906926
{
907927
foreach (var item in links)
908928
{
929+
_visitor.CurrentKeys.Link = item.Key;
909930
Walk(item.Key, () => Walk(item.Value));
910931
}
932+
_visitor.CurrentKeys.Link = null;
911933
}
912934
}
913935

@@ -1047,4 +1069,19 @@ private void ExitComponents()
10471069
_inComponents = false;
10481070
}
10491071
}
1072+
1073+
public class CurrentKeys
1074+
{
1075+
public string Path { get; set; }
1076+
public OperationType? Operation { get; set; }
1077+
public string Response { get; set; }
1078+
public string Content { get; set; }
1079+
public string Callback { get; set; }
1080+
public string Link { get; set; }
1081+
public string Header { get; internal set; }
1082+
public string Encoding { get; internal set; }
1083+
public string Example { get; internal set; }
1084+
public string Extension { get; internal set; }
1085+
public string ServerVariable { get; internal set; }
1086+
}
10501087
}

test/Microsoft.OpenApi.Tests/Walkers/WalkerLocationTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public void LocatePathOperationContentSchema()
107107
"#/paths/~1test/get/responses/200/content/application~1json/schema",
108108

109109
});
110+
111+
locator.Keys.ShouldAllBeEquivalentTo(new List<string> { "/test","Get","200", "application/json" });
110112
}
111113

112114
[Fact]
@@ -153,6 +155,8 @@ public void WalkDOMWithCycles()
153155
internal class LocatorVisitor : OpenApiVisitorBase
154156
{
155157
public List<string> Locations = new List<string>();
158+
public List<string> Keys = new List<string>();
159+
156160
public override void Visit(OpenApiInfo info)
157161
{
158162
Locations.Add(this.PathString);
@@ -175,6 +179,7 @@ public override void Visit(OpenApiPaths paths)
175179

176180
public override void Visit(OpenApiPathItem pathItem)
177181
{
182+
Keys.Add(CurrentKeys.Path);
178183
Locations.Add(this.PathString);
179184
}
180185

@@ -185,10 +190,12 @@ public override void Visit(OpenApiResponses responses)
185190

186191
public override void Visit(OpenApiOperation operation)
187192
{
193+
Keys.Add(CurrentKeys.Operation.ToString());
188194
Locations.Add(this.PathString);
189195
}
190196
public override void Visit(OpenApiResponse response)
191197
{
198+
Keys.Add(CurrentKeys.Response);
192199
Locations.Add(this.PathString);
193200
}
194201

@@ -199,6 +206,7 @@ public override void Visit(IDictionary<string,OpenApiMediaType> content)
199206

200207
public override void Visit(OpenApiMediaType mediaType)
201208
{
209+
Keys.Add(CurrentKeys.Content);
202210
Locations.Add(this.PathString);
203211
}
204212

0 commit comments

Comments
 (0)