Avoid mutating KQL query when validating it (#97081)
This commit is contained in:
parent
cb2cf67609
commit
787b493403
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue