Skip to content

Commit 22d8a9d

Browse files
committed
expand coverage for cookie params
1 parent 19da1af commit 22d8a9d

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,52 @@ function setPathParams(postman: sdk.Request, pathParams: Param[]) {
187187
function buildCookie(cookieParams: Param[]) {
188188
const cookies = cookieParams
189189
.map((param) => {
190-
if (param.value && !Array.isArray(param.value)) {
191-
return new sdk.Cookie({
192-
// TODO: Is this right?
193-
path: "",
194-
domain: "",
195-
key: param.name,
196-
value: param.value,
197-
});
190+
if (param.value) {
191+
const decodedValue = decodeURI(param.value as string);
192+
const tryJson = () => {
193+
try {
194+
return JSON.parse(decodedValue);
195+
} catch (e) {
196+
return false;
197+
}
198+
};
199+
200+
const jsonResult = tryJson();
201+
if (typeof jsonResult === "object") {
202+
if (param.style === "form") {
203+
// Handle form style
204+
if (param.explode) {
205+
// Serialize each key-value pair as a separate cookie
206+
return Object.entries(jsonResult).map(
207+
([key, val]) =>
208+
new sdk.Cookie({
209+
key: key,
210+
value: val,
211+
})
212+
);
213+
} else {
214+
// Serialize the object as a single cookie with key-value pairs joined by commas
215+
return new sdk.Cookie({
216+
key: param.name,
217+
value: Object.entries(jsonResult)
218+
.map(([key, val]) => `${key},${val}`)
219+
.join(","),
220+
});
221+
}
222+
}
223+
} else {
224+
// Handle scalar values
225+
return new sdk.Cookie({
226+
key: param.name,
227+
value: param.value,
228+
});
229+
}
198230
}
199231
return undefined;
200232
})
201-
.filter((item): item is sdk.Cookie => item !== undefined);
233+
.flat() // Flatten the array in case of nested arrays from map
234+
.filter((item) => item !== undefined);
235+
202236
const list = new sdk.CookieList(null, cookies);
203237
return list.toString();
204238
}

0 commit comments

Comments
 (0)