From bf97710c59831ad78935c5ea60c3fd2c08b13249 Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Fri, 17 Jan 2025 16:16:04 +0100 Subject: [PATCH] feat: add support schemaDefinitionsTagName --- src/services/MenuBuilder.ts | 27 +++++++++++++++++++++----- src/services/RedocNormalizedOptions.ts | 3 +++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/services/MenuBuilder.ts b/src/services/MenuBuilder.ts index 4ad28f7b..e09c03bf 100644 --- a/src/services/MenuBuilder.ts +++ b/src/services/MenuBuilder.ts @@ -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( diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index 7bfbc963..951871c7 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -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);