From c3972d53f581d46c58053c51805469e146b72469 Mon Sep 17 00:00:00 2001 From: Michael Andresen Date: Wed, 9 Jul 2025 13:26:31 +0200 Subject: [PATCH] fix: properly encode parameters --- .../theme/ApiExplorer/buildPostmanRequest.ts | 80 ++++++------------- 1 file changed, 24 insertions(+), 56 deletions(-) diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/buildPostmanRequest.ts b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/buildPostmanRequest.ts index b3f884ae4..78e57744c 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/buildPostmanRequest.ts +++ b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/buildPostmanRequest.ts @@ -55,20 +55,10 @@ function setQueryParams(postman: sdk.Request, queryParams: Param[]) { } } - const decodedValue = decodeURI(param.value); - const tryJson = () => { - try { - return JSON.parse(decodedValue); - } catch (e) { - return false; - } - }; - - const jsonResult = tryJson(); - // Handle object values - if (jsonResult && typeof jsonResult === "object") { - if (param.style === "deepObject") { + if (param.style === "deepObject") { + const jsonResult = tryDecodeJsonParam(param.value); + if (jsonResult && typeof jsonResult === "object") { return Object.entries(jsonResult).map( ([key, val]) => new sdk.QueryParam({ @@ -76,7 +66,10 @@ function setQueryParams(postman: sdk.Request, queryParams: Param[]) { value: String(val), }) ); - } else if (param.explode) { + } + } else if (param.explode) { + const jsonResult = tryDecodeJsonParam(param.value); + if (jsonResult && typeof jsonResult === "object") { return Object.entries(jsonResult).map( ([key, val]) => new sdk.QueryParam({ @@ -94,17 +87,9 @@ function setQueryParams(postman: sdk.Request, queryParams: Param[]) { } } - // Handle boolean values - if (typeof decodedValue === "boolean") { - return new sdk.QueryParam({ - key: param.name, - value: decodedValue ? "true" : "false", - }); - } - // Parameter allows empty value: "/hello?extended" if (param.allowEmptyValue) { - if (decodedValue === "true") { + if (param.value === "true") { return new sdk.QueryParam({ key: param.name, value: null, @@ -150,18 +135,9 @@ function setPathParams(postman: sdk.Request, pathParams: Param[]) { }); } - const decodedValue = decodeURI(param.value); - const tryJson = () => { - try { - return JSON.parse(decodedValue); - } catch (e) { - return false; - } - }; + const jsonResult = tryDecodeJsonParam(param.value); - const jsonResult = tryJson(); - - if (typeof jsonResult === "object") { + if (jsonResult && typeof jsonResult === "object") { if (param.style === "matrix") { serializedValue = Object.entries(jsonResult) .map(([key, val]) => `;${key}=${val}`) @@ -172,7 +148,7 @@ function setPathParams(postman: sdk.Request, pathParams: Param[]) { .join(","); } } else { - serializedValue = decodedValue || `:${param.name}`; + serializedValue = param.value; } return new sdk.Variable({ @@ -191,17 +167,8 @@ function buildCookie(cookieParams: Param[]) { const cookies = cookieParams .map((param) => { if (param.value) { - const decodedValue = decodeURI(param.value as string); - const tryJson = () => { - try { - return JSON.parse(decodedValue); - } catch (e) { - return false; - } - }; - - const jsonResult = tryJson(); - if (typeof jsonResult === "object") { + const jsonResult = tryDecodeJsonParam(param.value as string); + if (jsonResult && typeof jsonResult === "object") { if (param.style === "form") { // Handle form style if (param.explode) { @@ -266,16 +233,9 @@ function setHeaders( headerParams.forEach((param) => { if (param.value) { - const decodedValue = decodeURI(param.value as string); - const tryJson = () => { - try { - return JSON.parse(decodedValue); - } catch (e) { - return false; - } - }; - - const jsonResult = tryJson(); + const jsonResult = Array.isArray(param.value) + ? param.value.map(tryDecodeJsonParam) + : tryDecodeJsonParam(param.value); if (Array.isArray(param.value)) { if (param.style === "simple") { if (param.explode) { @@ -324,6 +284,14 @@ function setHeaders( } } +function tryDecodeJsonParam(value: string): any { + try { + return JSON.parse(decodeURI(value)); + } catch (e) { + return false; + } +} + // TODO: this is all a bit hacky function setBody(clonedPostman: sdk.Request, body: Body) { if (clonedPostman.body === undefined) {