Skip to content

fix: properly encode parameters #1202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,21 @@ 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({
key: `${param.name}[${key}]`,
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({
Expand All @@ -94,17 +87,9 @@ function setQueryParams(postman: sdk.Request, queryParams: Param[]) {
}
}

// Handle boolean values
Copy link
Contributor Author

@michaelandresen michaelandresen Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explanation: decodedValue is a constant value defined in Line 58 an therefore of type "string".

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,
Expand Down Expand Up @@ -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}`)
Expand All @@ -172,7 +148,7 @@ function setPathParams(postman: sdk.Request, pathParams: Param[]) {
.join(",");
}
} else {
serializedValue = decodedValue || `:${param.name}`;
serializedValue = param.value;
}

return new sdk.Variable({
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Loading