Skip to content

Commit

Permalink
fix: false-positive recursive detection with allOf at the same level
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Sep 30, 2019
1 parent 4649683 commit faa74d6
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class RefCounter {
export class OpenAPIParser {
specUrl?: string;
spec: OpenAPISpec;
mergeRefs: Set<string>;

private _refCounter: RefCounter = new RefCounter();

Expand All @@ -57,6 +58,8 @@ export class OpenAPIParser {

this.spec = spec;

this.mergeRefs = new Set();

const href = IS_BROWSER ? window.location.href : '';
if (typeof specUrl === 'string') {
this.specUrl = urlResolve(href, specUrl);
Expand Down Expand Up @@ -183,7 +186,12 @@ export class OpenAPIParser {
schema: OpenAPISchema,
$ref?: string,
forceCircular: boolean = false,
used$Refs = new Set<string>(),
): MergedOpenAPISchema {
if ($ref) {
used$Refs.add($ref);
}

schema = this.hoistOneOfs(schema);

if (schema.allOf === undefined) {
Expand All @@ -205,16 +213,25 @@ export class OpenAPIParser {
receiver.items = { ...receiver.items };
}

const allOfSchemas = schema.allOf.map(subSchema => {
const resolved = this.deref(subSchema, forceCircular);
const subRef = subSchema.$ref || undefined;
const subMerged = this.mergeAllOf(resolved, subRef, forceCircular);
receiver.parentRefs!.push(...(subMerged.parentRefs || []));
return {
$ref: subRef,
schema: subMerged,
};
});
const allOfSchemas = schema.allOf
.map(subSchema => {
if (subSchema && subSchema.$ref && used$Refs.has(subSchema.$ref)) {
return undefined;
}

const resolved = this.deref(subSchema, forceCircular);
const subRef = subSchema.$ref || undefined;
const subMerged = this.mergeAllOf(resolved, subRef, forceCircular, used$Refs);
receiver.parentRefs!.push(...(subMerged.parentRefs || []));
return {
$ref: subRef,
schema: subMerged,
};
})
.filter(child => child !== undefined) as Array<{
$ref: string | undefined;
schema: MergedOpenAPISchema;
}>;

for (const { $ref: subSchemaRef, schema: subSchema } of allOfSchemas) {
if (
Expand Down

0 comments on commit faa74d6

Please sign in to comment.