From 79dc2b46d2b55cfc6079ff34f16cb04b0a495ee2 Mon Sep 17 00:00:00 2001 From: Matthias Behr Date: Sat, 2 Jan 2021 19:24:20 +0100 Subject: [PATCH] fix(restquery): support non quoted attributes Non quoted but URI encoded attributes are now supported as well. At the same time to reduce the likelyhood of name clashes only ${attribute.*} will be replaced. --- src/webview/src/util.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/webview/src/util.js b/src/webview/src/util.js index 69cef7f..9a54d47 100644 --- a/src/webview/src/util.js +++ b/src/webview/src/util.js @@ -80,7 +80,7 @@ export async function triggerRestQueryDetails(dataSourceObj, attributes) { try { const reqSource = dataSourceObj.source; - const replaceAttr = (match, p1, offset) => { + const replaceAttr = (match, p1, offset, wrapStringInQuotes) => { //console.log(`replacing '${match}' '${p1}' at offset ${offset}`); if (p1.startsWith("attributes.")) { // currently only attribute supported let attrName = p1.slice(p1.indexOf('.') + 1); @@ -97,7 +97,7 @@ export async function triggerRestQueryDetails(dataSourceObj, attributes) { const attrKeyValue = Array.isArray(attrValue) ? attrValue.map(e => attrKey ? e[attrKey] : e) : attrKey ? attrValue[attrKey] : attrValue; if (typeof attrKeyValue === 'string') { // console.log(`attrKeyValue='${attrKeyValue}'`, attribute); - return `"${attrKeyValue}"`; // need to wrap the string in "" + return wrapStringInQuotes ? `"${attrKeyValue}"` : attrKeyValue; } else { // console.log(`attrKeyValue='${JSON.stringify(attrKeyValue)}'`, attribute); return JSON.stringify(attrKeyValue); @@ -105,14 +105,22 @@ export async function triggerRestQueryDetails(dataSourceObj, attributes) { } return ``; } - return ``; + return wrapStringInQuotes ? `"${p1}"` : p1; }; let requestStr = ''; if (typeof reqSource === 'string') { - requestStr = reqSource.replace(/"\$\{(.*?)\}"/g, (match, p1, offset) => replaceAttr(match, p1, offset)); + // rules are: + // "${attributes.name}" + // -> " JSON representation otherwise (e.g. for arrays [...]) + // problem is that arrays should not be "" quoted. + + requestStr = reqSource.replace(/"\$\{(.*?)\}"/g, (match, p1, offset) => replaceAttr(match, p1, offset, true)); // replace the URI encoded ones as well, but uri encode them then: requestStr = requestStr.replace(/%22%24%7B(.*?)%7D%22/g, (match, p1, offset) => encodeURIComponent(replaceAttr(match, p1, offset, true))); + // support uri encoded attrs like ${attributes.*} (w.o. being double enquoted) as well: + requestStr = requestStr.replace(/%24%7B(.*?)%7D/g, (match, p1, offset) => encodeURIComponent(replaceAttr(match, p1, offset, false))); } const res = await triggerRestQuery(requestStr);