feat: add support schemaDefinitionsTagName

This commit is contained in:
Alex Varchuk 2025-01-17 16:16:04 +01:00
parent 417b934ffc
commit bf97710c59
No known key found for this signature in database
GPG Key ID: 8A9260AE529FF454
2 changed files with 25 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import type { OpenAPISpec, OpenAPIPaths, OpenAPITag, OpenAPISchema } from '../types';
import type { OpenAPIPaths, OpenAPITag, OpenAPISchema } from '../types';
import { isOperationName, JsonPointer, alphabeticallyByProp } from '../utils';
import { MarkdownRenderer } from './MarkdownRenderer';
import { GroupModel, OperationModel } from './models';
@ -17,9 +17,19 @@ export class MenuBuilder {
options: RedocNormalizedOptions,
): ContentItemModel[] {
const spec = parser.spec;
const { schemaDefinitionsTagName } = options;
const items: ContentItemModel[] = [];
const tagsMap = MenuBuilder.getTagsWithOperations(parser, spec);
const tags = [...(spec.tags || [])];
const hasAutogenerated = [...(spec.tags || [])].find(
tag => tag?.name === schemaDefinitionsTagName,
);
console.log('hasAutogenerated', hasAutogenerated, schemaDefinitionsTagName);
if (!hasAutogenerated && schemaDefinitionsTagName) {
tags.push({ name: schemaDefinitionsTagName });
}
const tagsMap = MenuBuilder.getTagsWithOperations(parser, tags);
items.push(...MenuBuilder.addMarkdownItems(spec.info.description || '', undefined, 1, options));
if (spec['x-tagGroups'] && spec['x-tagGroups'].length > 0) {
items.push(
@ -28,6 +38,7 @@ export class MenuBuilder {
} else {
items.push(...MenuBuilder.getTagsItems(parser, tagsMap, undefined, undefined, options));
}
console.log('items', items);
return items;
}
@ -141,6 +152,7 @@ export class MenuBuilder {
parser,
tag,
parent: item,
schemaDefinitionsTagName: options.schemaDefinitionsTagName,
});
item.items = [
@ -195,10 +207,11 @@ export class MenuBuilder {
/**
* collects tags and maps each tag to list of operations belonging to this tag
*/
static getTagsWithOperations(parser: OpenAPIParser, spec: OpenAPISpec): TagsInfoMap {
static getTagsWithOperations(parser: OpenAPIParser, explicitTags: OpenAPITag[]): TagsInfoMap {
const { spec } = parser;
const tags: TagsInfoMap = {};
const webhooks = spec['x-webhooks'] || spec.webhooks;
for (const tag of spec.tags || []) {
for (const tag of explicitTags || []) {
tags[tag.name] = { ...tag, operations: [] };
}
@ -260,14 +273,18 @@ export class MenuBuilder {
parser,
tag,
parent,
schemaDefinitionsTagName,
}: {
parser: OpenAPIParser;
tag: TagInfo;
parent: GroupModel;
schemaDefinitionsTagName?: string;
}): GroupModel[] {
const defaultTags = schemaDefinitionsTagName ? [schemaDefinitionsTagName] : [];
return Object.entries(parser.spec.components?.schemas || {})
.map(([schemaName, schema]) => {
const schemaTags = schema['x-tags'];
const schemaTags = schema['x-tags'] || defaultTags;
if (!schemaTags?.includes(tag.name)) return null;
const item = new GroupModel(

View File

@ -47,6 +47,7 @@ export interface RedocRawOptions {
expandSingleSchemaField?: boolean | string;
schemaExpansionLevel?: number | string | 'all'; // remove in next major release
schemasExpansionLevel?: number | string | 'all';
schemaDefinitionsTagName?: string;
showObjectSchemaExamples?: boolean | string;
showSecuritySchemeType?: boolean;
hideSecuritySection?: boolean;
@ -253,6 +254,7 @@ export class RedocNormalizedOptions {
payloadSampleIdx: number;
expandSingleSchemaField: boolean;
schemasExpansionLevel: number;
schemaDefinitionsTagName?: string;
showObjectSchemaExamples: boolean;
showSecuritySchemeType?: boolean;
hideSecuritySection?: boolean;
@ -332,6 +334,7 @@ export class RedocNormalizedOptions {
this.schemasExpansionLevel = argValueToExpandLevel(
raw.schemasExpansionLevel || raw.schemaExpansionLevel,
);
this.schemaDefinitionsTagName = raw.schemaDefinitionsTagName;
this.showObjectSchemaExamples = argValueToBoolean(raw.showObjectSchemaExamples);
this.showSecuritySchemeType = argValueToBoolean(raw.showSecuritySchemeType);
this.hideSecuritySection = argValueToBoolean(raw.hideSecuritySection);