From 5db51d86680e504efbb6d18f7e3f04647b8ad7f3 Mon Sep 17 00:00:00 2001 From: JonLuca DeCaro Date: Tue, 5 Mar 2024 23:14:55 -0800 Subject: [PATCH] fix(spaces): support trailing spaces in pointer names --- lib/util/url.ts | 14 ++++++-- test/specs/substrings/slash.spec.ts | 53 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/lib/util/url.ts b/lib/util/url.ts index 3826fa91..9ab7e99c 100644 --- a/lib/util/url.ts +++ b/lib/util/url.ts @@ -31,7 +31,9 @@ export function resolve(from: string, to: string) { if (resolvedUrl.protocol === "resolve:") { // `from` is a relative URL. const { pathname, search, hash } = resolvedUrl; - return pathname + search + hash; + const endSpaces = to.match(/(\s*)$/)?.[1] || ""; + + return pathname + search + hash + endSpaces; } return resolvedUrl.toString(); } @@ -105,7 +107,10 @@ export function stripQuery(path: any) { * @param path * @returns */ -export function getHash(path: string) { +export function getHash(path: undefined | string) { + if (!path) { + return "#"; + } const hashIndex = path.indexOf("#"); if (hashIndex >= 0) { return path.substring(hashIndex); @@ -119,7 +124,10 @@ export function getHash(path: string) { * @param path * @returns */ -export function stripHash(path: string) { +export function stripHash(path?: string | undefined) { + if (!path) { + return ""; + } const hashIndex = path.indexOf("#"); if (hashIndex >= 0) { path = path.substring(0, hashIndex); diff --git a/test/specs/substrings/slash.spec.ts b/test/specs/substrings/slash.spec.ts index bf6bb0a0..5356e00e 100644 --- a/test/specs/substrings/slash.spec.ts +++ b/test/specs/substrings/slash.spec.ts @@ -21,4 +21,57 @@ describe("$refs that include slashes", () => { }, }); }); + + it("should parse trailing spaces successfully", async () => { + const parser = new $RefParser(); + const derefed = await parser.dereference({ + swagger: "2.0", + paths: { + somepath: { + post: { + $ref: "#/definitions/ABC ", + }, + }, + }, + definitions: { + "ABC ": { + // tested removing space at the end of "ABC " + type: "object", + properties: { + abc: { + type: "string", + }, + }, + title: "ABC ", + }, + }, + }); + expect(derefed).to.deep.equal({ + swagger: "2.0", + paths: { + somepath: { + post: { + type: "object", + properties: { + abc: { + type: "string", + }, + }, + title: "ABC ", + }, + }, + }, + definitions: { + "ABC ": { + type: "object", + properties: { + abc: { + type: "string", + }, + }, + title: "ABC ", + }, + }, + }); + }); });