Skip to content

Commit 3e27346

Browse files
michaelandresenMichael Andresen
andauthored
fix: properly encode parameters (#1202)
Co-authored-by: Michael Andresen <[email protected]>
1 parent cde3c60 commit 3e27346

File tree

1 file changed

+24
-56
lines changed

1 file changed

+24
-56
lines changed

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/buildPostmanRequest.ts

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,21 @@ function setQueryParams(postman: sdk.Request, queryParams: Param[]) {
5555
}
5656
}
5757

58-
const decodedValue = decodeURI(param.value);
59-
const tryJson = () => {
60-
try {
61-
return JSON.parse(decodedValue);
62-
} catch (e) {
63-
return false;
64-
}
65-
};
66-
67-
const jsonResult = tryJson();
68-
6958
// Handle object values
70-
if (jsonResult && typeof jsonResult === "object") {
71-
if (param.style === "deepObject") {
59+
if (param.style === "deepObject") {
60+
const jsonResult = tryDecodeJsonParam(param.value);
61+
if (jsonResult && typeof jsonResult === "object") {
7262
return Object.entries(jsonResult).map(
7363
([key, val]) =>
7464
new sdk.QueryParam({
7565
key: `${param.name}[${key}]`,
7666
value: String(val),
7767
})
7868
);
79-
} else if (param.explode) {
69+
}
70+
} else if (param.explode) {
71+
const jsonResult = tryDecodeJsonParam(param.value);
72+
if (jsonResult && typeof jsonResult === "object") {
8073
return Object.entries(jsonResult).map(
8174
([key, val]) =>
8275
new sdk.QueryParam({
@@ -94,17 +87,9 @@ function setQueryParams(postman: sdk.Request, queryParams: Param[]) {
9487
}
9588
}
9689

97-
// Handle boolean values
98-
if (typeof decodedValue === "boolean") {
99-
return new sdk.QueryParam({
100-
key: param.name,
101-
value: decodedValue ? "true" : "false",
102-
});
103-
}
104-
10590
// Parameter allows empty value: "/hello?extended"
10691
if (param.allowEmptyValue) {
107-
if (decodedValue === "true") {
92+
if (param.value === "true") {
10893
return new sdk.QueryParam({
10994
key: param.name,
11095
value: null,
@@ -150,18 +135,9 @@ function setPathParams(postman: sdk.Request, pathParams: Param[]) {
150135
});
151136
}
152137

153-
const decodedValue = decodeURI(param.value);
154-
const tryJson = () => {
155-
try {
156-
return JSON.parse(decodedValue);
157-
} catch (e) {
158-
return false;
159-
}
160-
};
138+
const jsonResult = tryDecodeJsonParam(param.value);
161139

162-
const jsonResult = tryJson();
163-
164-
if (typeof jsonResult === "object") {
140+
if (jsonResult && typeof jsonResult === "object") {
165141
if (param.style === "matrix") {
166142
serializedValue = Object.entries(jsonResult)
167143
.map(([key, val]) => `;${key}=${val}`)
@@ -172,7 +148,7 @@ function setPathParams(postman: sdk.Request, pathParams: Param[]) {
172148
.join(",");
173149
}
174150
} else {
175-
serializedValue = decodedValue || `:${param.name}`;
151+
serializedValue = param.value;
176152
}
177153

178154
return new sdk.Variable({
@@ -191,17 +167,8 @@ function buildCookie(cookieParams: Param[]) {
191167
const cookies = cookieParams
192168
.map((param) => {
193169
if (param.value) {
194-
const decodedValue = decodeURI(param.value as string);
195-
const tryJson = () => {
196-
try {
197-
return JSON.parse(decodedValue);
198-
} catch (e) {
199-
return false;
200-
}
201-
};
202-
203-
const jsonResult = tryJson();
204-
if (typeof jsonResult === "object") {
170+
const jsonResult = tryDecodeJsonParam(param.value as string);
171+
if (jsonResult && typeof jsonResult === "object") {
205172
if (param.style === "form") {
206173
// Handle form style
207174
if (param.explode) {
@@ -266,16 +233,9 @@ function setHeaders(
266233

267234
headerParams.forEach((param) => {
268235
if (param.value) {
269-
const decodedValue = decodeURI(param.value as string);
270-
const tryJson = () => {
271-
try {
272-
return JSON.parse(decodedValue);
273-
} catch (e) {
274-
return false;
275-
}
276-
};
277-
278-
const jsonResult = tryJson();
236+
const jsonResult = Array.isArray(param.value)
237+
? param.value.map(tryDecodeJsonParam)
238+
: tryDecodeJsonParam(param.value);
279239
if (Array.isArray(param.value)) {
280240
if (param.style === "simple") {
281241
if (param.explode) {
@@ -324,6 +284,14 @@ function setHeaders(
324284
}
325285
}
326286

287+
function tryDecodeJsonParam(value: string): any {
288+
try {
289+
return JSON.parse(decodeURI(value));
290+
} catch (e) {
291+
return false;
292+
}
293+
}
294+
327295
// TODO: this is all a bit hacky
328296
function setBody(clonedPostman: sdk.Request, body: Body) {
329297
if (clonedPostman.body === undefined) {

0 commit comments

Comments
 (0)