Skip to content

Commit

Permalink
Merge pull request #44 from stoplightio/get-location-incomplete-prope…
Browse files Browse the repository at this point in the history
…rty-pair

findNodeAtLocation does not handle incomplete property pair
  • Loading branch information
aeschli authored Dec 30, 2021
2 parents 4df0535 + 7e6ad07 commit fee184d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/impl/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export function findNodeAtLocation(root: Node | undefined, path: JSONPath): Node
}
let found = false;
for (const propertyNode of node.children) {
if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment) {
if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment && propertyNode.children.length === 2) {
node = propertyNode.children[1];
found = true;
break;
Expand Down
4 changes: 3 additions & 1 deletion src/test/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ suite('JSON', () => {
});

test('tree: find location', () => {
let root = parseTree('{ "key1": { "key11": [ "val111", "val112" ] }, "key2": [ { "key21": false, "key22": 221 }, null, [{}] ] }');
let root = parseTree('{ "key1": { "key11": [ "val111", "val112" ] }, "key2": [ { "key21": false, "key22": 221 }, null, [{}] ], "key3": { "key31":, "key32": 32 } }');
assertNodeAtLocation(root, ['key1'], { key11: ['val111', 'val112'] });
assertNodeAtLocation(root, ['key1', 'key11'], ['val111', 'val112']);
assertNodeAtLocation(root, ['key1', 'key11', 0], 'val111');
Expand All @@ -586,6 +586,8 @@ suite('JSON', () => {
assertNodeAtLocation(root, ['key2', 1], null);
assertNodeAtLocation(root, ['key2', 2], [{}]);
assertNodeAtLocation(root, ['key2', 2, 0], {});
assertNodeAtLocation(root, ['key3', 'key31', 'key311'], undefined);
assertNodeAtLocation(root, ['key3', 'key32'], 32);
});

test('location: matches', () => {
Expand Down

0 comments on commit fee184d

Please sign in to comment.