Skip to content

Commit dee59bc

Browse files
committed
fix: generate correct paths for $ref array items
1 parent e0770ef commit dee59bc

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

src/tree/__tests__/tree.spec.ts

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('SchemaTree', () => {
107107
});
108108
});
109109

110-
describe('array with refed items', () => {
110+
describe('array with $reffed items', () => {
111111
let tree: SchemaTree;
112112
let schema: JSONSchema4;
113113

@@ -163,4 +163,102 @@ describe('SchemaTree', () => {
163163
});
164164
});
165165
});
166+
167+
describe('paths generation', () => {
168+
let schema: JSONSchema4;
169+
let tree: SchemaTree;
170+
171+
beforeEach(() => {
172+
schema = {
173+
type: 'object',
174+
properties: {
175+
user: {
176+
type: 'object',
177+
properties: {
178+
name: {
179+
type: 'string',
180+
},
181+
address: {
182+
type: 'string',
183+
},
184+
},
185+
},
186+
permissions: {
187+
type: 'array',
188+
items: {
189+
$ref: '#/properties/user',
190+
},
191+
},
192+
},
193+
};
194+
195+
tree = new SchemaTree(schema, new SchemaTreeState(), {
196+
expandedDepth: Infinity,
197+
mergeAllOf: false,
198+
resolveRef: void 0,
199+
});
200+
});
201+
202+
test('for root', () => {
203+
tree.populate();
204+
205+
expect(getNodeMetadata(tree.itemAt(0)!)).toHaveProperty('path', []);
206+
});
207+
208+
test('for plain object member', () => {
209+
tree.populate();
210+
211+
expect(getNodeMetadata(tree.itemAt(1)!)).toHaveProperty('path', [
212+
'properties',
213+
'user'
214+
]);
215+
});
216+
217+
test('for deep object member', () => {
218+
tree.populate();
219+
220+
expect(getNodeMetadata(tree.itemAt(2)!)).toHaveProperty('path', [
221+
'properties',
222+
'user',
223+
'properties',
224+
'name',
225+
]);
226+
});
227+
228+
test('for array items', () => {
229+
tree.populate();
230+
231+
expect(getNodeMetadata(tree.itemAt(tree.count - 1)!)).toHaveProperty('path', [
232+
'properties',
233+
'permissions',
234+
]);
235+
});
236+
237+
test('for $reffed array items', () => {
238+
tree.populate();
239+
tree.unwrap(tree.itemAt(tree.count - 1) as TreeListParentNode);
240+
241+
expect(getNodeMetadata(tree.itemAt(tree.count - 3)!)).toHaveProperty('path', [
242+
'properties',
243+
'permissions',
244+
'items',
245+
]);
246+
247+
expect(getNodeMetadata(tree.itemAt(tree.count - 2)!)).toHaveProperty('path', [
248+
'properties',
249+
'permissions',
250+
'items',
251+
'properties',
252+
'name',
253+
]);
254+
255+
expect(getNodeMetadata(tree.itemAt(tree.count - 1)!)).toHaveProperty('path', [
256+
'properties',
257+
'permissions',
258+
'items',
259+
'properties',
260+
'address',
261+
]);
262+
});
263+
});
166264
});

src/tree/tree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class SchemaTree extends Tree {
120120
} else if (isRefNode(schemaNode)) {
121121
this.populateRefFragment(node, path, schemaNode.$ref);
122122
} else if (hasRefItems(schemaNode)) {
123-
this.populateRefFragment(node, path, schemaNode.items.$ref);
123+
this.populateRefFragment(node, [...path, 'items'], schemaNode.items.$ref);
124124
} else {
125125
throw new Error(`I do know not how not expand node ${path.join('.')}`);
126126
}

0 commit comments

Comments
 (0)