Skip to content

Commit

Permalink
fix(spaces): support trailing spaces in pointer names
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca committed Mar 6, 2024
1 parent 5929a46 commit 5db51d8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/util/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
53 changes: 53 additions & 0 deletions test/specs/substrings/slash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ",
},
},
});
});
});

0 comments on commit 5db51d8

Please sign in to comment.