Skip to content

Commit

Permalink
feat(slash): support slashes in pointer names
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca committed Mar 6, 2024
1 parent 9f1b2cb commit 8a42204
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ class Pointer {

const token = tokens[i];
if (this.value[token] === undefined || (this.value[token] === null && i === tokens.length - 1)) {
// 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
let didFindSubstringSlashMatch = false;
for (let j = tokens.length - 1; j > i; j--) {
const joinedToken = tokens.slice(i, j + 1).join("/");
if (this.value[joinedToken] !== undefined) {
this.value = this.value[joinedToken];
i = j;
didFindSubstringSlashMatch = true;
break;
}
}
if (didFindSubstringSlashMatch) {
continue;
}

this.value = null;
throw new MissingPointerError(token, decodeURI(this.originalPath));
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/specs/substrings/definitions/slash-strings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
channels:
'smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured':
description: The topic on which measured values may be produced and consumed.
parameters:
streetlightId:
description: The ID of the streetlight.
schema:
type: string
24 changes: 24 additions & 0 deletions test/specs/substrings/slash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, it } from "vitest";
import $RefParser from "../../../lib/index.js";
import path from "../../utils/path.js";

import { expect } from "vitest";

describe("$refs that include slashes", () => {
it("should parse successfully", async () => {
const parser = new $RefParser();
await parser.parse(path.rel("test/specs/substrings/definitions/slash-strings.yaml"));
const $refs = parser.$refs;
const ref = $refs.get(
"#/channels/smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured/parameters",
);
expect(ref).to.deep.equal({
streetlightId: {
description: "The ID of the streetlight.",
schema: {
type: "string",
},
},
});
});
});

0 comments on commit 8a42204

Please sign in to comment.