Skip to content

Commit

Permalink
Merge pull request #4394 from BasLee/fix-or-study-search-query
Browse files Browse the repository at this point in the history
Fix infinite loop when study search query consists of single "or"
  • Loading branch information
alisman authored Oct 19, 2022
2 parents a34c4a3 + a72e9e1 commit 153a877
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/shared/components/query/filteredSearch/SearchClause.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { CancerTreeNodeFields } from 'shared/lib/query/textQueryUtils';
import _ from 'lodash';
import { CancerTreeNode } from 'shared/components/query/CancerStudyTreeData';
import { Phrase } from 'shared/components/query/filteredSearch/Phrase';

export const FILTER_SEPARATOR = `:`;
Expand Down
14 changes: 14 additions & 0 deletions src/shared/lib/query/QueryParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,19 @@ describe('QueryParser', () => {
expect(toQueryString(parsedQuery)).toEqual(toQueryString(expected));
expect(toQueryString(parsedQuery)).toEqual(query);
});

it('interprets or as search phrase when query consists of single or', () => {
const query = 'or';
const result = parser.parseSearchQuery(query);
const expected = 'or';
expect(toQueryString(result)).toEqual(expected);
});

it('interprets or as search phrase when query ends with or', () => {
const query = 'part1 or';
const result = parser.parseSearchQuery(query);
const expected = 'part1 or';
expect(toQueryString(result)).toEqual(expected);
});
});
});
14 changes: 12 additions & 2 deletions src/shared/lib/query/QueryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,18 @@ export class QueryParser {
this.createAndClause(phrases.slice(currInd, nextDash))
);
return nextDash;
} else if (nextOr > 0 && nextDash === -1) {
clauses.push(this.createAndClause(phrases.slice(currInd, nextOr)));
} else if (nextOr >= 0 && nextDash === -1) {
if (nextOr === phrases.length - 1) {
// When query ends with 'or', interpret 'or' as a phrase:
clauses.push(
this.createAndClause(phrases.slice(currInd, nextOr + 1))
);
} else {
// When 'or' is between phrases, interpret 'or' as separator of and-clauses:
clauses.push(
this.createAndClause(phrases.slice(currInd, nextOr))
);
}
return nextOr + 1;
} else {
if (nextOr < nextDash) {
Expand Down

0 comments on commit 153a877

Please sign in to comment.