Skip to content

Commit f55014f

Browse files
authored
fix(traverse): fix Path.getPathKeys() method (#26)
The method now returns semantic structures instead of internal ApiDOM paths.
1 parent a3bfd57 commit f55014f

File tree

2 files changed

+172
-224
lines changed

2 files changed

+172
-224
lines changed

packages/apidom-traverse/src/Path.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { Element } from '@speclynx/apidom-datamodel';
1+
import {
2+
isMemberElement,
3+
isArrayElement,
4+
isStringElement,
5+
type Element,
6+
} from '@speclynx/apidom-datamodel';
27
import { compile as compileJSONPointer } from '@speclynx/apidom-json-pointer';
38
import { NormalizedPath } from '@speclynx/apidom-json-path';
49

@@ -152,16 +157,36 @@ export class Path<TNode = Element> {
152157
}
153158

154159
/**
155-
* Get the path from root as an array of keys.
160+
* Get the semantic path from root as an array of keys.
161+
* Returns logical document keys (property names, array indices) rather than
162+
* internal ApiDOM structure keys.
163+
*
164+
* @example
165+
* // For a path to $.paths['/pets'].get in an OpenAPI document:
166+
* path.getPathKeys(); // => ['paths', '/pets', 'get']
156167
*/
157168
getPathKeys(): PropertyKey[] {
158169
const keys: PropertyKey[] = [];
159170
// eslint-disable-next-line @typescript-eslint/no-this-alias
160171
let current: Path<TNode> | null = this;
172+
161173
while (current !== null && current.key !== undefined) {
162-
keys.unshift(current.key);
174+
const { key, parent, parentPath } = current;
175+
176+
if (isMemberElement(parent) && key === 'value') {
177+
// Inside MemberElement.value → push the member's key
178+
if (!isStringElement(parent.key)) {
179+
throw new TypeError('MemberElement.key must be a StringElement');
180+
}
181+
keys.unshift(parent.key.toValue() as PropertyKey);
182+
} else if (isArrayElement(parentPath?.node) && typeof key === 'number') {
183+
// Inside ArrayElement → push the numeric index
184+
keys.unshift(key);
185+
}
186+
163187
current = current.parentPath;
164188
}
189+
165190
return keys;
166191
}
167192

0 commit comments

Comments
 (0)