Skip to content

Commit c6f9ff2

Browse files
committed
Changed the way we determine if an element is a component
1 parent 483b35e commit c6f9ff2

File tree

1 file changed

+26
-111
lines changed

1 file changed

+26
-111
lines changed

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 26 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public class OpenApiWalker
1919
private readonly Stack<OpenApiSchema> _schemaLoop = new Stack<OpenApiSchema>();
2020
private readonly Stack<OpenApiPathItem> _pathItemLoop = new Stack<OpenApiPathItem>();
2121

22-
private bool _inComponents = false;
23-
2422
/// <summary>
2523
/// Initializes the <see cref="OpenApiWalker"/> class.
2624
/// </summary>
@@ -42,7 +40,6 @@ public void Walk(OpenApiDocument doc)
4240

4341
_schemaLoop.Clear();
4442
_pathItemLoop.Clear();
45-
_inComponents = false;
4643

4744
_visitor.Visit(doc);
4845

@@ -102,8 +99,6 @@ internal void Walk(OpenApiComponents components)
10299
return;
103100
}
104101

105-
EnterComponents();
106-
107102
_visitor.Visit(components);
108103

109104
if (components == null)
@@ -117,7 +112,7 @@ internal void Walk(OpenApiComponents components)
117112
{
118113
foreach (var item in components.Schemas)
119114
{
120-
Walk(item.Key, () => Walk(item.Value));
115+
Walk(item.Key, () => Walk(item.Value, true));
121116
}
122117
}
123118
});
@@ -128,7 +123,7 @@ internal void Walk(OpenApiComponents components)
128123
{
129124
foreach (var item in components.Callbacks)
130125
{
131-
Walk(item.Key, () => Walk(item.Value));
126+
Walk(item.Key, () => Walk(item.Value, true));
132127
}
133128
}
134129
});
@@ -139,7 +134,7 @@ internal void Walk(OpenApiComponents components)
139134
{
140135
foreach (var item in components.Parameters)
141136
{
142-
Walk(item.Key, () => Walk(item.Value));
137+
Walk(item.Key, () => Walk(item.Value, true));
143138
}
144139
}
145140
});
@@ -150,7 +145,7 @@ internal void Walk(OpenApiComponents components)
150145
{
151146
foreach (var item in components.Examples)
152147
{
153-
Walk(item.Key, () => Walk(item.Value));
148+
Walk(item.Key, () => Walk(item.Value, true));
154149
}
155150
}
156151
});
@@ -161,7 +156,7 @@ internal void Walk(OpenApiComponents components)
161156
{
162157
foreach (var item in components.Headers)
163158
{
164-
Walk(item.Key, () => Walk(item.Value));
159+
Walk(item.Key, () => Walk(item.Value, true));
165160
}
166161
}
167162
});
@@ -172,7 +167,7 @@ internal void Walk(OpenApiComponents components)
172167
{
173168
foreach (var item in components.Links)
174169
{
175-
Walk(item.Key, () => Walk(item.Value));
170+
Walk(item.Key, () => Walk(item.Value, true));
176171
}
177172
}
178173
});
@@ -183,7 +178,7 @@ internal void Walk(OpenApiComponents components)
183178
{
184179
foreach (var item in components.RequestBodies)
185180
{
186-
Walk(item.Key, () => Walk(item.Value));
181+
Walk(item.Key, () => Walk(item.Value, true));
187182
}
188183
}
189184
});
@@ -194,13 +189,12 @@ internal void Walk(OpenApiComponents components)
194189
{
195190
foreach (var item in components.Responses)
196191
{
197-
Walk(item.Key, () => Walk(item.Value));
192+
Walk(item.Key, () => Walk(item.Value, true));
198193
}
199194
}
200195
});
201196

202197
Walk(components as IOpenApiExtensible);
203-
ExitComponents();
204198
}
205199

