Avoid mutating KQL query when validating it (#97081)

This commit is contained in:
Pierre Gayvallet 2021-04-18 23:07:36 +02:00 committed by GitHub
parent cb2cf67609
commit 787b493403
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View file

@ -6,6 +6,7 @@
* Side Public License, v 1.
*/
import { cloneDeep } from 'lodash';
// @ts-expect-error no ts
import { esKuery } from '../../es_query';
@ -105,6 +106,22 @@ describe('Filter Utils', () => {
)
).toEqual(esKuery.fromKueryExpression('foo.title: "best"'));
});
test('does not mutate the input KueryNode', () => {
const input = esKuery.nodeTypes.function.buildNode(
'is',
`foo.attributes.title`,
'best',
true
);
const inputCopy = cloneDeep(input);
validateConvertFilterToKueryNode(['foo'], input, mockMappings);
expect(input).toEqual(inputCopy);
});
test('Validate a simple KQL expression filter', () => {
expect(
validateConvertFilterToKueryNode(['foo'], 'foo.attributes.title: "best"', mockMappings)

View file

@ -7,11 +7,12 @@
*/
import { set } from '@elastic/safer-lodash-set';
import { get } from 'lodash';
import { get, cloneDeep } from 'lodash';
import { SavedObjectsErrorHelpers } from './errors';
import { IndexMapping } from '../../mappings';
// @ts-expect-error no ts
import { esKuery } from '../../es_query';
type KueryNode = any;
const astFunctionType = ['is', 'range', 'nested'];
@ -23,7 +24,7 @@ export const validateConvertFilterToKueryNode = (
): KueryNode | undefined => {
if (filter && indexMapping) {
const filterKueryNode =
typeof filter === 'string' ? esKuery.fromKueryExpression(filter) : filter;
typeof filter === 'string' ? esKuery.fromKueryExpression(filter) : cloneDeep(filter);
const validationFilterKuery = validateFilterKueryNode({
astFilter: filterKueryNode,