@@ -19,8 +19,6 @@ public class OpenApiWalker
19
19
private readonly Stack < OpenApiSchema > _schemaLoop = new Stack < OpenApiSchema > ( ) ;
20
20
private readonly Stack < OpenApiPathItem > _pathItemLoop = new Stack < OpenApiPathItem > ( ) ;
21
21
22
- private bool _inComponents = false ;
23
-
24
22
/// <summary>
25
23
/// Initializes the <see cref="OpenApiWalker"/> class.
26
24
/// </summary>
@@ -42,7 +40,6 @@ public void Walk(OpenApiDocument doc)
42
40
43
41
_schemaLoop . Clear ( ) ;
44
42
_pathItemLoop . Clear ( ) ;
45
- _inComponents = false ;
46
43
47
44
_visitor . Visit ( doc ) ;
48
45
@@ -102,8 +99,6 @@ internal void Walk(OpenApiComponents components)
102
99
return ;
103
100
}
104
101
105
- EnterComponents ( ) ;
106
-
107
102
_visitor . Visit ( components ) ;
108
103
109
104
if ( components == null )
@@ -117,7 +112,7 @@ internal void Walk(OpenApiComponents components)
117
112
{
118
113
foreach ( var item in components . Schemas )
119
114
{
120
- Walk ( item . Key , ( ) => Walk ( item . Value ) ) ;
115
+ Walk ( item . Key , ( ) => Walk ( item . Value , isComponent : true ) ) ;
121
116
}
122
117
}
123
118
} ) ;
@@ -128,7 +123,7 @@ internal void Walk(OpenApiComponents components)
128
123
{
129
124
foreach ( var item in components . Callbacks )
130
125
{
131
- Walk ( item . Key , ( ) => Walk ( item . Value ) ) ;
126
+ Walk ( item . Key , ( ) => Walk ( item . Value , isComponent : true ) ) ;
132
127
}
133
128
}
134
129
} ) ;
@@ -139,7 +134,7 @@ internal void Walk(OpenApiComponents components)
139
134
{
140
135
foreach ( var item in components . Parameters )
141
136
{
142
- Walk ( item . Key , ( ) => Walk ( item . Value ) ) ;
137
+ Walk ( item . Key , ( ) => Walk ( item . Value , isComponent : true ) ) ;
143
138
}
144
139
}
145
140
} ) ;
@@ -150,7 +145,7 @@ internal void Walk(OpenApiComponents components)
150
145
{
151
146
foreach ( var item in components . Examples )
152
147
{
153
- Walk ( item . Key , ( ) => Walk ( item . Value ) ) ;
148
+ Walk ( item . Key , ( ) => Walk ( item . Value , isComponent : true ) ) ;
154
149
}
155
150
}
156
151
} ) ;
@@ -161,7 +156,7 @@ internal void Walk(OpenApiComponents components)
161
156
{
162
157
foreach ( var item in components . Headers )
163
158
{
164
- Walk ( item . Key , ( ) => Walk ( item . Value ) ) ;
159
+ Walk ( item . Key , ( ) => Walk ( item . Value , isComponent : true ) ) ;
165
160
}
166
161
}
167
162
} ) ;
@@ -172,7 +167,7 @@ internal void Walk(OpenApiComponents components)
172
167
{
173
168
foreach ( var item in components . Links )
174
169
{
175
- Walk ( item . Key , ( ) => Walk ( item . Value ) ) ;
170
+ Walk ( item . Key , ( ) => Walk ( item . Value , isComponent : true ) ) ;
176
171
}
177
172
}
178
173
} ) ;
@@ -183,7 +178,7 @@ internal void Walk(OpenApiComponents components)
183
178
{
184
179
foreach ( var item in components . RequestBodies )
185
180
{
186
- Walk ( item . Key , ( ) => Walk ( item . Value ) ) ;
181
+ Walk ( item . Key , ( ) => Walk ( item . Value , isComponent : true ) ) ;
187
182
}
188
183
}
189
184
} ) ;
@@ -194,13 +189,12 @@ internal void Walk(OpenApiComponents components)
194
189
{
195
190
foreach ( var item in components . Responses )
196
191
{
197
- Walk ( item . Key , ( ) => Walk ( item . Value ) ) ;
192
+ Walk ( item . Key , ( ) => Walk ( item . Value , isComponent : true ) ) ;
198
193
}
199
194
}
200
195
} ) ;
201
196
202
197
Walk ( components as IOpenApiExtensible ) ;
203
- ExitComponents ( ) ;
204
198
}
205
199
206
200
/// <summary>
@@ -332,9 +326,9 @@ internal void Walk(OpenApiContact contact)
332
326
/// <summary>
333
327
/// Visits <see cref="OpenApiCallback"/> and child objects
334
328
/// </summary>
335
- internal void Walk ( OpenApiCallback callback )
329
+ internal void Walk ( OpenApiCallback callback , bool isComponent = false )
336
330
{
337
- if ( callback == null )
331
+ if ( callback == null || ProcessAsReference ( callback , isComponent ) )
338
332
{
339
333
return ;
340
334
}
@@ -358,7 +352,7 @@ internal void Walk(OpenApiCallback callback)
358
352
/// </summary>
359
353
internal void Walk ( OpenApiTag tag )
360
354
{
361
- if ( tag == null || IsReference ( tag ) )
355
+ if ( tag == null || ProcessAsReference ( tag ) )
362
356
{
363
357
return ;
364
358
}
@@ -541,9 +535,9 @@ internal void Walk(IList<OpenApiParameter> parameters)
541
535
/// <summary>
542
536
/// Visits <see cref="OpenApiParameter"/> and child objects
543
537
/// </summary>
544
- internal void Walk ( OpenApiParameter parameter )
538
+ internal void Walk ( OpenApiParameter parameter , bool isComponent = false )
545
539
{
546
- if ( parameter == null || IsReference ( parameter ) )
540
+ if ( parameter == null || ProcessAsReference ( parameter , isComponent ) )
547
541
{
548
542
return ;
549
543
}
@@ -583,9 +577,9 @@ internal void Walk(OpenApiResponses responses)
583
577
/// <summary>
584
578
/// Visits <see cref="OpenApiResponse"/> and child objects
585
579
/// </summary>
586
- internal void Walk ( OpenApiResponse response )
580
+ internal void Walk ( OpenApiResponse response , bool isComponent = false )
587
581
{
588
- if ( response == null || IsReference ( response ) )
582
+ if ( response == null || ProcessAsReference ( response , isComponent ) )
589
583
{
590
584
return ;
591
585
}
@@ -594,16 +588,15 @@ internal void Walk(OpenApiResponse response)
594
588
Walk ( OpenApiConstants . Content , ( ) => Walk ( response . Content ) ) ;
595
589
Walk ( OpenApiConstants . Links , ( ) => Walk ( response . Links ) ) ;
596
590
Walk ( OpenApiConstants . Headers , ( ) => Walk ( response . Headers ) ) ;
597
-
598
591
Walk ( response as IOpenApiExtensible ) ;
599
592
}
600
593
601
594
/// <summary>
602
595
/// Visits <see cref="OpenApiRequestBody"/> and child objects
603
596
/// </summary>
604
- internal void Walk ( OpenApiRequestBody requestBody )
597
+ internal void Walk ( OpenApiRequestBody requestBody , bool isComponent = false )
605
598
{
606
- if ( requestBody == null || IsReference ( requestBody ) )
599
+ if ( requestBody == null || ProcessAsReference ( requestBody , isComponent ) )
607
600
{
608
601
return ;
609
602
}
@@ -744,9 +737,9 @@ internal void Walk(OpenApiEncoding encoding)
744
737
/// <summary>
745
738
/// Visits <see cref="OpenApiSchema"/> and child objects
746
739
/// </summary>
747
- internal void Walk ( OpenApiSchema schema )
740
+ internal void Walk ( OpenApiSchema schema , bool isComponent = false )
748
741
{
749
- if ( schema == null || IsReference ( schema ) )
742
+ if ( schema == null || ProcessAsReference ( schema , isComponent ) )
750
743
{
751
744
return ;
752
745
}
@@ -831,9 +824,9 @@ internal void Walk(IOpenApiAny example)
831
824
/// <summary>
832
825
/// Visits <see cref="OpenApiExample"/> and child objects
833
826
/// </summary>
834
- internal void Walk ( OpenApiExample example )
827
+ internal void Walk ( OpenApiExample example , bool isComponent = false )
835
828
{
836
- if ( example == null || IsReference ( example ) )
829
+ if ( example == null || ProcessAsReference ( example , isComponent ) )
837
830
{
838
831
return ;
839
832
}
@@ -937,9 +930,9 @@ internal void Walk(IDictionary<string,OpenApiLink> links)
937
930
/// <summary>
938
931
/// Visits <see cref="OpenApiLink"/> and child objects
939
932
/// </summary>
940
- internal void Walk ( OpenApiLink link )
933
+ internal void Walk ( OpenApiLink link , bool isComponent = false )
941
934
{
942
- if ( link == null || IsReference ( link ) )
935
+ if ( link == null || ProcessAsReference ( link , isComponent ) )
943
936
{
944
937
return ;
945
938
}
@@ -952,9 +945,9 @@ internal void Walk(OpenApiLink link)
952
945
/// <summary>
953
946
/// Visits <see cref="OpenApiHeader"/> and child objects
954
947
/// </summary>
955
- internal void Walk ( OpenApiHeader header )
948
+ internal void Walk ( OpenApiHeader header , bool isComponent = false )
956
949
{
957
- if ( header == null || IsReference ( header ) )
950
+ if ( header == null || ProcessAsReference ( header , isComponent ) )
958
951
{
959
952
return ;
960
953
}
@@ -986,7 +979,7 @@ internal void Walk(OpenApiSecurityRequirement securityRequirement)
986
979
/// </summary>
987
980
internal void Walk ( OpenApiSecurityScheme securityScheme )
988
981
{
989
- if ( securityScheme == null || IsReference ( securityScheme ) )
982
+ if ( securityScheme == null || ProcessAsReference ( securityScheme ) )
990
983
{
991
984
return ;
992
985
}
@@ -995,6 +988,14 @@ internal void Walk(OpenApiSecurityScheme securityScheme)
995
988
Walk ( securityScheme as IOpenApiExtensible ) ;
996
989
}
997
990
991
+ /// <summary>
992
+ /// Visits <see cref="OpenApiSecurityScheme"/> and child objects
993
+ /// </summary>
994
+ internal void Walk ( IOpenApiReferenceable referenceable )
995
+ {
996
+ _visitor . Visit ( referenceable ) ;
997
+ }
998
+
998
999
/// <summary>
999
1000
/// Dispatcher method that enables using a single method to walk the model
1000
1001
/// starting from any <see cref="IOpenApiElement"/>
@@ -1055,19 +1056,14 @@ private void Walk(string context, Action walk)
1055
1056
/// <summary>
1056
1057
/// Identify if an element is just a reference to a component, or an actual component
1057
1058
/// </summary>
1058
- private bool IsReference ( IOpenApiReferenceable referenceable )
1059
- {
1060
- return referenceable . Reference != null && ! _inComponents ;
1061
- }
1062
-
1063
- private void EnterComponents ( )
1064
- {
1065
- _inComponents = true ;
1066
- }
1067
-
1068
- private void ExitComponents ( )
1059
+ private bool ProcessAsReference ( IOpenApiReferenceable referenceable , bool isComponent = false )
1069
1060
{
1070
- _inComponents = false ;
1061
+ var isReference = referenceable . Reference != null && ! isComponent ;
1062
+ if ( isReference )
1063
+ {
1064
+ Walk ( referenceable ) ;
1065
+ }
1066
+ return isReference ;
1071
1067
}
1072
1068
}
1073
1069
0 commit comments