206200
/// <summary>
@@ -332,20 +326,13 @@ internal void Walk(OpenApiContact contact)
332326
/// <summary>
333327
/// Visits <see cref="OpenApiCallback"/> and child objects
334328
/// </summary>
335-
internal void Walk(OpenApiCallback callback)
329+
internal void Walk(OpenApiCallback callback, bool isComponent = false)
336330
{
337-
if (callback == null)
331+
if (callback == null || IsReference(callback, isComponent))
338332
{
339333
return;
340334
}
341335

342-
var isAComponent = false; // Handle $refs within component
343-
if (_inComponents)
344-
{
345-
isAComponent = true;
346-
ExitComponents();
347-
}
348-
349336
_visitor.Visit(callback);
350337

351338
if (callback != null)
@@ -358,11 +345,6 @@ internal void Walk(OpenApiCallback callback)
358345
_visitor.CurrentKeys.Callback = null;
359346
}
360347
}
361-
362-
if (isAComponent)
363-
{
364-
EnterComponents();
365-
}
366348
}
367349

368350
/// <summary>
@@ -553,31 +535,19 @@ internal void Walk(IList<OpenApiParameter> parameters)
553535
/// <summary>
554536
/// Visits <see cref="OpenApiParameter"/> and child objects
555537
/// </summary>
556-
internal void Walk(OpenApiParameter parameter)
538+
internal void Walk(OpenApiParameter parameter, bool isComponent = false)
557539
{
558-
if (parameter == null || IsReference(parameter))
540+
if (parameter == null || IsReference(parameter, isComponent))
559541
{
560542
return;
561543
}
562544

563-
var isAComponent = false; // Handle $refs within component
564-
if (_inComponents)
565-
{
566-
isAComponent = true;
567-
ExitComponents();
568-
}
569-
570545
_visitor.Visit(parameter);
571546
Walk(OpenApiConstants.Schema, () => Walk(parameter.Schema));
572547
Walk(OpenApiConstants.Content, () => Walk(parameter.Content));
573548
Walk(OpenApiConstants.Examples, () => Walk(parameter.Examples));
574549

575550
Walk(parameter as IOpenApiExtensible);
576-
577-
if (isAComponent)
578-
{
579-
EnterComponents();
580-
}
581551
}
582552

583553
/// <summary>
@@ -607,47 +577,29 @@ internal void Walk(OpenApiResponses responses)
607577
/// <summary>
608578
/// Visits <see cref="OpenApiResponse"/> and child objects
609579
/// </summary>
610-
internal void Walk(OpenApiResponse response)
580+
internal void Walk(OpenApiResponse response, bool isComponent = false)
611581
{
612-
if (response == null || IsReference(response))
582+
if (response == null || IsReference(response, isComponent))
613583
{
614584
return;
615585
}
616586

617-
var isAComponent = false; // Handle $refs within component
618-
if (_inComponents)
619-
{
620-
isAComponent = true;
621-
ExitComponents();
622-
}
623-
624587
_visitor.Visit(response);
625588
Walk(OpenApiConstants.Content, () => Walk(response.Content));
626589
Walk(OpenApiConstants.Links, () => Walk(response.Links));
627590
Walk(OpenApiConstants.Headers, () => Walk(response.Headers));
628591
Walk(response as IOpenApiExtensible);
629-
630-
if (isAComponent)
631-
{
632-
EnterComponents();
633-
}
634592
}
635593

636594
/// <summary>
637595
/// Visits <see cref="OpenApiRequestBody"/> and child objects
638596
/// </summary>
639-
internal void Walk(OpenApiRequestBody requestBody)
597+
internal void Walk(OpenApiRequestBody requestBody, bool isComponent = false)
640598
{
641-
if (requestBody == null || IsReference(requestBody))
599+
if (requestBody == null || IsReference(requestBody, isComponent))
642600
{
643601
return;
644602
}
645-
var isAComponent = false; // Handle $refs within component
646-
if (_inComponents)
647-
{
648-
isAComponent = true;
649-
ExitComponents();
650-
}
651603

652604
_visitor.Visit(requestBody);
653605

@@ -659,11 +611,6 @@ internal void Walk(OpenApiRequestBody requestBody)
659611
}
660612
}
661613
Walk(requestBody as IOpenApiExtensible);
662-
663-
if (isAComponent)
664-
{
665-
EnterComponents();
666-
}
667614
}
668615

