Skip to content

Commit 70f4c87

Browse files
Refactor tag methods
Create common implementation to walk `OpenApiTag` and `OpenApiTagReference`.
1 parent 41c53c9 commit 70f4c87

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,7 @@ internal void Walk(ISet<OpenApiTag>? tags)
9494

9595
_visitor.Visit(tags);
9696

97-
// Visit tags
98-
if (tags is HashSet<OpenApiTag> { Count: 1 } hashSet && hashSet.First() is { } only)
99-
{
100-
WalkItem("0", only, Walk);
101-
}
102-
else
103-
{
104-
int index = 0;
105-
foreach (var tag in tags)
106-
{
107-
if (tag is null)
108-
{
109-
continue;
110-
}
111-
112-
var context = index.ToString();
113-
WalkItem(context, tag, Walk);
114-
index++;
115-
}
116-
}
117-
118-
static void Walk(OpenApiWalker self, OpenApiTag tag) => self.Walk(tag);
97+
WalkTags(tags, static (self, tag) => self.Walk(tag));
11998
}
12099

121100
/// <summary>
@@ -131,27 +110,7 @@ internal void Walk(ISet<OpenApiTagReference>? tags)
131110
_visitor.Visit(tags);
132111

133112
// Visit tags
134-
if (tags is HashSet<OpenApiTagReference> { Count: 1 } hashSet && hashSet.First() is { } only)
135-
{
136-
WalkItem("0", only, Walk);
137-
}
138-
else
139-
{
140-
int index = 0;
141-
foreach (var tag in tags)
142-
{
143-
if (tag is null)
144-
{
145-
continue;
146-
}
147-
148-
var context = index.ToString();
149-
WalkItem(context, tag, Walk);
150-
index++;
151-
}
152-
}
153-
154-
static void Walk(OpenApiWalker self, OpenApiTagReference tag) => self.Walk(tag);
113+
WalkTags(tags, static (self, tag) => self.Walk(tag));
155114
}
156115

157116
/// <summary>
@@ -1394,6 +1353,31 @@ private bool ProcessAsReference(IOpenApiReferenceHolder referenceableHolder, boo
13941353
}
13951354
return isReference;
13961355
}
1356+
1357+
private void WalkTags<T>(ISet<T> tags, Action<OpenApiWalker, T> walk)
1358+
where T : IOpenApiTag
1359+
{
1360+
// Visit tags
1361+
if (tags is HashSet<T> { Count: 1 } hashSet && hashSet.First() is { } only)
1362+
{
1363+
WalkItem("0", only, walk);
1364+
}
1365+
else
1366+
{
1367+
int index = 0;
1368+
foreach (var tag in tags)
1369+
{
1370+
if (tag is null)
1371+
{
1372+
continue;
1373+
}
1374+
1375+
var context = index.ToString();
1376+
WalkItem(context, tag, walk);
1377+
index++;
1378+
}
1379+
}
1380+
}
13971381
}
13981382

13991383
/// <summary>

0 commit comments

Comments
 (0)