fix: allOf and deref exit not only named refs

This commit is contained in:
Roman Hotsiy 2017-12-07 18:38:49 +02:00
parent 14f8408e8d
commit 435cccd72f
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 12 additions and 12 deletions

View File

@ -9,7 +9,7 @@ import { COMPONENT_REGEXP, buildComponentComment } from './MarkdownRenderer';
import { RedocNormalizedOptions } from './RedocNormalizedOptions'; import { RedocNormalizedOptions } from './RedocNormalizedOptions';
import { appendToMdHeading } from '../utils/index'; import { appendToMdHeading } from '../utils/index';
export type MergedOpenAPISchema = OpenAPISchema & { namedParents?: string[] }; export type MergedOpenAPISchema = OpenAPISchema & { parentRefs?: string[] };
/** /**
* Helper class to keep track of visited references to avoid * Helper class to keep track of visited references to avoid
@ -166,7 +166,7 @@ export class OpenAPIParser {
*/ */
mergeAllOf( mergeAllOf(
schema: OpenAPISchema, schema: OpenAPISchema,
$ref: string, $ref?: string,
forceCircular: boolean = false, forceCircular: boolean = false,
): MergedOpenAPISchema { ): MergedOpenAPISchema {
if (schema.allOf === undefined) { if (schema.allOf === undefined) {
@ -176,14 +176,14 @@ export class OpenAPIParser {
let receiver: MergedOpenAPISchema = { let receiver: MergedOpenAPISchema = {
...schema, ...schema,
allOf: undefined, allOf: undefined,
namedParents: [], parentRefs: [],
}; };
const allOfSchemas = schema.allOf.map((subSchema, idx) => { const allOfSchemas = schema.allOf.map(subSchema => {
const resolved = this.deref(subSchema, forceCircular); const resolved = this.deref(subSchema, forceCircular);
const subRef = subSchema.$ref || $ref + '/allOf/' + idx; const subRef = subSchema.$ref || undefined;
const subMerged = this.mergeAllOf(resolved, subRef, forceCircular); const subMerged = this.mergeAllOf(resolved, subRef, forceCircular);
receiver.namedParents!.push(...(subMerged.namedParents || [])); receiver.parentRefs!.push(...(subMerged.parentRefs || []));
return { return {
$ref: subRef, $ref: subRef,
schema: subMerged, schema: subMerged,
@ -219,9 +219,9 @@ export class OpenAPIParser {
receiver.required = (receiver.required || []).concat(subSchema.required); receiver.required = (receiver.required || []).concat(subSchema.required);
} }
if (isNamedDefinition(subSchemaRef)) { if (subSchemaRef) {
receiver.namedParents!.push(subSchemaRef); receiver.parentRefs!.push(subSchemaRef);
if (receiver.title === undefined) { if (receiver.title === undefined && isNamedDefinition(subSchemaRef)) {
receiver.title = JsonPointer.baseName(subSchemaRef); receiver.title = JsonPointer.baseName(subSchemaRef);
} }
} }

View File

@ -69,7 +69,7 @@ export class SchemaModel {
parser.exitRef(schemaOrRef); parser.exitRef(schemaOrRef);
for (let $ref of this.schema.namedParents || []) { for (let $ref of this.schema.parentRefs || []) {
// exit all the refs visited during allOf traverse // exit all the refs visited during allOf traverse
parser.exitRef({ $ref }); parser.exitRef({ $ref });
} }

View File

@ -113,8 +113,8 @@ export function langFromMime(contentType: string): string {
return 'clike'; return 'clike';
} }
export function isNamedDefinition(pointer: string): boolean { export function isNamedDefinition(pointer?: string): boolean {
return /^#\/components\/schemas\/[^\/]+$/.test(pointer); return /^#\/components\/schemas\/[^\/]+$/.test(pointer || '');
} }
export function humanizeConstraints(schema: OpenAPISchema): string[] { export function humanizeConstraints(schema: OpenAPISchema): string[] {