Skip to content

Commit 6c0f788

Browse files
authored
Merge pull request #503 from tcchhabra/issue_3740
Fix for issue 3740
2 parents 6f8eda7 + 049f4dc commit 6c0f788

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/routes/projects/list.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,22 @@ const setFilter = (value, keyword, fieldName) => {
268268
return buildEsQueryWithFilter(value, keyword, MATCH_TYPE_EXACT_PHRASE, fieldName);
269269
};
270270

271+
/**
272+
* ES need to skip special chars else it is considered as RegEx
273+
*
274+
* @param {String} query query being searched for
275+
* @return {String} result after parsing
276+
*/
277+
function escapeElasticsearchQuery(query) {
278+
const chars = ['\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']',
279+
'^', '"', '~', '*', '?', ':', '/', '<', '>'];
280+
let result = query;
281+
_.forEach(chars, (item) => {
282+
result = result.replace(item, `\\${item}`);
283+
});
284+
return result;
285+
}
286+
271287
/**
272288
* Parse the ES search criteria and prepare search request body
273289
*
@@ -426,7 +442,7 @@ const parseElasticSearchCriteria = (criteria, fields, order) => {
426442

427443
if (!keyword) {
428444
// Not a specific field search nor an exact phrase search, do a wildcard match
429-
keyword = criteria.filters.keyword;
445+
keyword = escapeElasticsearchQuery(keywordCriterion);
430446
matchType = MATCH_TYPE_WILDCARD;
431447
}
432448

src/routes/projects/list.spec.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const data = [
2121
type: 'generic',
2222
billingAccountId: 1,
2323
name: 'test1',
24-
description: 'test project1',
24+
description: 'test project1 abc/d',
2525
status: 'active',
2626
details: {
2727
utm: {
@@ -1157,7 +1157,7 @@ describe('LIST Project', () => {
11571157
resJson.should.have.lengthOf(1);
11581158
resJson[0].should.have.property('description');
11591159
resJson[0].should.not.have.property('cancelReason');
1160-
resJson[0].description.should.be.eq('test project1');
1160+
resJson[0].description.should.be.eq('test project1 abc/d');
11611161
done();
11621162
}
11631163
});
@@ -1336,6 +1336,46 @@ describe('LIST Project', () => {
13361336
}
13371337
});
13381338
});
1339+
1340+
it('should find a project by quoted keyword with a special symbol in the name', (done) => {
1341+
request(server)
1342+
.get('/v5/projects/?keyword="abc/d"')
1343+
.set({
1344+
Authorization: `Bearer ${testUtil.jwts.admin}`,
1345+
})
1346+
.expect('Content-Type', /json/)
1347+
.expect(200)
1348+
.end((err, res) => {
1349+
if (err) {
1350+
done(err);
1351+
} else {
1352+
const resJson = res.body;
1353+
should.exist(resJson);
1354+
resJson.should.have.lengthOf(1);
1355+
done();
1356+
}
1357+
});
1358+
});
1359+
1360+
it('should find a project by keyword with a special symbol in the name', (done) => {
1361+
request(server)
1362+
.get('/v5/projects/?keyword=abc/d')
1363+
.set({
1364+
Authorization: `Bearer ${testUtil.jwts.admin}`,
1365+
})
1366+
.expect('Content-Type', /json/)
1367+
.expect(200)
1368+
.end((err, res) => {
1369+
if (err) {
1370+
done(err);
1371+
} else {
1372+
const resJson = res.body;
1373+
should.exist(resJson);
1374+
resJson.should.have.lengthOf(1);
1375+
done();
1376+
}
1377+
});
1378+
});
13391379
});
13401380
});
13411381
});

0 commit comments

Comments
 (0)