Skip to content

Commit 1122d94

Browse files
authored
[V3] Fix tagGroup display when showSchemas is configured (#852)
* Rearrange Schemas category from each tagGroup category This commit takes the first tagGroup category's Schema category, adds it to the very end of the sidebarSlice, and deletes the Schema category from all the (now sibling) tagGroup categories. Closes #836 * add a schema object to the Restaurant specfile * show schemas in the restaurant demo API * add documentation to sidebars.md
1 parent 0a8aafc commit 1122d94

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

demo/docs/sidebars.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ The OpenAPI docs plugin provides out-of-the-box support for grouping paths by "t
5757
What this means is that the plugin will automatically generate a sidebar slice using the first path group as the "group by" value and the path itself as one of the `tags` under that category.
5858
Use `x-tagGroups` to group tags in the [Reference](https://redocly.com/docs/api-reference-docs/specification-extensions/x-tag-groups/) docs navigation sidebar. Add it to the root OpenAPI object.
5959

60+
If `x-tagGroups` is used for grouping API paths, and you've also configured `showSchemas: true` for your OpenAPI Docs plugin, an additional "sibling" category labelled `Schemas` will be created and placed at the end of sidebar, after all the `tagGroups` categories.
61+
6062
### Category Links
6163

6264
Docusaurus now supports the ability to designate or customize what page gets displayed when a category is clicked.

demo/docusaurus.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ const config: Config = {
266266
sidebarOptions: {
267267
groupPathsBy: "tagGroup",
268268
},
269+
showSchemas: true,
269270
} satisfies OpenApiPlugin.Options,
270271
} satisfies Plugin.PluginOptions,
271272
},

demo/examples/food/restaurant/openapi.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ paths:
4343
200:
4444
description: OK
4545

46+
components:
47+
schemas:
48+
Payment:
49+
type: object
50+
properties:
51+
amount:
52+
type: number
53+
method:
54+
type: string
55+
enum: [cash, card, check]
56+
4657
tags:
4758
- name: tag1
4859
description: Everything about your restaurant

packages/docusaurus-plugin-openapi-docs/src/sidebars/index.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export default function generateSidebarSlice(
267267
let sidebarSlice: ProcessedSidebar = [];
268268

269269
if (sidebarOptions.groupPathsBy === "tagGroup") {
270+
let schemasGroup: ProcessedSidebar = [];
270271
tagGroups?.forEach((tagGroup) => {
271272
//filter tags only included in group
272273
const filteredTags: TagObject[] = [];
@@ -288,10 +289,24 @@ export default function generateSidebarSlice(
288289
[filteredTags],
289290
docPath
290291
),
291-
} as ProcessedSidebarItem;
292+
};
292293

293-
sidebarSlice.push(groupCategory);
294+
if (options.showSchemas) {
295+
// For the first tagGroup, save the generated "Schemas" category for later.
296+
if (schemasGroup.length === 0) {
297+
schemasGroup = groupCategory.items?.filter(
298+
(item) => item.type === "category" && item.label === "Schemas"
299+
);
300+
}
301+
// Remove the "Schemas" category from every `groupCategory`.
302+
groupCategory.items = groupCategory.items.filter((item) =>
303+
"label" in item ? item.label !== "Schemas" : true
304+
);
305+
}
306+
sidebarSlice.push(groupCategory as ProcessedSidebarItem);
294307
});
308+
// Add `schemasGroup` to the end of the sidebar.
309+
sidebarSlice.push(...schemasGroup);
295310
} else if (sidebarOptions.groupPathsBy === "tag") {
296311
sidebarSlice = groupByTags(api, sidebarOptions, options, tags, docPath);
297312
}

0 commit comments

Comments
 (0)