Add showSchemas to show all top-level Schemas under group in menu

This commit is contained in:
Shelby Sanders 2020-10-26 17:03:37 -07:00
parent b4f3f02f8d
commit 23f1576a32
5 changed files with 46 additions and 0 deletions

View File

@ -247,6 +247,7 @@ You can use all of the following options with standalone version on <redoc> tag
* **selector**: selector of the element to be used for specifying the offset. The distance from the top of the page to the element's bottom will be used as offset.
* **function**: A getter function. Must return a number representing the offset (in pixels).
* `showExtensions` - show vendor extensions ("x-" fields). Extensions used by ReDoc are ignored. Can be boolean or an array of `string` with names of extensions to display.
* `showSchemas` - Show all top-level Schemas under group in menu, default `false`.
* `sortPropsAlphabetically` - sort properties alphabetically.
* `suppressWarnings` - if set, warnings are not rendered at the top of documentation (they still are logged to the console).
* `payloadSampleIdx` - if set, payload sample will be inserted at this index or last. Indexes start from 0.

View File

@ -42,4 +42,18 @@ describe('Menu', () => {
cy.contains('[role=menuitem].-depth1', 'store').click({ force: true });
petItem().should('not.have.class', 'active');
});
it('should omit Schemas group when showSchemas == false, which is default', () => {
cy.get('.menu-content').should('exist');
cy.get('[data-item-id="section/Schemas"]').should('not.exist');
cy.get('[type=section] [title=HoneyBee]').should('not.exist');
});
it('should include Schemas group and sections when showSchemas == true', () => {
cy.visit('e2e/showSchemas.html');
cy.get('.menu-content').should('exist');
cy.get('[data-item-id="section/Schemas"]').should('exist');
cy.get('[type=section] [title=HoneyBee]').should('exist');
});
});

8
e2e/showSchemas.html Normal file
View File

@ -0,0 +1,8 @@
<html>
<body>
<redoc spec-url="../demo/openapi.yaml" show-schemas="true"></redoc>
<script src="../bundles/redoc.standalone.js"></script>
</body>
</html>

View File

@ -62,6 +62,22 @@ export class MenuBuilder {
} else {
items.push(...MenuBuilder.getTagsItems(parser, tagsMap, undefined, undefined, options));
}
if (options.showSchemas && spec.components?.schemas) {
// Ignore entries that are side-effects of Swagger 2 conversion
const titles = Object.keys(spec.components?.schemas).filter((title) => {
return !/^schema[0-9]*$/.test(title);
});
let markdown = '# Schemas\n\n';
titles.sort();
for (const title of titles) {
markdown += `## ${title}\n`;
markdown += `<SchemaDefinition schemaRef="#/components/schemas/${title}" />\n\n`;
}
items.push(...MenuBuilder.addMarkdownItems(markdown, undefined, 1, options));
}
return items;
}

View File

@ -41,6 +41,7 @@ export interface RedocRawOptions {
expandDefaultServerVariables?: boolean;
maxDisplayedEnumValues?: number;
ignoreNamedSchemas?: string[] | string;
showSchemas?: boolean;
}
function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
@ -86,6 +87,10 @@ export class RedocNormalizedOptions {
return !!value;
}
static normalizeShowSchemas(value: RedocRawOptions['showSchemas']): boolean {
return !!value;
}
static normalizeScrollYOffset(value: RedocRawOptions['scrollYOffset']): () => number {
// just number is not valid selector and leads to crash so checking if isNumeric here
if (typeof value === 'string' && !isNumeric(value)) {
@ -194,6 +199,7 @@ export class RedocNormalizedOptions {
maxDisplayedEnumValues?: number;
ignoreNamedSchemas: Set<string>;
showSchemas: boolean;
constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) {
raw = { ...defaults, ...raw };
@ -252,5 +258,6 @@ export class RedocNormalizedOptions {
this.maxDisplayedEnumValues = argValueToNumber(raw.maxDisplayedEnumValues);
const ignoreNamedSchemas = Array.isArray(raw.ignoreNamedSchemas) ? raw.ignoreNamedSchemas : raw.ignoreNamedSchemas?.split(',').map(s => s.trim());
this.ignoreNamedSchemas = new Set(ignoreNamedSchemas);
this.showSchemas = RedocNormalizedOptions.normalizeShowSchemas(raw.showSchemas);
}
}