Skip to content

Commit

Permalink
fix(aggregate): automatically convert functions to strings when using…
Browse files Browse the repository at this point in the history
… `$function` operator

Fix #9897
  • Loading branch information
vkarpov15 committed Feb 9, 2021
1 parent a82b3ee commit 425f35a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/aggregate.js
Expand Up @@ -8,7 +8,7 @@ const AggregationCursor = require('./cursor/AggregationCursor');
const Query = require('./query');
const applyGlobalMaxTimeMS = require('./helpers/query/applyGlobalMaxTimeMS');
const promiseOrCallback = require('./helpers/promiseOrCallback');
const stringifyAccumulatorOptions = require('./helpers/aggregate/stringifyAccumulatorOptions');
const stringifyFunctionOperators = require('./helpers/aggregate/stringifyFunctionOperators');
const util = require('util');
const utils = require('./utils');
const read = Query.prototype.read;
Expand Down Expand Up @@ -983,7 +983,7 @@ Aggregate.prototype.exec = function(callback) {

return promiseOrCallback(callback, cb => {
prepareDiscriminatorPipeline(this);
stringifyAccumulatorOptions(this._pipeline);
stringifyFunctionOperators(this._pipeline);

model.hooks.execPre('aggregate', this, error => {
if (error) {
Expand Down
@@ -1,6 +1,6 @@
'use strict';

module.exports = function stringifyAccumulatorOptions(pipeline) {
module.exports = function stringifyFunctionOperators(pipeline) {
if (!Array.isArray(pipeline)) {
return;
}
Expand All @@ -17,9 +17,21 @@ module.exports = function stringifyAccumulatorOptions(pipeline) {
}
}

const stageType = Object.keys(stage)[0];
if (stageType && typeof stage[stageType] === 'object') {
const stageOptions = stage[stageType];
for (const key of Object.keys(stageOptions)) {
if (stageOptions[key] != null &&
stageOptions[key].$function != null &&
typeof stageOptions[key].$function.body === 'function') {
stageOptions[key].$function.body = stageOptions[key].$function.body.toString();
}
}
}

if (stage.$facet != null) {
for (const key of Object.keys(stage.$facet)) {
stringifyAccumulatorOptions(stage.$facet[key]);
stringifyFunctionOperators(stage.$facet[key]);
}
}
}
Expand Down
26 changes: 23 additions & 3 deletions test/helpers/aggregate.test.js
@@ -1,9 +1,9 @@
'use strict';

const assert = require('assert');
const stringifyAccumulatorOptions = require('../../lib/helpers/aggregate/stringifyAccumulatorOptions');
const stringifyFunctionOperators = require('../../lib/helpers/aggregate/stringifyFunctionOperators');

describe('stringifyAccumulatorOptions', function() {
describe('stringifyFunctionOperators', function() {
it('converts accumulator args to strings (gh-9364)', function() {
const pipeline = [{
$group: {
Expand Down Expand Up @@ -35,11 +35,31 @@ describe('stringifyAccumulatorOptions', function() {
}
}];

stringifyAccumulatorOptions(pipeline);
stringifyFunctionOperators(pipeline);

assert.equal(typeof pipeline[0].$group.avgCopies.$accumulator.init, 'string');
assert.equal(typeof pipeline[0].$group.avgCopies.$accumulator.accumulate, 'string');
assert.equal(typeof pipeline[0].$group.avgCopies.$accumulator.merge, 'string');
assert.equal(typeof pipeline[0].$group.avgCopies.$accumulator.finalize, 'string');
});

it('converts function args to strings (gh-9897)', function() {
const pipeline = [{
$addFields: {
newField: {
$function: {
body: function() {
return true;
},
args: ['$oldField'],
lang: 'js'
}
}
}
}];

stringifyFunctionOperators(pipeline);

assert.equal(typeof pipeline[0].$addFields.newField.$function.body, 'string');
});
});

0 comments on commit 425f35a

Please sign in to comment.