Skip to content

Commit adc6c6a

Browse files
committed
feat: unwrap local pointers
1 parent b5ece97 commit adc6c6a

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/__fixtures__/default-schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
}
6969
},
7070
"ref": {
71-
"$ref": "#/definitions/error-response"
71+
"$ref": "#/properties/permissions"
7272
}
7373
},
7474
"patternProperties": {

src/tree/tree.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { isLocalRef } from '@stoplight/json';
1+
import { isLocalRef, pointerToPath } from '@stoplight/json';
22
import { Tree, TreeListParentNode, TreeState } from '@stoplight/tree-list';
3+
import { JsonPath } from '@stoplight/types/dist';
34
import { JSONSchema4 } from 'json-schema';
45
import { get } from 'lodash-es';
56
import { SchemaNode } from '../types';
@@ -30,8 +31,7 @@ export class SchemaTree extends Tree {
3031
this.invalidate();
3132
}
3233

33-
public populateTreeFragment(parent: TreeListParentNode) {
34-
const { path } = MetadataStore[parent.id];
34+
public populateTreeFragment(parent: TreeListParentNode, path: JsonPath) {
3535
const initialLevel = Tree.getLevel(parent);
3636
const artificialRoot = Tree.createArtificialRoot();
3737
populateTree(get(this.schema, path), artificialRoot, initialLevel, path, {
@@ -43,12 +43,18 @@ export class SchemaTree extends Tree {
4343
}
4444

4545
public unwrap(node: TreeListParentNode) {
46-
if (isRefNode(MetadataStore[node.id].schema)) {
47-
} else if (node.children.length === 0 && !this.visited.has(node)) {
48-
this.populateTreeFragment(node);
49-
super.unwrap(node);
46+
if (node.children.length !== 0 || this.visited.has(node)) {
47+
return super.unwrap(node);
48+
}
49+
50+
const { path, schema } = MetadataStore[node.id];
51+
if (isRefNode(schema)) {
52+
this.populateTreeFragment(node, pointerToPath(schema.$ref));
5053
} else {
51-
super.unwrap(node);
54+
this.populateTreeFragment(node, path);
5255
}
56+
57+
this.visited.add(node);
58+
return super.unwrap(node);
5359
}
5460
}

0 commit comments

Comments
 (0)