Skip to content

Commit c81b48f

Browse files
committed
normalize path
1 parent df7967e commit c81b48f

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

test/specs/resolvers/resolvers.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ async function withSelfSignedHttpsServer(run: (schemaUrl: string) => Promise<voi
5656
}
5757
}
5858

59+
function normalizeWindowsDriveUrl(path: string) {
60+
return path.replace(/\\/g, "/").replace(/^([A-Z]):/, (_, drive: string) => `${drive.toLowerCase()}:`);
61+
}
62+
5963
describe("options.resolve", () => {
6064
it('should not resolve external links if "resolve.external" is disabled', async () => {
6165
const schema = await $RefParser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"), {
@@ -338,9 +342,10 @@ describe("options.resolve", () => {
338342
order: 1,
339343
canRead: true,
340344
read(file: FileInfo) {
341-
seenUrls.push(file.url);
345+
const normalizedUrl = normalizeWindowsDriveUrl(file.url);
346+
seenUrls.push(normalizedUrl);
342347

343-
const remote = remotes[file.url];
348+
const remote = remotes[normalizedUrl];
344349
if (remote === undefined) {
345350
throw new Error(`Unexpected file URL: ${file.url}`);
346351
}

test/specs/special-characters-files/special-characters-files.spec.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@ import path from "../../utils/path.js";
44

55
const isBrowser = typeof window !== "undefined";
66

7+
function normalizePathForAssertion(entry: string) {
8+
return decodeURIComponent(entry)
9+
.replace(/\\/g, "/")
10+
.replace(/^file:\/\/\/([A-Z]):/, (_match, drive: string) => `file:///${drive.toLowerCase()}:`)
11+
.replace(/^([A-Z]):/, (_match, drive: string) => `${drive.toLowerCase()}:`);
12+
}
13+
714
describe.skipIf(isBrowser)("Special-character file paths", () => {
815
// Adapted replacement for the repo's previously ignored filename regression, using
916
// a cross-platform-safe subset of special characters.
1017
const schemaPath = path.rel(
1118
"test/specs/special-characters-files/dir with [brackets] & spaces/root schema.yaml",
1219
);
13-
const nestedPath = path.abs(
14-
"test/specs/special-characters-files/dir with [brackets] & spaces/defs [value] %.json",
15-
);
1620

1721
it("should resolve external refs from filenames with spaces and special characters", async () => {
1822
const parser = new $RefParser();
1923
const $refs = await parser.resolve(schemaPath);
20-
const resolvedPath = $refs.paths().find((entry) => entry.includes("defs [value] %.json"));
2124
const values = $refs.values();
25+
const resolvedEntry = Object.entries(values).find(([entry]) =>
26+
normalizePathForAssertion(entry).includes("defs [value] %.json"),
27+
);
2228

23-
expect($refs.paths()).toContain(nestedPath);
24-
expect(resolvedPath).toBeTruthy();
25-
expect(values[resolvedPath as string]).toEqual({
29+
expect(resolvedEntry).toBeTruthy();
30+
expect(resolvedEntry?.[1]).toEqual({
2631
definitions: {
2732
value: {
2833
type: "object",
@@ -32,7 +37,7 @@ describe.skipIf(isBrowser)("Special-character file paths", () => {
3237
},
3338
},
3439
});
35-
expect(values[resolvedPath as string].definitions.value).toEqual({
40+
expect((resolvedEntry?.[1] as any).definitions.value).toEqual({
3641
type: "object",
3742
properties: {
3843
name: { type: "string" },

0 commit comments

Comments
 (0)