diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js index 0d04f3b8e372f..333064d9712d3 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js @@ -617,5 +617,24 @@ curl -XGET "http://localhost:5620/api/spaces/space" -H \"kbn-xsrf: reporting\"`. expect(curl).toContain('valueA'); expect(curl).toContain('valueB'); }); + + it('should replace variables in bulk request', async () => { + storage.set('variables', [ + { name: 'exampleVariableA', value: 'valueA' }, + { name: 'exampleVariableB', value: 'valueB' }, + ]); + input + ?.getCoreEditor() + .setValue( + 'POST _bulk\n{"index": {"_id": "0"}}\n{"field" : "${exampleVariableA}"}\n{"index": {"_id": "1"}}\n{"field" : "${exampleVariableB}"}\n', + false + ); + const curl = await input.getRequestsAsCURL('http://localhost:9200', { + start: { lineNumber: 1 }, + end: { lineNumber: 4 }, + }); + expect(curl).toContain('valueA'); + expect(curl).toContain('valueB'); + }); }); }); diff --git a/src/plugins/console/public/lib/row_parser.ts b/src/plugins/console/public/lib/row_parser.ts index fe369e71fb799..6412a013ff71e 100644 --- a/src/plugins/console/public/lib/row_parser.ts +++ b/src/plugins/console/public/lib/row_parser.ts @@ -41,18 +41,6 @@ export default class RowParser { } let line = (this.editor.getLineValue(lineNumber) || '').trim(); - // Check if the line has variables, depending on the request type, (e.g. single line, multi doc requests) return the correct mode - if (line && /(\${\w+})/.test(line)) { - lineNumber++; - line = (this.editor.getLineValue(lineNumber) || '').trim(); - - if (line.startsWith('{')) { - return MODE.REQUEST_START; - } - // next line is another request - // eslint-disable-next-line no-bitwise - return MODE.REQUEST_START | MODE.REQUEST_END; - } if (!line || line.startsWith('#') || line.startsWith('//') || line.startsWith('/*')) { return MODE.BETWEEN_REQUESTS; } // empty line or a comment waiting for a new req to start diff --git a/src/plugins/console/public/lib/utils/index.ts b/src/plugins/console/public/lib/utils/index.ts index 996384e07939e..62074e3b19208 100644 --- a/src/plugins/console/public/lib/utils/index.ts +++ b/src/plugins/console/public/lib/utils/index.ts @@ -127,6 +127,11 @@ export const replaceVariables = ( const bodyRegexTripleQuotes = /([\\"]?)"""\${(\w+)}"""(?!")/g; return requests.map((req) => { + // safeguard - caller passes any[] from editor's getRequestsInRange() as requests + if (!req || !req.url || !req.data) { + return req; + } + if (urlRegex.test(req.url)) { req.url = req.url.replaceAll(urlRegex, (match, key) => { const variable = variables.find(({ name }) => name === key); @@ -135,9 +140,9 @@ export const replaceVariables = ( }); } - if (req.data && req.data.length) { - if (bodyRegexSingleQuote.test(req.data[0])) { - const data = req.data[0].replaceAll(bodyRegexSingleQuote, (match, lookbehind, key) => { + req.data = req.data.map((data) => { + if (bodyRegexSingleQuote.test(data)) { + data = data.replaceAll(bodyRegexSingleQuote, (match, lookbehind, key) => { const variable = variables.find(({ name }) => name === key); if (!lookbehind && variable) { @@ -172,20 +177,20 @@ export const replaceVariables = ( return match; }); - req.data = [data]; } - if (bodyRegexTripleQuotes.test(req.data[0])) { - const data = req.data[0].replaceAll(bodyRegexTripleQuotes, (match, lookbehind, key) => { + if (bodyRegexTripleQuotes.test(data)) { + data = data.replaceAll(bodyRegexTripleQuotes, (match, lookbehind, key) => { const variable = variables.find(({ name }) => name === key); return !lookbehind && variable?.value ? '""' + JSON.stringify(variable?.value) + '""' : match; }); - req.data = [data]; } - } + + return data; + }); return req; }); diff --git a/src/plugins/console/public/lib/utils/utils.test.js b/src/plugins/console/public/lib/utils/utils.test.js index 7788aedeb7829..416f7c9dbb67d 100644 --- a/src/plugins/console/public/lib/utils/utils.test.js +++ b/src/plugins/console/public/lib/utils/utils.test.js @@ -318,5 +318,29 @@ describe('Utils class', () => { } ); }); + + it('should replace variables in bulk request', () => { + testVariables( + { + url: '${v13}/_bulk', + data: [ + '{"index": {"_id": "0"}}', + '{\n "f": "${v13}"\n}', + '{"index": {"_id": "1"}}', + '{\n "f": "${v13}"\n}', + ], + }, + { name: 'v13', value: 'test' }, + { + url: 'test/_bulk', + data: [ + '{"index": {"_id": "0"}}', + '{\n "f": "test"\n}', + '{"index": {"_id": "1"}}', + '{\n "f": "test"\n}', + ], + } + ); + }); }); });