eui icon indicating copy to clipboard operation
eui copied to clipboard

[EuiSearchBar] Allow bad query string

Open nickofthyme opened this issue 4 years ago • 4 comments

In kibana I am updating the advanced settings page to sync the search text to the query URL search params (https://github.com/elastic/kibana/pull/81829). This allows the URL to possibly be formatted incorrectly. Thus, running Query.parse('category:(accessibility))') will throw an error.

I am testing Query.parse before passing to EuiSearchBar and displaying a warning toast in case of an error. But it would be nice to still update the value of the search bar input element. The only workaround is to set the query to ''.

I would like to propose the query prop as a string allows an incorrectly formatted (aka bad) query string. Currently, passing a bad string will throw an error inside the EuiSearchBar component with no checks on the parsing failure.

https://github.com/elastic/eui/blob/05b3de41656f9e69e0517c56c5344c2b3c035b1d/src/components/search_bar/search_bar.tsx#L101-L103

nickofthyme avatar Dec 15 '20 17:12 nickofthyme

Looks like the initial state construction for EuiSearchBar needs to get a try/catch that correctly populates the query text & any error.

https://github.com/elastic/eui/blob/bca59468fda3fd29e5176d9ddd375c26b918651f/src/components/search_bar/search_bar.tsx#L122-L127

chandlerprall avatar Dec 15 '20 19:12 chandlerprall

@chandlerprall, should I just pass an empty string in case if query.text is undefined?

queryText: query.text | '' 

Also, I'm not sure if I correctly understand the issue, @nickofthyme, can you please give an example of a bad query string so I can reproduce the error you mentioned?

Dishebh avatar Dec 16 '20 08:12 Dishebh

@Dishebh Yeah, I think '' should be the fallback query when no Query nor string is passed to either defaultQuery or query. I'm not sure if that's already handled.

This issue is focused on the bad query. An example would be 'key:())' note the extra ) at the end.

nickofthyme avatar Dec 16 '20 15:12 nickofthyme

should I just pass an empty string in case if query.text is undefined?

Should be able to use the provided string in defaultQuery in the error state, something like:

try {
  const query = parseQuery(props.defaultQuery || props.query, props);
  this.state = {
    query,
    queryText: query.text,
    error: null,
  };
} catch(e) {
  this.state = {
    query: parseQuery(''),
    queryText: props.defaultQuery, // I believe this will error in TypeScript, need to very it is a string and still fallback to empty string
    error: e.toString(), // maybe e.message? maybe just e?
  };
}

chandlerprall avatar Dec 16 '20 16:12 chandlerprall

👋 Hi there - this issue hasn't had any activity in 6 months. If the EUI team has not explicitly expressed that this is something on our roadmap, it's unlikely that we'll pick this issue up. We would sincerely appreciate a PR/community contribution if this is something that matters to you! If not, and there is no further activity on this issue for another 6 months (i.e. it's stale for over a year), the issue will be auto-closed.

github-actions[bot] avatar Sep 22 '25 00:09 github-actions[bot]