Skip to content

Commit

Permalink
fix destructive recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Feb 1, 2021
1 parent 119c767 commit eef541c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
54 changes: 54 additions & 0 deletions src/plugins/data/common/search/search_source/search_source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,33 @@ describe('SearchSource', () => {
],
}
`);

// calling twice gives the same result: no searchSources in the hierarchy were modified
expect(searchSource.getFields(RECURSE)).toMatchInlineSnapshot(`
Object {
"aggs": 5,
"filter": Array [
Object {
"meta": Object {
"alias": null,
"disabled": false,
"index": "d180cae0-60c3-11eb-8569-bd1f5ed24bc9",
"negate": false,
"params": Object {},
},
"query": Object {
"range": Object {
"@date": Object {
"format": "strict_date_optional_time",
"gte": "2016-01-27T18:11:05.010Z",
"lte": "2021-01-27T18:11:05.010Z",
},
},
},
},
],
}
`);
});

test('recurses parents to get the entire filters: function filter', () => {
Expand Down Expand Up @@ -192,6 +219,33 @@ describe('SearchSource', () => {
],
}
`);

// calling twice gives the same result: no double-added filters
expect(searchSource.getFields(RECURSE)).toMatchInlineSnapshot(`
Object {
"aggs": 5,
"filter": Array [
Object {
"meta": Object {
"alias": null,
"disabled": false,
"index": "d180cae0-60c3-11eb-8569-bd1f5ed24bc9",
"negate": false,
"params": Object {},
},
"query": Object {
"range": Object {
"@date": Object {
"format": "strict_date_optional_time",
"gte": "2016-01-27T18:11:05.010Z",
"lte": "2021-01-27T18:11:05.010Z",
},
},
},
},
],
}
`);
});
});

Expand Down
9 changes: 6 additions & 3 deletions src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,13 @@ export class SearchSource {
}
}

// set filter to the array of non-function-type filters
this.setField('filter', thisFilter);
// add combined filters to the fields
const thisFields = {
...this.fields,
filter: thisFilter,
};

return { ...parentFields, ...this.fields };
return { ...parentFields, ...thisFields };
}
}
return { ...this.fields };
Expand Down

0 comments on commit eef541c

Please sign in to comment.