Skip to content

Commit

Permalink
fix: allOf and deref exit not only named refs
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Dec 7, 2017
1 parent 14f8408 commit 435cccd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { COMPONENT_REGEXP, buildComponentComment } from './MarkdownRenderer';
import { RedocNormalizedOptions } from './RedocNormalizedOptions';
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
Expand Down Expand Up @@ -166,7 +166,7 @@ export class OpenAPIParser {
*/
mergeAllOf(
schema: OpenAPISchema,
$ref: string,
$ref?: string,
forceCircular: boolean = false,
): MergedOpenAPISchema {
if (schema.allOf === undefined) {
Expand All @@ -176,14 +176,14 @@ export class OpenAPIParser {
let receiver: MergedOpenAPISchema = {
...schema,
allOf: undefined,
namedParents: [],
parentRefs: [],
};

const allOfSchemas = schema.allOf.map((subSchema, idx) => {
const allOfSchemas = schema.allOf.map(subSchema => {
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);
receiver.namedParents!.push(...(subMerged.namedParents || []));
receiver.parentRefs!.push(...(subMerged.parentRefs || []));
return {
$ref: subRef,
schema: subMerged,
Expand Down Expand Up @@ -219,9 +219,9 @@ export class OpenAPIParser {
receiver.required = (receiver.required || []).concat(subSchema.required);
}

if (isNamedDefinition(subSchemaRef)) {
receiver.namedParents!.push(subSchemaRef);
if (receiver.title === undefined) {
if (subSchemaRef) {
receiver.parentRefs!.push(subSchemaRef);
if (receiver.title === undefined && isNamedDefinition(subSchemaRef)) {
receiver.title = JsonPointer.baseName(subSchemaRef);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class SchemaModel {

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
parser.exitRef({ $ref });
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export function langFromMime(contentType: string): string {
return 'clike';
}

export function isNamedDefinition(pointer: string): boolean {
return /^#\/components\/schemas\/[^\/]+$/.test(pointer);
export function isNamedDefinition(pointer?: string): boolean {
return /^#\/components\/schemas\/[^\/]+$/.test(pointer || '');
}

export function humanizeConstraints(schema: OpenAPISchema): string[] {
Expand Down

0 comments on commit 435cccd

Please sign in to comment.