From d2d531b5ae54473e91477a7490645cef2cda8be5 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 3 Mar 2021 20:31:30 -0500 Subject: [PATCH] fix(schema): correctly handle trailing array filters when looking up schema paths Fix #9977 --- .../schema/cleanPositionalOperators.js | 4 ++-- .../schema.cleanPositionalOperators.test.js | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 test/helpers/schema.cleanPositionalOperators.test.js diff --git a/lib/helpers/schema/cleanPositionalOperators.js b/lib/helpers/schema/cleanPositionalOperators.js index b467be44968..905bb78820b 100644 --- a/lib/helpers/schema/cleanPositionalOperators.js +++ b/lib/helpers/schema/cleanPositionalOperators.js @@ -7,6 +7,6 @@ module.exports = function cleanPositionalOperators(path) { return path. - replace(/\.\$(\[[^\]]*\])?\./g, '.0.'). - replace(/\.(\[[^\]]*\])?\$$/g, '.0'); + replace(/\.\$(\[[^\]]*\])?(?=\.)/g, '.0'). + replace(/\.\$(\[[^\]]*\])?$/g, '.0'); }; \ No newline at end of file diff --git a/test/helpers/schema.cleanPositionalOperators.test.js b/test/helpers/schema.cleanPositionalOperators.test.js new file mode 100644 index 00000000000..e22a7d89015 --- /dev/null +++ b/test/helpers/schema.cleanPositionalOperators.test.js @@ -0,0 +1,22 @@ +'use strict'; + +const assert = require('assert'); +const cleanPositionalOperators = require('../../lib/helpers/schema/cleanPositionalOperators'); + +describe('cleanPositionalOperators', function() { + it('replaces trailing array filter', function() { + assert.equal(cleanPositionalOperators('questions.$[q]'), 'questions.0'); + }); + + it('replaces trailing $', function() { + assert.equal(cleanPositionalOperators('questions.$'), 'questions.0'); + }); + + it('replaces interior array filters', function() { + assert.equal(cleanPositionalOperators('questions.$[q].$[r].test'), 'questions.0.0.test'); + }); + + it('replaces interior elemMatch', function() { + assert.equal(cleanPositionalOperators('questions.$.$.test'), 'questions.0.0.test'); + }); +}); \ No newline at end of file