Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(walker): support circular JSON $refs with overrides #26

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading