Skip to content

Commit

Permalink
fix(filters): make sure clauses is always initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Aug 16, 2019
1 parent 53e0917 commit 246ce4c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/api/NestedRestriction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class NestedRestriction {
}

/** The clauses containing the nested restrictions and their logical operators. */
public clauses = [] as Clause[];
public clauses?: Clause[];

constructor(...clauses: Clause[]) {
this.clauses = clauses;
Expand All @@ -37,8 +37,10 @@ export class NestedRestriction {

/** Adds an additional clause. */
private withClause(clause: Clause) {
if (!this.clauses) {
this.clauses = [] as Clause[];
}
this.clauses.push(clause);
return this;
}

}
16 changes: 10 additions & 6 deletions src/dao/AbstractDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ export abstract class AbstractDAO<K, T> extends BaseDAO implements IValueProvide
if (visitor.onRestriction) { visitor.onRestriction(restriction); }
} else if (restriction instanceof NestedRestriction) {
if (visitor.onNestedRestriction) { visitor.onNestedRestriction(restriction); }
restriction.clauses.forEach((c) => {
self.visitClause(c, visitor);
});
if (restriction.clauses) {
restriction.clauses.forEach((c) => {
self.visitClause(c, visitor);
});
}
} else {
log.warn('Restriction is of an unknown type: ' + JSON.stringify(restriction));
}
Expand All @@ -181,9 +183,11 @@ export abstract class AbstractDAO<K, T> extends BaseDAO implements IValueProvide
protected visitFilter(filter: Filter, visitor: IFilterVisitor) {
const self = this;
if (visitor.onFilter) { visitor.onFilter(filter); }
filter.clauses.forEach((clause) => {
self.visitClause(clause, visitor);
});
if (filter.clauses) {
filter.clauses.forEach((clause) => {
self.visitClause(clause, visitor);
});
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/dao/V1FilterProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class V1FilterProcessor implements IFilterProcessor {
ret.limit = '' + filter.limit;
}

if (!filter.clauses) {
return ret;
}

for (const clause of filter.clauses) {
if (clause.operator !== Operators.OR) {
throw new OnmsError('V1 only supports OR operators!');
Expand Down
5 changes: 4 additions & 1 deletion src/dao/V2FilterProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ export class V2FilterProcessor implements IFilterProcessor {
}

/** Given a list of clauses, recursively generate the FIQL query string. */
private toFIQL(clauses: Clause[]) {
private toFIQL(clauses: Clause[] | undefined) {
let search = '';
if (!clauses || clauses.length === 0) {
return search;
}
for (const clause of clauses) {
if (search.length > 0) {
search += this.toFIQLOperator(clause.operator);
Expand Down

0 comments on commit 246ce4c

Please sign in to comment.