Skip to content

Commit

Permalink
Fixes how to process bulk request body with variables (#162745)
Browse files Browse the repository at this point in the history
Fixes #162744

## Summary

This PR fixes a problem in a way to process bulk request body with
variables.

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

## Release note

Fixes a problem in a way to process bulk request body with variables.

---------

Co-authored-by: Yulia Čech <[email protected]>
Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Dzmitry Lemechko <[email protected]>
  • Loading branch information
4 people authored Aug 3, 2023
1 parent a4d3c02 commit 357b7f9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
12 changes: 0 additions & 12 deletions src/plugins/console/public/lib/row_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions src/plugins/console/public/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
});
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/console/public/lib/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}',
],
}
);
});
});
});

0 comments on commit 357b7f9

Please sign in to comment.