Skip to content

Commit

Permalink
do not throw when query parsing fails (#85970) (#86004)
Browse files Browse the repository at this point in the history
pgayvallet authored Dec 16, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1adf949 commit f2f324f
Showing 3 changed files with 53 additions and 8 deletions.
27 changes: 22 additions & 5 deletions src/plugins/saved_objects_tagging_oss/public/api.ts
Original file line number Diff line number Diff line change
@@ -126,26 +126,42 @@ export interface SavedObjectsTaggingApiUi {
* Parse given query using EUI's `Query` syntax, and return the search term and the tag references
* to be used when using the `_find` API to retrieve the filtered objects.
*
* @remark if the query cannot be parsed, `searchTerm` will contain the raw query value, and `valid`
* will be `false`
*
* @param query The query to parse
* @param options see {@link ParseSearchQueryOptions}
*
* @example
* ```typescript
* parseSearchQuery('(tag:(tag-1 or tag-2) some term', { useNames: true })
* parseSearchQuery('tag:(tag-1 or tag-2) some term', { useNames: true })
* >>
* {
* searchTerm: 'some term',
* tagReferences: [{type: 'tag', id: 'tag-1-id'}, {type: 'tag', id: 'tag-2-id'}]
* tagReferences: [{type: 'tag', id: 'tag-1-id'}, {type: 'tag', id: 'tag-2-id'}],
* valid: true,
* }
* ```
*
* @example
* ```typescript
* parseSearchQuery('(tagging:(some-tag-uuid or some-other-tag-uuid) some term', { tagClause: 'tagging' })
* parseSearchQuery('tagging:(some-tag-uuid or some-other-tag-uuid) some term', { tagClause: 'tagging' })
* >>
* {
* searchTerm: 'some term',
* tagReferences: [{type: 'tag', id: 'some-tag-uuid'}, {type: 'tag', id: 'some-other-tag-uuid'}]
* tagReferences: [{type: 'tag', id: 'some-tag-uuid'}, {type: 'tag', id: 'some-other-tag-uuid'}],
* valid: true,
* }
* ```
*
* @example
* ```typescript
* parseSearchQuery('tag:(tag-1) [foo]')
* >>
* {
* searchTerm: 'tag:(tag-1) [foo]',
* tagReferences: [],
* valid: false,
* }
* ```
*/
@@ -282,7 +298,8 @@ export interface GetSearchBarFilterOptions {
*/
export interface ParsedSearchQuery {
searchTerm: string;
tagReferences?: SavedObjectsFindOptionsReference[];
tagReferences: SavedObjectsFindOptionsReference[];
valid: boolean;
}

/**
Original file line number Diff line number Diff line change
@@ -36,7 +36,18 @@ describe('parseSearchQuery', () => {

expect(parseSearchQuery(searchTerm)).toEqual({
searchTerm,
tagReferences: undefined,
tagReferences: [],
valid: true,
});
});

it('returns the raw search term when the syntax is not valid', () => {
const searchTerm = 'tag:id-1 [search term]';

expect(parseSearchQuery(searchTerm)).toEqual({
searchTerm,
tagReferences: [],
valid: false,
});
});

@@ -46,6 +57,7 @@ describe('parseSearchQuery', () => {
expect(parseSearchQuery(searchTerm, { useName: false })).toEqual({
searchTerm: 'my search term',
tagReferences: [tagRef('id-1'), tagRef('id-2')],
valid: true,
});
});

@@ -55,6 +67,7 @@ describe('parseSearchQuery', () => {
expect(parseSearchQuery(searchTerm, { useName: true })).toEqual({
searchTerm: 'my search term',
tagReferences: [tagRef('id-1'), tagRef('id-2')],
valid: true,
});
});

@@ -64,6 +77,7 @@ describe('parseSearchQuery', () => {
expect(parseSearchQuery(searchTerm, { tagField: 'custom' })).toEqual({
searchTerm: 'my search term',
tagReferences: [tagRef('id-1'), tagRef('id-2')],
valid: true,
});
});

@@ -73,6 +87,7 @@ describe('parseSearchQuery', () => {
expect(parseSearchQuery(searchTerm, { useName: true })).toEqual({
searchTerm: 'my search term',
tagReferences: [tagRef('id-1')],
valid: true,
});
});
});
Original file line number Diff line number Diff line change
@@ -20,13 +20,25 @@ export const buildParseSearchQuery = ({
cache,
}: BuildParseSearchQueryOptions): SavedObjectsTaggingApiUi['parseSearchQuery'] => {
return (query: string, { tagField = 'tag', useName = true }: ParseSearchQueryOptions = {}) => {
const parsed = Query.parse(query);
let parsed: Query;

try {
parsed = Query.parse(query);
} catch (e) {
return {
searchTerm: query,
tagReferences: [],
valid: false,
};
}

// from other usages of `Query.parse` in the codebase, it seems that
// for empty term, the parsed query can be undefined, even if the type def state otherwise.
if (!query) {
return {
searchTerm: '',
tagReferences: [],
valid: true,
};
}

@@ -58,7 +70,8 @@ export const buildParseSearchQuery = ({

return {
searchTerm,
tagReferences: tagReferences.length ? tagReferences : undefined,
tagReferences,
valid: true,
};
};
};

0 comments on commit f2f324f

Please sign in to comment.