From 752c0bc0aa0476315ca6558bc47e7dca0c659183 Mon Sep 17 00:00:00 2001 From: Aschen Date: Mon, 30 Mar 2020 09:11:07 +0200 Subject: [PATCH 1/4] Fix document search with Http protocol --- src/controllers/document.js | 8 ++------ src/protocols/http.js | 18 ++++++++++++++++-- test/protocol/http.test.js | 12 +++++++++++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/controllers/document.js b/src/controllers/document.js index 6baaccd64..02934b828 100644 --- a/src/controllers/document.js +++ b/src/controllers/document.js @@ -194,12 +194,8 @@ class DocumentController extends BaseController { delete options[opt]; } - if (request.size === undefined) { - request.size = 10; - } - - if (!request.scroll && !request.body.sort && !request.from) { - request.from = 0; + if (!options.verb) { + options.verb = 'POST'; } return this.query(request, options) diff --git a/src/protocols/http.js b/src/protocols/http.js index 57f69ec77..e53912f04 100644 --- a/src/protocols/http.js +++ b/src/protocols/http.js @@ -307,8 +307,19 @@ class HttpWrapper extends KuzzleAbstractProtocol { if (http && http.length === 1) { routes[controller][action] = http[0]; - } else if (http && http.length > 1) { - routes[controller][action] = getCorrectRoute(http); + } + else if (http && http.length > 1) { + // We need this ugly fix because the document:search route can also + // in GET with this url: "/:index/:collection" + // But to send a query, we need to pass it in the body so we need POST + // so we can change the verb but then POST on "/:index/:collection" + // is the collection:update method (document:search is "/:index/:collection/_search") + if (controller === 'document' && action === 'search') { + routes[controller][action] = getPostRoute(http); + } + else { + routes[controller][action] = getCorrectRoute(http); + } } } @@ -325,7 +336,10 @@ class HttpWrapper extends KuzzleAbstractProtocol { _warn (message) { console.warn(message); // eslint-disable-line no-console } +} +function getPostRoute (routes) { + return routes[0].verb === 'POST' ? routes[0] : routes[1] } function getCorrectRoute (routes) { diff --git a/test/protocol/http.test.js b/test/protocol/http.test.js index 15e594942..5f9ca09d1 100644 --- a/test/protocol/http.test.js +++ b/test/protocol/http.test.js @@ -721,6 +721,14 @@ describe('HTTP networking module', () => { describe('_constructRoutes', () => { it('should construct http routes from server:publicApi', () => { const publicApi = { + document: { + search: { + http: [ + { verb: 'GET', url: '/:index/:collection/' }, + { verb: 'POST', url: '/:index/:collection/_search' } + ] + } + }, foo: { login: { http: [ @@ -766,8 +774,10 @@ describe('HTTP networking module', () => { // will be in the query string should(routes.foo.create.url).be.eql('/:index/:collection/_create'); - should(routes.foo.subscribe).be.undefined(); + // we should choose the POST route for document:create + should(routes.document.search.url).be.eql('/:index/:collection/_search'); + should(routes.foo.subscribe).be.undefined(); }); it('should overwrite kuzzle routes with custom routes', () => { From 8176e7a17c46665ef6e53d2f84ee4cc75235b24c Mon Sep 17 00:00:00 2001 From: Aschen Date: Mon, 30 Mar 2020 09:11:12 +0200 Subject: [PATCH 2/4] 7.1.2 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1efaedd2b..cb2aea5ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "7.1.1", + "version": "7.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7f1cfd5c6..60f024f0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "7.1.1", + "version": "7.1.2", "description": "Official Javascript SDK for Kuzzle", "author": "The Kuzzle Team ", "repository": { From 8b572d23ee270082bb48af172745237a2c266f06 Mon Sep 17 00:00:00 2001 From: Aschen Date: Mon, 30 Mar 2020 14:13:01 +0200 Subject: [PATCH 3/4] tests --- src/protocols/http.js | 2 +- test/controllers/document.test.js | 28 +++++----------------------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/protocols/http.js b/src/protocols/http.js index e53912f04..eb1a8f5ee 100644 --- a/src/protocols/http.js +++ b/src/protocols/http.js @@ -339,7 +339,7 @@ class HttpWrapper extends KuzzleAbstractProtocol { } function getPostRoute (routes) { - return routes[0].verb === 'POST' ? routes[0] : routes[1] + return routes[0].verb === 'POST' ? routes[0] : routes[1]; } function getCorrectRoute (routes) { diff --git a/test/controllers/document.test.js b/test/controllers/document.test.js index d3447d0cf..08722d6a1 100644 --- a/test/controllers/document.test.js +++ b/test/controllers/document.test.js @@ -382,14 +382,14 @@ describe('Document Controller', () => { .then(res => { should(kuzzle.query) .be.calledOnce() - .be.calledWith({ + .be.calledWithMatch({ controller: 'document', action: 'search', index: 'index', collection: 'collection', body: {foo: 'bar'}, - from: 0, - size: 10, + from: undefined, + size: undefined, scroll: undefined }, options); @@ -416,7 +416,7 @@ describe('Document Controller', () => { .then(res => { should(kuzzle.query) .be.calledOnce() - .be.calledWith({ + .be.calledWithMatch({ controller: 'document', action: 'search', index: 'index', @@ -428,30 +428,13 @@ describe('Document Controller', () => { }, {}); should(res).be.an.instanceOf(DocumentSearchResult); - should(res._options).be.empty(); + should(res._options).match({ verb: 'POST' }); should(res._response).be.equal(result); should(res.fetched).be.equal(2); should(res.total).be.equal(3); }); }); - it('should set default value for from and size', () => { - const result = { - hits: [], - total: 0 - }; - kuzzle.document.query = sinon.stub().resolves({result}); - - return kuzzle.document.search('index', 'collection', {}) - .then(() => { - should(kuzzle.document.query).be.calledOnce(); - - const request = kuzzle.document.query.getCall(0).args[0]; - should(request.from).be.eql(0); - should(request.size).be.eql(10); - }); - }); - it('should allow to set value of 0 for size', () => { const result = { hits: [], @@ -464,7 +447,6 @@ describe('Document Controller', () => { should(kuzzle.document.query).be.calledOnce(); const request = kuzzle.document.query.getCall(0).args[0]; - should(request.from).be.eql(0); should(request.size).be.eql(0); }); }); From b325e74655dd8e29f1a8818f5025ac28bc265d4a Mon Sep 17 00:00:00 2001 From: Aschen Date: Mon, 30 Mar 2020 14:28:27 +0200 Subject: [PATCH 4/4] nit --- src/protocols/http.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocols/http.js b/src/protocols/http.js index eb1a8f5ee..0b0a8fcf8 100644 --- a/src/protocols/http.js +++ b/src/protocols/http.js @@ -310,7 +310,7 @@ class HttpWrapper extends KuzzleAbstractProtocol { } else if (http && http.length > 1) { // We need this ugly fix because the document:search route can also - // in GET with this url: "/:index/:collection" + // be accessed in GET with this url: "/:index/:collection" // But to send a query, we need to pass it in the body so we need POST // so we can change the verb but then POST on "/:index/:collection" // is the collection:update method (document:search is "/:index/:collection/_search")