Skip to content

Commit

Permalink
fix: crashes on some dereferencing/allOf merging cases
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Nov 24, 2017
1 parent ce543cf commit 335deb9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ export class OpenAPIParser {
* resets visited enpoints. should be run after
*/
resetVisited() {
for (let k in this._refCounter._counter) {
if (this._refCounter._counter[k] > 0) {
if (__DEV__) {
// check in dev mode
for (let k in this._refCounter._counter) {
if (this._refCounter._counter[k] > 0) {
console.warn('Not exited reference: ' + k);
}
}
}
this._refCounter = new RefCounter();
Expand All @@ -129,11 +133,12 @@ export class OpenAPIParser {
deref<T extends object>(obj: OpenAPIRef | T, forceCircular: boolean = false): T {
if (this.isRef(obj)) {
const resolved = this.byRef<T>(obj.$ref)!;
if (this._refCounter.visited(obj.$ref) && !forceCircular) {
const visited = this._refCounter.visited(obj.$ref);
this._refCounter.visit(obj.$ref);
if (visited && !forceCircular) {
// circular reference detected
return Object.assign({}, resolved, { 'x-circular-ref': true });
}
this._refCounter.visit(obj.$ref);
// deref again in case one more $ref is here
if (this.isRef(resolved)) {
const res = this.deref(resolved);
Expand Down Expand Up @@ -169,9 +174,11 @@ export class OpenAPIParser {
const allOfSchemas = schema.allOf.map((subSchema, idx) => {
const resolved = this.deref(subSchema, forceCircular);
const subRef = subSchema.$ref || $ref + '/allOf/' + idx;
const subMerged = this.mergeAllOf(resolved, subRef, forceCircular);
receiver.namedParents!.push(...(subMerged.namedParents || []));
return {
$ref: subRef,
schema: this.mergeAllOf(resolved, subRef, forceCircular),
schema: subMerged,
};
});

Expand All @@ -188,7 +195,10 @@ export class OpenAPIParser {
throw new Error(`Uncopatible types in allOf at "${$ref}"`);
}

receiver.type = subSchema.type;
if (subSchema.type !== undefined) {
receiver.type = subSchema.type;
}

if (subSchema.properties !== undefined) {
// TODO: merge properties contents
receiver.properties = {
Expand Down
1 change: 1 addition & 0 deletions src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class SchemaModel {
this.init(parser, isChild);

parser.exitRef(schemaOrRef);

for (let $ref of this.schema.namedParents || []) {
// exit all the refs visited during allOf traverse
parser.exitRef({ $ref });
Expand Down

0 comments on commit 335deb9

Please sign in to comment.