Skip to content

Commit

Permalink
feat(Json-Path): Update to be backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Archangelza1 authored and Brendan Sahli committed Aug 14, 2024
1 parent a88da12 commit fedd157
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/commons/engine_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,8 @@ function dummyParser(body, callback) {
return callback(null, body);
}

function extractJSONPath(doc, expr) {
// doc is a JSON object
function extractJSONPath(doc, expr, opts) {
// typeof null is 'object' hence the explicit check here
if (typeof doc !== 'object' || doc === null) {
return '';
Expand All @@ -579,7 +580,7 @@ function extractJSONPath(doc, expr) {
let results;

try {
results = jsonpath({ path: expr, json: doc, wrap: false });
results = jsonpath({ path: expr, json: doc, wrap: opts.multiple ?? true });
} catch (queryErr) {
debug(queryErr);
}
Expand All @@ -588,7 +589,15 @@ function extractJSONPath(doc, expr) {
return '';
}

return results;
if (opts.multiple === false) {
return results;
}

if (results.length > 1) {
return results[randomInt(0, results.length - 1)];
} else {
return results[0];
}
}

// doc is a string or an object (body parsed by Request when headers indicate JSON)
Expand Down

0 comments on commit fedd157

Please sign in to comment.