Skip to content

Commit 8a42204

Browse files
committed
feat(slash): support slashes in pointer names
1 parent 9f1b2cb commit 8a42204

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

lib/pointer.ts

+15
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ class Pointer {
103103

104104
const token = tokens[i];
105105
if (this.value[token] === undefined || (this.value[token] === null && i === tokens.length - 1)) {
106+
// one final case is if the entry itself includes slashes, and was parsed out as a token - we can join the remaining tokens and try again
107+
let didFindSubstringSlashMatch = false;
108+
for (let j = tokens.length - 1; j > i; j--) {
109+
const joinedToken = tokens.slice(i, j + 1).join("/");
110+
if (this.value[joinedToken] !== undefined) {
111+
this.value = this.value[joinedToken];
112+
i = j;
113+
didFindSubstringSlashMatch = true;
114+
break;
115+
}
116+
}
117+
if (didFindSubstringSlashMatch) {
118+
continue;
119+
}
120+
106121
this.value = null;
107122
throw new MissingPointerError(token, decodeURI(this.originalPath));
108123
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
channels:
2+
'smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured':
3+
description: The topic on which measured values may be produced and consumed.
4+
parameters:
5+
streetlightId:
6+
description: The ID of the streetlight.
7+
schema:
8+
type: string

test/specs/substrings/slash.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, it } from "vitest";
2+
import $RefParser from "../../../lib/index.js";
3+
import path from "../../utils/path.js";
4+
5+
import { expect } from "vitest";
6+
7+
describe("$refs that include slashes", () => {
8+
it("should parse successfully", async () => {
9+
const parser = new $RefParser();
10+
await parser.parse(path.rel("test/specs/substrings/definitions/slash-strings.yaml"));
11+
const $refs = parser.$refs;
12+
const ref = $refs.get(
13+
"#/channels/smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured/parameters",
14+
);
15+
expect(ref).to.deep.equal({
16+
streetlightId: {
17+
description: "The ID of the streetlight.",
18+
schema: {
19+
type: "string",
20+
},
21+
},
22+
});
23+
});
24+
});

0 commit comments

Comments
 (0)