Skip to content

Commit

Permalink
fix: Object variables not being correctly included in json body
Browse files Browse the repository at this point in the history
Interpolate will now fallback to the original object to collect
variable values. Objets will automaticly json encoded. And if
intererpolate is executed for the json body all variables will
be json encoded so strings get double quoted.

This should fix:
- usebruno#1910
- usebruno#1635
  • Loading branch information
Its-treason committed Mar 25, 2024
1 parent 5539cc1 commit a2e80a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
8 changes: 4 additions & 4 deletions packages/bruno-cli/src/runner/interpolate-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
});
});

const _interpolate = (str) => {
const _interpolate = (str, isJsonBody = false) => {
if (!str || !str.length || typeof str !== 'string') {
return str;
}
Expand All @@ -44,7 +44,7 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
}
};

return interpolate(str, combinedVars);
return interpolate(str, combinedVars, isJsonBody);
};

request.url = _interpolate(request.url);
Expand All @@ -60,14 +60,14 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
if (typeof request.data === 'object') {
try {
let parsed = JSON.stringify(request.data);
parsed = _interpolate(parsed);
parsed = _interpolate(parsed, true);
request.data = JSON.parse(parsed);
} catch (err) {}
}

if (typeof request.data === 'string') {
if (request.data.length) {
request.data = _interpolate(request.data);
request.data = _interpolate(request.data, true);
}
}
} else if (contentType === 'application/x-www-form-urlencoded') {
Expand Down
19 changes: 13 additions & 6 deletions packages/bruno-common/src/interpolate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@

import { flattenObject } from '../utils';

const interpolate = (str: string, obj: Record<string, any>): string => {
const interpolate = (str: string, obj: Record<string, any>, isJsonBody = false): string => {
if (!str || typeof str !== 'string' || !obj || typeof obj !== 'object') {
return str;
}

const patternRegex = /\{\{([^}]+)\}\}/g;
const flattenedObj = flattenObject(obj);
const result = str.replace(patternRegex, (match, placeholder) => {
const replacement = flattenedObj[placeholder];
return replacement !== undefined ? replacement : match;
});
return str.replace(patternRegex, (match, placeholder) => {
const replacement = flattenedObj[placeholder] || obj[placeholder];
// Return the original string so nothing gets replaced
if (replacement === undefined) {
return match;
}

return result;
// When inside json body everything must be encoded so string get double quotes
if (isJsonBody || typeof replacement === 'object') {
return JSON.stringify(replacement);
}
return replacement;
});
};

export default interpolate;
8 changes: 4 additions & 4 deletions packages/bruno-electron/src/ipc/network/interpolate-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
});
});

const _interpolate = (str) => {
const _interpolate = (str, isJsonBody = false) => {
if (!str || !str.length || typeof str !== 'string') {
return str;
}
Expand All @@ -44,7 +44,7 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
}
};

return interpolate(str, combinedVars);
return interpolate(str, combinedVars, isJsonBody);
};

request.url = _interpolate(request.url);
Expand All @@ -60,14 +60,14 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
if (typeof request.data === 'object') {
try {
let parsed = JSON.stringify(request.data);
parsed = _interpolate(parsed);
parsed = _interpolate(parsed, true);
request.data = JSON.parse(parsed);
} catch (err) {}
}

if (typeof request.data === 'string') {
if (request.data.length) {
request.data = _interpolate(request.data);
request.data = _interpolate(request.data, true);
}
}
} else if (contentType === 'application/x-www-form-urlencoded') {
Expand Down

0 comments on commit a2e80a7

Please sign in to comment.