Skip to content

Commit ea80626

Browse files
committed
Minor simplifcations
1 parent 8cb7dcd commit ea80626

File tree

2 files changed

+23
-38
lines changed

2 files changed

+23
-38
lines changed

packages/zudoku/src/lib/plugins/openapi/util/buildTagCategories.test.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,32 +142,14 @@ describe("buildTagCategories", () => {
142142
expect(contacts?.type).toBe("category");
143143

144144
if (contacts?.type === "category") {
145-
// Operations from the "Contacts" tag come first, then "Notes" as a child
145+
// Operations from the "Contacts" tag come first, then "Notes" as a child.
146+
// "Contacts" must not appear as a nested child of itself.
146147
expect(contacts.items).toHaveLength(2);
147148
expect(contacts.items[0]?.label).toBe("Contacts op");
148149
expect(contacts.items[1]?.label).toBe("Notes");
149150
}
150151
});
151152

152-
it("excludes self-reference when tagGroup includes its own name in tags", () => {
153-
const tagCategories = new Map<string, NavigationItem>([
154-
["Contacts", makeTag("Contacts")],
155-
["Notes", makeTag("Notes")],
156-
]);
157-
158-
const result = buildTagCategories({
159-
tagCategories,
160-
tagGroups: [{ name: "Contacts", tags: ["Contacts", "Notes"] }],
161-
});
162-
163-
const contacts = result[0];
164-
if (contacts?.type === "category") {
165-
// "Contacts" tag should NOT appear as a nested child of itself
166-
const childLabels = contacts.items.map((i) => i.label);
167-
expect(childLabels).not.toContain("Contacts");
168-
}
169-
});
170-
171153
it("excludes merged tag from ungrouped results", () => {
172154
const tagCategories = new Map<string, NavigationItem>([
173155
["Contacts", makeTag("Contacts")],

packages/zudoku/src/lib/plugins/openapi/util/buildTagCategories.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,44 @@ type BuildTagCategoriesOptions = {
88
expandAllTags?: boolean;
99
};
1010

11+
// Merges tags and x-tagGroups with the same name into a single sidebar
12+
// category (mimics OpenAPI 3.2.0 "Enhanced tags" behaviour).
1113
export const buildTagCategories = ({
1214
tagCategories,
1315
tagGroups,
1416
expandAllTags,
1517
}: BuildTagCategoriesOptions): NavigationItem[] => {
1618
const consumedTags = new Set<string>();
1719

18-
const groupedCategories: NavigationItem[] = tagGroups.flatMap((group) => {
20+
const groupedCategories = tagGroups.flatMap<NavigationItem>((group) => {
21+
// Use a same-named tag as base so its operations appear first
1922
const matchingTag = tagCategories.get(group.name);
2023
const base = matchingTag?.type === "category" ? matchingTag : undefined;
2124

2225
if (base) consumedTags.add(group.name);
2326

24-
const childTags = group.tags
25-
.filter((name) => name !== group.name && tagCategories.has(name))
26-
.flatMap((name) => {
27-
consumedTags.add(name);
28-
const tag = tagCategories.get(name);
29-
return tag ? [tag] : [];
30-
});
27+
// Exclude group's own name to avoid nesting a tag inside itself
28+
const childTags = group.tags.flatMap((name) => {
29+
if (name === group.name) return [];
30+
const tag = tagCategories.get(name);
31+
if (!tag) return [];
32+
consumedTags.add(name);
33+
return tag;
34+
});
3135

3236
if (!base && childTags.length === 0) return [];
3337

34-
return [
35-
{
36-
...base,
37-
type: "category" as const,
38-
label: base?.label ?? group.name,
39-
items: [...(base?.items ?? []), ...childTags],
40-
collapsible: base?.collapsible ?? true,
41-
collapsed: base?.collapsed ?? !expandAllTags,
42-
},
43-
];
38+
return {
39+
...base,
40+
type: "category",
41+
label: base?.label ?? group.name,
42+
items: [...(base?.items ?? []), ...childTags],
43+
collapsible: base?.collapsible ?? true,
44+
collapsed: base?.collapsed ?? !expandAllTags,
45+
};
4446
});
4547

48+
// Tags not claimed by any group appear as standalone entries
4649
const ungroupedCategories = Array.from(tagCategories.entries())
4750
.filter(([name]) => !consumedTags.has(name))
4851
.map(([, cat]) => cat);

0 commit comments

Comments
 (0)