|
1 | | -import type { Element } from '@speclynx/apidom-datamodel'; |
| 1 | +import { |
| 2 | + isMemberElement, |
| 3 | + isArrayElement, |
| 4 | + isStringElement, |
| 5 | + type Element, |
| 6 | +} from '@speclynx/apidom-datamodel'; |
2 | 7 | import { compile as compileJSONPointer } from '@speclynx/apidom-json-pointer'; |
3 | 8 | import { NormalizedPath } from '@speclynx/apidom-json-path'; |
4 | 9 |
|
@@ -152,16 +157,36 @@ export class Path<TNode = Element> { |
152 | 157 | } |
153 | 158 |
|
154 | 159 | /** |
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'] |
156 | 167 | */ |
157 | 168 | getPathKeys(): PropertyKey[] { |
158 | 169 | const keys: PropertyKey[] = []; |
159 | 170 | // eslint-disable-next-line @typescript-eslint/no-this-alias |
160 | 171 | let current: Path<TNode> | null = this; |
| 172 | + |
161 | 173 | 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 | + |
163 | 187 | current = current.parentPath; |
164 | 188 | } |
| 189 | + |
165 | 190 | return keys; |
166 | 191 | } |
167 | 192 |
|
|
0 commit comments