Skip to content

Commit

Permalink
[8.17] [Console] Fix wrongly split json data (#201115) (#201722)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.17`:
- [[Console] Fix wrongly split json data
(#201115)](#201115)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Ignacio
Rivas","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-22T10:20:58Z","message":"[Console]
Fix wrongly split json data
(#201115)","sha":"cc214cd331789e538f87f59e22eb069a5ef2963f","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Console","Team:Kibana
Management","release_note:skip","v9.0.0","backport:prev-minor","v8.16.0","v8.17.0","v8.18.0"],"number":201115,"url":"https://github.com/elastic/kibana/pull/201115","mergeCommit":{"message":"[Console]
Fix wrongly split json data
(#201115)","sha":"cc214cd331789e538f87f59e22eb069a5ef2963f"}},"sourceBranch":"main","suggestedTargetBranches":["8.17"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201115","number":201115,"mergeCommit":{"message":"[Console]
Fix wrongly split json data
(#201115)","sha":"cc214cd331789e538f87f59e22eb069a5ef2963f"}},{"branch":"8.16","label":"v8.16.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/201721","number":201721,"state":"OPEN"},{"branch":"8.17","label":"v8.17.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.18.0","labelRegex":"^v8.18.0$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/201339","number":201339,"state":"MERGED","mergeCommit":{"sha":"e86c69cfdfdf67371322893d0c37d2ef6b79ac3c","message":"[8.x]
[Console] Fix wrongly split json data (#201115) (#201339)\n\n#
Backport\n\nThis will backport the following commits from `main` to
`8.x`:\n- [[Console] Fix wrongly split json
data\n(#201115)](https://github.com/elastic/kibana/pull/201115)\n\n<!---
Backport version: 9.4.3 -->\n\n### Questions ?\nPlease refer to the
[Backport
tool\ndocumentation](https://github.com/sqren/backport)\n\n<!--BACKPORT
[{\"author\":{\"name\":\"Ignacio\nRivas\",\"email\":\"[email protected]\"},\"sourceCommit\":{\"committedDate\":\"2024-11-22T10:20:58Z\",\"message\":\"[Console]\nFix
wrongly split json
data\n(#201115)\",\"sha\":\"cc214cd331789e538f87f59e22eb069a5ef2963f\",\"branchLabelMapping\":{\"^v9.0.0$\":\"main\",\"^v8.18.0$\":\"8.x\",\"^v(\\\\d+).(\\\\d+).\\\\d+$\":\"$1.$2\"}},\"sourcePullRequest\":{\"labels\":[\"Feature:Console\",\"Team:Kibana\nManagement\",\"release_note:skip\",\"v9.0.0\",\"backport:prev-minor\"],\"title\":\"[Console]\nFix
wrongly split
json\ndata\",\"number\":201115,\"url\":\"https://github.com/elastic/kibana/pull/201115\",\"mergeCommit\":{\"message\":\"[Console]\nFix
wrongly split json
data\n(#201115)\",\"sha\":\"cc214cd331789e538f87f59e22eb069a5ef2963f\"}},\"sourceBranch\":\"main\",\"suggestedTargetBranches\":[],\"targetPullRequestStates\":[{\"branch\":\"main\",\"label\":\"v9.0.0\",\"branchLabelMappingKey\":\"^v9.0.0$\",\"isSourceBranch\":true,\"state\":\"MERGED\",\"url\":\"https://github.com/elastic/kibana/pull/201115\",\"number\":201115,\"mergeCommit\":{\"message\":\"[Console]\nFix
wrongly split json
data\n(#201115)\",\"sha\":\"cc214cd331789e538f87f59e22eb069a5ef2963f\"}}]}]\nBACKPORT-->\n\nCo-authored-by:
Ignacio Rivas <[email protected]>"}}]}] BACKPORT-->
  • Loading branch information
sabarasaba authored Nov 26, 2024
1 parent 1651d1b commit 62c1d11
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,17 @@ describe('requests_utils', () => {
expect(request).toEqual({ method: 'GET', url: '_search', data: ['{\n "query": {}\n}'] });
});

it('correctly handles nested braces', () => {
const content = ['GET _search', '{', ' "query": "{a} {b}"', '}', '{', ' "query": {}', '}'];
const model = getMockModel(content);
const request = getRequestFromEditor(model, 1, 7);
expect(request).toEqual({
method: 'GET',
url: '_search',
data: ['{\n "query": "{a} {b}"\n}', '{\n "query": {}\n}'],
});
});

it('works for several request bodies', () => {
const content = ['GET _search', '{', ' "query": {}', '}', '{', ' "query": {}', '}'];
const model = getMockModel(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,23 +271,61 @@ const replaceVariables = (
return text;
};

/**
* Splits a concatenated string of JSON objects into individual JSON objects.
*
* This function takes a string containing one or more JSON objects concatenated together,
* separated by optional whitespace, and splits them into an array of individual JSON strings.
* It ensures that nested objects and strings containing braces do not interfere with the splitting logic.
*
* Example inputs:
* - '{ "query": "test"} { "query": "test" }' -> ['{ "query": "test"}', '{ "query": "test" }']
* - '{ "query": "test"}' -> ['{ "query": "test"}']
* - '{ "query": "{a} {b}"}' -> ['{ "query": "{a} {b}"}']
*
*/
const splitDataIntoJsonObjects = (dataString: string): string[] => {
const jsonSplitRegex = /}\s*{/;
if (dataString.match(jsonSplitRegex)) {
return dataString.split(jsonSplitRegex).map((part, index, parts) => {
let restoredBracketsString = part;
// add an opening bracket to all parts except the 1st
if (index > 0) {
restoredBracketsString = `{${restoredBracketsString}`;
const jsonObjects = [];
// Tracks the depth of nested braces
let depth = 0;
// Holds the current JSON object as we iterate
let currentObject = '';
// Tracks whether the current position is inside a string
let insideString = false;

// Iterate through each character in the input string
for (let i = 0; i < dataString.length; i++) {
const char = dataString[i];
// Append the character to the current JSON object string
currentObject += char;

// If the character is a double quote and it is not escaped, toggle the `insideString` state
if (char === '"' && dataString[i - 1] !== '\\') {
insideString = !insideString;
} else if (!insideString) {
// Only modify depth if not inside a string

if (char === '{') {
depth++;
} else if (char === '}') {
depth--;
}
// add a closing bracket to all parts except the last
if (index < parts.length - 1) {
restoredBracketsString = `${restoredBracketsString}}`;

// If depth is zero, we have completed a JSON object
if (depth === 0) {
jsonObjects.push(currentObject.trim());
currentObject = '';
}
return restoredBracketsString;
});
}
}
return [dataString];

// If there's remaining data in currentObject, add it as the last JSON object
if (currentObject.trim()) {
jsonObjects.push(currentObject.trim());
}

// Filter out any empty strings from the result array
return jsonObjects.filter((obj) => obj !== '');
};

const cleanUpWhitespaces = (line: string): string => {
Expand Down

0 comments on commit 62c1d11

Please sign in to comment.