Skip to content

Commit 61a4076

Browse files
committed
Don't guess path (id) when using update handler
1 parent 653bec4 commit 61a4076

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

clerk.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,15 @@ Base.prototype._meta = function (doc) {
464464
* Parse arguments.
465465
*
466466
* @param {Array} args The arguments.
467-
* @param {Integer} start The index from which to start reading arguments.
468-
* @param {Boolean} withDoc Set to `true` if the doc source is given as a
467+
* @param {Integer} [start] The index from which to start reading arguments.
468+
* @param {Boolean} [withBody] Set to `true` if the request body is given as a
469469
* parameter before HTTP query options.
470-
* @return {Promise} A Promise, if no callback is provided,
471-
* otherwise `null`.
470+
* @param {Boolean} [notDoc] The request body is not a document.
471+
* @return {Promise} A Promise, if no callback is provided, otherwise `null`.
472472
* @private
473473
*/
474474

475-
Base.prototype._ = function (args, start, withDoc) {
475+
Base.prototype._ = function (args, start, withBody, notDoc) {
476476
var self = this, doc, id, rev;
477477

478478
function request(method, path, options) {
@@ -493,11 +493,12 @@ Base.prototype._ = function (args, start, withDoc) {
493493

494494
request.f = isFunction(args[args.length - 1]) && args.pop();
495495
request.p = isString(args[0]) && encodeURI(args.shift());
496-
request.q = args[withDoc ? 1 : 0] || {};
497-
request.h = args[withDoc ? 2 : 1] || {};
496+
request.q = args[withBody ? 1 : 0] || {};
497+
request.h = args[withBody ? 2 : 1] || {};
498498

499-
if (withDoc) {
500-
if (doc = (request.b = args[0])) {
499+
if (withBody) {
500+
doc = request.b = args[0];
501+
if (!notDoc) {
501502
if (id = request.p || doc._id || doc.id) request.p = id;
502503
if (rev = request.q.rev || doc._rev || doc.rev) request.q.rev = rev;
503504
}
@@ -1110,8 +1111,8 @@ DB.prototype._changes = function (request) {
11101111
*
11111112
* @param {String} handler Update handler. Example: mydesign/myhandler
11121113
* @param {String} [id] Document ID.
1114+
* @param {any} data Data.
11131115
* @param {Object} [query] HTTP query options.
1114-
* @param {Object|String} [data] Data.
11151116
* @param {Object} [headers] Headers.
11161117
* @param {handler} [callback] Callback function.
11171118
* @return {Promise} A Promise, if no callback is provided,
@@ -1120,7 +1121,7 @@ DB.prototype._changes = function (request) {
11201121
*/
11211122

11221123
DB.prototype.update = function (handler /* [id], [data], [query], [headers], [callback] */) {
1123-
var request = this._(arguments, 1, 1);
1124+
var request = this._(arguments, 1, 1, 1);
11241125
var path = handler.split("/", 2);
11251126

11261127
path = "_design/" + encodeURIComponent(path[0]) +

test/database_test.js

+8
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@ describe("DB", function () {
429429
});
430430
});
431431

432+
it("should not set the path from the request body", function (done) {
433+
this.db.update("test/test", {_id: "foo", Hello: "World"}, function (err) {
434+
expect(err).to.be.an(Error);
435+
expect(err.url).to.not.match(/\bfoo\b/)
436+
done();
437+
});
438+
});
439+
432440
});
433441

434442

0 commit comments

Comments
 (0)