Skip to content

Commit 5db51d8

Browse files
committed
fix(spaces): support trailing spaces in pointer names
1 parent 5929a46 commit 5db51d8

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

lib/util/url.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export function resolve(from: string, to: string) {
3131
if (resolvedUrl.protocol === "resolve:") {
3232
// `from` is a relative URL.
3333
const { pathname, search, hash } = resolvedUrl;
34-
return pathname + search + hash;
34+
const endSpaces = to.match(/(\s*)$/)?.[1] || "";
35+
36+
return pathname + search + hash + endSpaces;
3537
}
3638
return resolvedUrl.toString();
3739
}
@@ -105,7 +107,10 @@ export function stripQuery(path: any) {
105107
* @param path
106108
* @returns
107109
*/
108-
export function getHash(path: string) {
110+
export function getHash(path: undefined | string) {
111+
if (!path) {
112+
return "#";
113+
}
109114
const hashIndex = path.indexOf("#");
110115
if (hashIndex >= 0) {
111116
return path.substring(hashIndex);
@@ -119,7 +124,10 @@ export function getHash(path: string) {
119124
* @param path
120125
* @returns
121126
*/
122-
export function stripHash(path: string) {
127+
export function stripHash(path?: string | undefined) {
128+
if (!path) {
129+
return "";
130+
}
123131
const hashIndex = path.indexOf("#");
124132
if (hashIndex >= 0) {
125133
path = path.substring(0, hashIndex);

test/specs/substrings/slash.spec.ts

+53
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,57 @@ describe("$refs that include slashes", () => {
2121
},
2222
});
2323
});
24+
25+
it("should parse trailing spaces successfully", async () => {
26+
const parser = new $RefParser();
27+
const derefed = await parser.dereference({
28+
swagger: "2.0",
29+
paths: {
30+
somepath: {
31+
post: {
32+
$ref: "#/definitions/ABC ",
33+
},
34+
},
35+
},
36+
definitions: {
37+
"ABC ": {
38+
// tested removing space at the end of "ABC "
39+
type: "object",
40+
properties: {
41+
abc: {
42+
type: "string",
43+
},
44+
},
45+
title: "ABC ",
46+
},
47+
},
48+
});
49+
expect(derefed).to.deep.equal({
50+
swagger: "2.0",
51+
paths: {
52+
somepath: {
53+
post: {
54+
type: "object",
55+
properties: {
56+
abc: {
57+
type: "string",
58+
},
59+
},
60+
title: "ABC ",
61+
},
62+
},
63+
},
64+
definitions: {
65+
"ABC ": {
66+
type: "object",
67+
properties: {
68+
abc: {
69+
type: "string",
70+
},
71+
},
72+
title: "ABC ",
73+
},
74+
},
75+
});
76+
});
2477
});

0 commit comments

Comments
 (0)