Skip to content

Commit

Permalink
feat: add support schemaDefinitionsTagName
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk committed Jan 17, 2025
1 parent 417b934 commit bf97710
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/services/MenuBuilder.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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(
Expand All @@ -28,6 +38,7 @@ export class MenuBuilder {
} else {
items.push(...MenuBuilder.getTagsItems(parser, tagsMap, undefined, undefined, options));
}
console.log('items', items);
return items;
}

Expand Down Expand Up @@ -141,6 +152,7 @@ export class MenuBuilder {
parser,
tag,
parent: item,
schemaDefinitionsTagName: options.schemaDefinitionsTagName,
});

item.items = [
Expand Down Expand Up @@ -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: [] };
}

Expand Down Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -253,6 +254,7 @@ export class RedocNormalizedOptions {
payloadSampleIdx: number;
expandSingleSchemaField: boolean;
schemasExpansionLevel: number;
schemaDefinitionsTagName?: string;
showObjectSchemaExamples: boolean;
showSecuritySchemeType?: boolean;
hideSecuritySection?: boolean;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bf97710

Please sign in to comment.