Skip to content

Commit

Permalink
fix(walker): support circular JSON $refs with overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Nov 16, 2023
1 parent 628627c commit f39ebfe
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/__tests__/__fixtures__/references/circular-with-overrides.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"type": "object",
"properties": {
"order": {
"$ref": "#/definitions/Order",
"description": "My Order"
}
},
"definitions": {
"Cart": {
"type": "object",
"properties": {
"order": {
"$ref": "#/definitions/Order",
"description": "My Order"
}
}
},
"Order": {
"type": "object",
"properties": {
"member": {
"$ref": "#/definitions/Member",
"description": "Member"
}
}
},
"Member": {
"type": "object",
"properties": {
"referredMember": {
"$ref": "#/definitions/Member",
"description": "Member"
}
}
}
}
}
30 changes: 30 additions & 0 deletions src/__tests__/__snapshots__/tree.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,36 @@ exports[`SchemaTree output should generate valid tree for references/base.json 1
"
`;

exports[`SchemaTree output should generate valid tree for references/circular-with-overrides.json 1`] = `
"└─ #
├─ types
│ └─ 0: object
├─ primaryType: object
└─ children
└─ 0
└─ #/properties/order
├─ types
│ └─ 0: object
├─ primaryType: object
└─ children
└─ 0
└─ #/properties/order/properties/member
├─ types
│ └─ 0: object
├─ primaryType: object
└─ children
└─ 0
└─ #/properties/order/properties/member/properties/referredMember
├─ types
│ └─ 0: object
├─ primaryType: object
└─ children
└─ 0
└─ #/properties/order/properties/member/properties/referredMember/properties/referredMember
└─ mirrors: #/properties/order/properties/member/properties/referredMember
"
`;

exports[`SchemaTree output should generate valid tree for references/nullish.json 1`] = `
"└─ #
├─ types
Expand Down
8 changes: 7 additions & 1 deletion src/walker/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ export class Walker extends EventEmitter<WalkerEmitter> {
return retrieved;
}

let initialFragment: ProcessedFragment = fragment;

if ('$ref' in fragment) {
if (typeof walkingOptions.maxRefDepth === 'number' && walkingOptions.maxRefDepth < depth) {
return [new ReferenceNode(fragment, `max $ref depth limit reached`), fragment];
Expand All @@ -297,6 +299,11 @@ export class Walker extends EventEmitter<WalkerEmitter> {
if (typeof fragment.description === 'string') {
newFragment = { ...newFragment };
Object.assign(newFragment, { description: fragment.description });
} else {
retrieved = this.retrieveFromFragment(newFragment, originalFragment);
if (retrieved) {
return retrieved;
}
}

fragment = newFragment;
Expand Down Expand Up @@ -331,7 +338,6 @@ export class Walker extends EventEmitter<WalkerEmitter> {
}
}
}
let initialFragment: ProcessedFragment = fragment;
if (walkingOptions.mergeAllOf && SchemaCombinerName.AllOf in fragment) {
try {
if (Array.isArray(fragment.allOf)) {
Expand Down

0 comments on commit f39ebfe

Please sign in to comment.