669616
/// <summary>
@@ -790,18 +737,12 @@ internal void Walk(OpenApiEncoding encoding)
790737
/// <summary>
791738
/// Visits <see cref="OpenApiSchema"/> and child objects
792739
/// </summary>
793-
internal void Walk(OpenApiSchema schema)
740+
internal void Walk(OpenApiSchema schema, bool isComponent = false)
794741
{
795-
if (schema == null || IsReference(schema))
742+
if (schema == null || IsReference(schema, isComponent))
796743
{
797744
return;
798745
}
799-
var isAComponent = false; // Handle $refs within component
800-
if (_inComponents)
801-
{
802-
isAComponent = true;
803-
ExitComponents();
804-
}
805746

806747
if (_schemaLoop.Contains(schema))
807748
{
@@ -842,11 +783,6 @@ internal void Walk(OpenApiSchema schema)
842783
Walk(schema as IOpenApiExtensible);
843784

844785
_schemaLoop.Pop();
845-
846-
if (isAComponent)
847-
{
848-
EnterComponents();
849-
}
850786
}
851787

852788
/// <summary>
@@ -888,9 +824,9 @@ internal void Walk(IOpenApiAny example)
888824
/// <summary>
889825
/// Visits <see cref="OpenApiExample"/> and child objects
890826
/// </summary>
891-
internal void Walk(OpenApiExample example)
827+
internal void Walk(OpenApiExample example, bool isComponent = false)
892828
{
893-
if (example == null || IsReference(example))
829+
if (example == null || IsReference(example, isComponent))
894830
{
895831
return;
896832
}
@@ -994,9 +930,9 @@ internal void Walk(IDictionary<string,OpenApiLink> links)
994930
/// <summary>
995931
/// Visits <see cref="OpenApiLink"/> and child objects
996932
/// </summary>
997-
internal void Walk(OpenApiLink link)
933+
internal void Walk(OpenApiLink link, bool isComponent = false)
998934
{
999-
if (link == null || IsReference(link))
935+
if (link == null || IsReference(link, isComponent))
1000936
{
1001937
return;
1002938
}
@@ -1009,30 +945,19 @@ internal void Walk(OpenApiLink link)
1009945
/// <summary>
1010946
/// Visits <see cref="OpenApiHeader"/> and child objects
1011947
/// </summary>
1012-
internal void Walk(OpenApiHeader header)
948+
internal void Walk(OpenApiHeader header, bool isComponent = false)
1013949
{
1014-
if (header == null || IsReference(header))
950+
if (header == null || IsReference(header, isComponent))
1015951
{
1016952
return;
1017953
}
1018-
var isAComponent = false; // Handle $refs within component
1019-
if (_inComponents)
1020-
{
1021-
isAComponent = true;
1022-
ExitComponents();
1023-
}
1024954

1025955
_visitor.Visit(header);
1026956
Walk(OpenApiConstants.Content, () => Walk(header.Content));
1027957
Walk(OpenApiConstants.Example, () => Walk(header.Example));
1028958
Walk(OpenApiConstants.Examples, () => Walk(header.Examples));
1029959
Walk(OpenApiConstants.Schema, () => Walk(header.Schema));
1030960
Walk(header as IOpenApiExtensible);
1031-
1032-
if (isAComponent)
1033-
{
1034-
EnterComponents();
1035-
}
1036961
}
1037962

1038963
/// <summary>
@@ -1131,25 +1056,15 @@ private void Walk(string context, Action walk)
11311056
/// <summary>
11321057
/// Identify if an element is just a reference to a component, or an actual component
11331058
/// </summary>
1134-
private bool IsReference(IOpenApiReferenceable referenceable)
1059+
private bool IsReference(IOpenApiReferenceable referenceable, bool isComponent = false)
11351060
{
1136-
var isReference = referenceable.Reference != null && !_inComponents;
1061+
var isReference = referenceable.Reference != null && !isComponent;
11371062
if (isReference)
11381063
{
11391064
Walk(referenceable);
11401065
}
11411066
return isReference;
11421067
}
1143-
1144-
private void EnterComponents()
1145-
{
1146-
_inComponents = true;
1147-
}
1148-
1149-
private void ExitComponents()
1150-
{
1151-
_inComponents = false;
1152-
}
11531068
}
11541069

11551070
/// <summary>

0 commit comments

Comments
 (0)