Skip to content

Commit 42f8b51

Browse files
gerardosabettaJerry (Gerardo Sabetta)
authored andcommitted
fix: prevents runtime error on undefined facets
It is not safe to assume that facets is going to be in the response
1 parent 77cdb7c commit 42f8b51

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
POST /api/as/v1/engines/node-modules/search.json
2+
authorization: Bearer api-hean6g8dmxnm2shqqiag757a
3+
content-type: application/json
4+
x-swiftype-client: elastic-app-search-javascript
5+
x-swiftype-client-version: 8.7.0
6+
accept: */*
7+
accept-encoding: gzip,deflate
8+
body: {\"query\":\"dog\",\"page\":{\"size\":0},\"filters\":{},\"facets\":{\"license\":[{\"type\":\"value\",\"size\":3}]},\"record_analytics\":false,\"analytics\":{\"tags\":[\"Facet-Only\"]}}
9+
10+
HTTP/1.1 200 OK
11+
date: Thu, 27 Sep 2018 20:36:08 GMT
12+
content-type: application/json; charset=utf-8
13+
transfer-encoding: chunked
14+
connection: close
15+
vary: Accept-Encoding, Origin
16+
status: 200 OK
17+
x-frame-options: SAMEORIGIN
18+
x-xss-protection: 1; mode=block
19+
x-content-type-options: nosniff
20+
etag: W/"d31aa222a68122cb8392577cf4a1a7f1"
21+
cache-control: max-age=0, private, must-revalidate
22+
x-request-id: 91197d45d104a3e2281a60eecec630ff
23+
x-runtime: 0.270027
24+
x-swiftype-frontend-datacenter: dal05
25+
x-swiftype-frontend-node: web01.dal05
26+
x-swiftype-edge-datacenter: dal05
27+
x-swiftype-edge-node: web01.dal05
28+
29+
{"meta":{"warnings":[],"page":{"current":1,"total_pages":0,"total_results":0,"size":1},"request_id":"91197d45d104a3e2281a60eecec630ff"},"results":[]}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
POST /api/as/v1/engines/node-modules/search.json
2+
authorization: Bearer api-hean6g8dmxnm2shqqiag757a
3+
content-type: application/json
4+
x-swiftype-client: elastic-app-search-javascript
5+
x-swiftype-client-version: 8.7.0
6+
accept: */*
7+
accept-encoding: gzip,deflate
8+
body: {\"query\":\"dog\",\"page\":{\"size\":1},\"filters\":{\"license\":\"BSD\"},\"facets\":{\"license\":[{\"type\":\"value\",\"size\":3}],\"dependencies\":[{\"type\":\"value\",\"size\":3}]}}
9+
10+
HTTP/1.1 200 OK
11+
date: Thu, 27 Sep 2018 20:53:56 GMT
12+
content-type: application/json; charset=utf-8
13+
transfer-encoding: chunked
14+
connection: close
15+
vary: Accept-Encoding, Origin
16+
status: 200 OK
17+
x-frame-options: SAMEORIGIN
18+
x-xss-protection: 1; mode=block
19+
x-content-type-options: nosniff
20+
etag: W/"5010d52221547ddec5baceeb9f8a4931"
21+
cache-control: max-age=0, private, must-revalidate
22+
x-request-id: 8369bdda4d5c018ddbd85fc2859fbadb
23+
x-runtime: 0.212519
24+
x-swiftype-frontend-datacenter: dal05
25+
x-swiftype-frontend-node: web01.dal05
26+
x-swiftype-edge-datacenter: dal05
27+
x-swiftype-edge-node: web01.dal05
28+
29+
{"meta":{"warnings":[],"page":{"current":1,"total_pages":0,"total_results":0,"size":10},"request_id":"8369bdda4d5c018ddbd85fc2859fbadb"},"results":[]}

src/client.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,17 @@ export default class Client {
189189

190190
return Promise.all([baseQueryPromise, ...disjunctiveQueriesPromises]).then(
191191
([baseQueryResults, ...disjunctiveQueries]) => {
192-
disjunctiveQueries.forEach(disjunctiveQueryResults => {
193-
const [facetName, facetValue] = Object.entries(
194-
disjunctiveQueryResults.facets
195-
)[0];
196-
baseQueryResults.facets[facetName] = facetValue;
197-
});
192+
if (
193+
baseQueryResults.facets &&
194+
Object.keys(baseQueryResults.facets).length
195+
) {
196+
disjunctiveQueries.forEach(disjunctiveQueryResults => {
197+
const [facetName, facetValue] = Object.entries(
198+
disjunctiveQueryResults.facets
199+
)[0];
200+
baseQueryResults.facets[facetName] = facetValue;
201+
});
202+
}
198203
return baseQueryResults;
199204
}
200205
);

tests/client.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ describe("Client", () => {
248248
});
249249
});
250250

251+
// Fixture: disjunctive_license_returning_no_facets
252+
// Fixture: search_filter_and_multi_facet_returning_no_facets
253+
it("will handle not having `facets` in the backend response without throwing", async () => {
254+
const result = await client.search("dog", {
255+
...config,
256+
filters: {
257+
license: "BSD"
258+
},
259+
facets: {
260+
license: [{ type: "value", size: 3 }],
261+
dependencies: [{ type: "value", size: 3 }]
262+
},
263+
disjunctiveFacets: ["license", "dependencies"]
264+
});
265+
266+
expect(result.info.facets).toBeUndefined();
267+
});
268+
251269
// Fixture: disjunctive_license_with_override_tags
252270
// Fixture: Fixture: search_filter_and_multi_facet_with_tags
253271
it("will accept an alternative analytics tag for disjunctive queries", async () => {

0 commit comments

Comments
 (0)