feat: support for ignoring specified named schemas

This commit is contained in:
Roman Hotsiy 2020-09-24 13:59:59 +03:00
parent a5468fb7bb
commit 9730c4ee1c
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 19 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types';
import { appendToMdHeading, IS_BROWSER } from '../utils/';
import { JsonPointer } from '../utils/JsonPointer';
import {
getDefinitionName,
isNamedDefinition,
SECURITY_DEFINITIONS_COMPONENT_NAME,
SECURITY_DEFINITIONS_JSX_NAME,
@ -150,6 +151,11 @@ export class OpenAPIParser {
*/
deref<T extends object>(obj: OpenAPIRef | T, forceCircular = false): T {
if (this.isRef(obj)) {
const schemaName = getDefinitionName(obj.$ref);
if (schemaName && this.options.ignoreNamedSchemas.has(schemaName)) {
return { type: 'object', title: schemaName } as T;
}
const resolved = this.byRef<T>(obj.$ref)!;
const visited = this._refCounter.visited(obj.$ref);
this._refCounter.visit(obj.$ref);
@ -202,7 +208,7 @@ export class OpenAPIParser {
...schema,
allOf: undefined,
parentRefs: [],
title: schema.title || (isNamedDefinition($ref) ? JsonPointer.baseName($ref) : undefined),
title: schema.title || getDefinitionName($ref),
};
// avoid mutating inner objects
@ -214,7 +220,7 @@ export class OpenAPIParser {
}
const allOfSchemas = schema.allOf
.map(subSchema => {
.map((subSchema) => {
if (subSchema && subSchema.$ref && used$Refs.has(subSchema.$ref)) {
return undefined;
}

View File

@ -40,6 +40,7 @@ export interface RedocRawOptions {
expandDefaultServerVariables?: boolean;
maxDisplayedEnumValues?: number;
ignoreNamedSchemas?: string[] | string;
}
function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
@ -192,6 +193,8 @@ export class RedocNormalizedOptions {
expandDefaultServerVariables: boolean;
maxDisplayedEnumValues?: number;
ignoreNamedSchemas: Set<string>;
constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) {
raw = { ...defaults, ...raw };
const hook = raw.theme && raw.theme.extensionsHook;
@ -247,5 +250,7 @@ export class RedocNormalizedOptions {
this.expandDefaultServerVariables = argValueToBoolean(raw.expandDefaultServerVariables);
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);
}
}

View File

@ -369,6 +369,12 @@ export function isNamedDefinition(pointer?: string): boolean {
return /^#\/components\/schemas\/[^\/]+$/.test(pointer || '');
}
export function getDefinitionName(pointer?: string): string | undefined {
if (!pointer) return undefined;
const match = pointer.match(/^#\/components\/schemas\/([^\/]+)$/);
return match === null ? undefined : match[1]
}
function humanizeMultipleOfConstraint(multipleOf: number | undefined): string | undefined {
if (multipleOf === undefined) {
return;