Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite loop when study search query consists of single "or" #4394

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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