Skip to content

Commit 6b73f33

Browse files
authored
test: expand extractPlaceholders tests (#413)
1 parent 9a558e2 commit 6b73f33

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

packages/openapi-code-generator/src/core/openapi-utils.spec.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,70 @@ describe("core/openapi-utils", () => {
5858
])
5959
})
6060

61-
// todo: expand tests for special characters / escaping
61+
it("handles special characters in placeholder names", () => {
62+
// template-expression-param-name = 1*( %x00-7A / %x7C / %x7E-10FFFF )
63+
// essentially everything except { and }
64+
65+
const specialNames = [
66+
"id:with-colon",
67+
"id@with-at",
68+
"id.with-dot",
69+
"id_with-underscore",
70+
"id~with-tilde",
71+
"id!with-bang",
72+
"id$with-dollar",
73+
"id&with-ampersand",
74+
"id'with-quote",
75+
"id(with-parens)",
76+
"id*with-asterisk",
77+
"id+with-plus",
78+
"id,with-comma",
79+
"id;with-semicolon",
80+
"id=with-equals",
81+
"id%20with-percent",
82+
"id中文", // Unicode
83+
]
84+
85+
for (const name of specialNames) {
86+
expect(extractPlaceholders(`/{${name}}`)).toStrictEqual([
87+
{
88+
placeholder: name,
89+
wholeString: `{${name}}`,
90+
},
91+
])
92+
}
93+
})
94+
95+
it("handles /, ?, and # in placeholder names as per ABNF", () => {
96+
// although these are forbidden in parameter values, the ABNF for the name allows them
97+
const names = ["name/with/slash", "name?with?question", "name#with#hash"]
98+
99+
for (const name of names) {
100+
expect(extractPlaceholders(`/{${name}}`)).toStrictEqual([
101+
{
102+
placeholder: name,
103+
wholeString: `{${name}}`,
104+
},
105+
])
106+
}
107+
})
108+
109+
it("does not extract empty placeholders", () => {
110+
// template-expression-param-name = 1*...
111+
expect(extractPlaceholders("/{}")).toStrictEqual([])
112+
})
113+
114+
it("handles multiple placeholders in a single path segment", () => {
115+
expect(extractPlaceholders("/{foo}{bar}")).toStrictEqual([
116+
{placeholder: "foo", wholeString: "{foo}"},
117+
{placeholder: "bar", wholeString: "{bar}"},
118+
])
119+
})
120+
121+
it("handles placeholders mixed with literals in a single path segment", () => {
122+
expect(extractPlaceholders("/prefix{foo}suffix")).toStrictEqual([
123+
{placeholder: "foo", wholeString: "{foo}"},
124+
])
125+
})
62126
})
63127
})

packages/openapi-code-generator/src/core/openapi-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export function getNameFromRef({$ref}: Reference, prefix: string): string {
1919
return prefix + name.replace(/[-.]+/g, "_")
2020
}
2121

22+
/**
23+
* Given an openapi path template, extract the variable placeholders
24+
* ref: {@link https://spec.openapis.org/oas/v3.2.0.html#path-templating path-templating}
25+
*/
2226
export function extractPlaceholders(
2327
it: string,
2428
): {wholeString: string; placeholder: string | undefined}[] {

0 commit comments

Comments
 (0)