From 3d839afa0fccfd4762ebf6384a1b94a618a6ed2a Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Thu, 19 Aug 2021 17:47:56 +0100 Subject: [PATCH] fix: filter out {} stages from the view pipeline --- .../compass-aggregations/src/modules/index.js | 16 +++++++++++++++- .../src/modules/index.spec.js | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/compass-aggregations/src/modules/index.js b/packages/compass-aggregations/src/modules/index.js index 303d4a563e9..14377988ffb 100644 --- a/packages/compass-aggregations/src/modules/index.js +++ b/packages/compass-aggregations/src/modules/index.js @@ -2,6 +2,7 @@ const debug = require('debug')('mongodb-aggregations:modules:index'); import { combineReducers } from 'redux'; import { ObjectId } from 'bson'; +import isEmpty from 'lodash.isempty'; import dataService, { INITIAL_STATE as DS_INITIAL_STATE } from './data-service'; import fields, { INITIAL_STATE as FIELDS_INITIAL_STATE } from './fields'; @@ -675,6 +676,19 @@ export const getPipelineFromIndexedDB = (pipelineId) => { }; }; + +/** + * Make view pipeline. + * + * @returns {Array} The mapped/filtered view pipeline. + */ +export const makeViewPipeline = (unfilteredPipeline) => { + return unfilteredPipeline + .map((p) => (p.executor || generateStage(p))) + // generateStage can return {} under various conditions + .filter((stage) => !isEmpty(stage)); +}; + /** * Open create view. * @@ -686,7 +700,7 @@ export const openCreateView = () => { return (dispatch, getState) => { const state = getState(); const sourceNs = state.namespace; - const sourcePipeline = state.pipeline.map((p) => (p.executor || generateStage(p))); + const sourcePipeline = makeViewPipeline(state.pipeline); const meta = { source: sourceNs, diff --git a/packages/compass-aggregations/src/modules/index.spec.js b/packages/compass-aggregations/src/modules/index.spec.js index 0331262b81d..c98e4440f40 100644 --- a/packages/compass-aggregations/src/modules/index.spec.js +++ b/packages/compass-aggregations/src/modules/index.spec.js @@ -5,6 +5,7 @@ import reducer, { clonePipeline, newPipeline, modifySource, + makeViewPipeline, RESET, CLEAR_PIPELINE, RESTORE_PIPELINE, @@ -256,4 +257,21 @@ describe('root [ module ]', () => { }); }); }); + + describe('#makeViewPipeline', () => { + it('filters out empty stages', () => { + const pipeline = [ + // executor preferred + { executor: { a: 1 } }, + + // falling back to generateStage() + { isEnabled: false}, // !isEnabled + {}, // no stageOperator + { stage: '' } // stage === '' + // leaving out non-blank generated ones for generateStage()'s own unit tests + ]; + + expect(makeViewPipeline(pipeline)).to.deep.equal([{ a: 1 }]); + }); + }); });