From 7ced65484fa7c91e10e96f70d6791b689a5686d3 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sun, 15 May 2022 19:28:42 +0200 Subject: [PATCH] fix(core): Fix issue with fixedCollection having all default values --- packages/workflow/src/NodeHelpers.ts | 31 +++ packages/workflow/test/NodeHelpers.test.ts | 300 +++++++++++++++++++++ 2 files changed, 331 insertions(+) diff --git a/packages/workflow/src/NodeHelpers.ts b/packages/workflow/src/NodeHelpers.ts index a4eacf91dd..a706d88030 100644 --- a/packages/workflow/src/NodeHelpers.ts +++ b/packages/workflow/src/NodeHelpers.ts @@ -720,6 +720,18 @@ export function getNodeParameters( } } + if ( + !returnDefaults && + nodeProperties.typeOptions?.multipleValues === false && + propertyValues && + Object.keys(propertyValues).length === 0 + ) { + // For fixedCollections, which only allow one value, it is important to still return + // the empty object which indicates that a value got added, even if it does not have + // anything set. If that is not done, the value would get lost. + return nodeValues; + } + // Iterate over all collections for (const itemName of Object.keys(propertyValues || {})) { if ( @@ -795,6 +807,25 @@ export function getNodeParameters( } } + if ( + !returnDefaults && + nodeProperties.typeOptions?.multipleValues === false && + collectionValues && + Object.keys(collectionValues).length === 0 && + propertyValues && + propertyValues?.constructor.name === 'Object' && + Object.keys(propertyValues).length !== 0 + ) { + // For fixedCollections, which only allow one value, it is important to still return + // the object with an empty collection property which indicates that a value got added + // which contains all default values. If that is not done, the value would get lost. + const returnValue = {} as INodeParameters; + Object.keys(propertyValues || {}).forEach((value) => { + returnValue[value] = {}; + }); + return { [nodeProperties.name]: returnValue }; + } + if (Object.keys(collectionValues).length !== 0 || returnDefaults) { // Set only if value got found if (returnDefaults) { diff --git a/packages/workflow/test/NodeHelpers.test.ts b/packages/workflow/test/NodeHelpers.test.ts index 47c7b68ad0..315909ddb8 100644 --- a/packages/workflow/test/NodeHelpers.test.ts +++ b/packages/workflow/test/NodeHelpers.test.ts @@ -3008,6 +3008,306 @@ describe('Workflow', () => { }, }, }, + { + description: + 'complex type "collection" which contains a "fixedCollection" with "multipleValues: false" that has a collection value added but nothing to the fixedCollection', + input: { + nodePropertiesArray: [ + { + displayName: 'Options', + name: 'options', + placeholder: 'Add Option', + type: 'collection', + default: {}, + options: [ + { + displayName: 'Sort', + name: 'sort', + type: 'fixedCollection', + typeOptions: { + multipleValues: false, + }, + default: {}, + placeholder: 'Add Sort', + options: [ + { + displayName: 'Sort', + name: 'value', + values: [ + { + displayName: 'Descending', + name: 'descending', + type: 'boolean', + default: true, + description: 'Sort by descending order', + }, + { + displayName: 'Order By', + name: 'ordering', + type: 'options', + default: 'date', + options: [ + { + name: 'Date', + value: 'date', + }, + { + name: 'Name', + value: 'name', + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + nodeValues: { + options: { + sort: {}, + }, + }, + }, + output: { + noneDisplayedFalse: { + defaultsFalse: { + options: { + sort: {}, + }, + }, + defaultsTrue: { + options: { + sort: {}, + }, + }, + }, + noneDisplayedTrue: { + defaultsFalse: { + options: { + sort: {}, + }, + }, + defaultsTrue: { + options: { + sort: {}, + }, + }, + }, + }, + }, + { + description: + 'complex type "collection" which contains a "fixedCollection" with "multipleValues: false" that has all values set to the default values (by having it as an empty object)', + input: { + nodePropertiesArray: [ + { + displayName: 'Options', + name: 'options', + placeholder: 'Add Option', + type: 'collection', + default: {}, + options: [ + { + displayName: 'Sort', + name: 'sort', + type: 'fixedCollection', + typeOptions: { + multipleValues: false, + }, + default: {}, + placeholder: 'Add Sort', + options: [ + { + displayName: 'Sort', + name: 'value', + values: [ + { + displayName: 'Descending', + name: 'descending', + type: 'boolean', + default: true, + description: 'Sort by descending order', + }, + { + displayName: 'Order By', + name: 'ordering', + type: 'options', + default: 'date', + options: [ + { + name: 'Date', + value: 'date', + }, + { + name: 'Name', + value: 'name', + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + nodeValues: { + options: { + sort: { + value: {}, + }, + }, + }, + }, + output: { + noneDisplayedFalse: { + defaultsFalse: { + options: { + sort: { + value: {}, + }, + }, + }, + defaultsTrue: { + options: { + sort: { + value: { + descending: true, + ordering: 'date', + }, + }, + }, + }, + }, + noneDisplayedTrue: { + defaultsFalse: { + options: { + sort: { + value: {}, + }, + }, + }, + defaultsTrue: { + options: { + sort: { + value: { + descending: true, + ordering: 'date', + }, + }, + }, + }, + }, + }, + }, + { + description: + 'complex type "collection" which contains a "fixedCollection" with "multipleValues: false" that has all values set to the default values (by having each value set)', + input: { + nodePropertiesArray: [ + { + displayName: 'Options', + name: 'options', + placeholder: 'Add Option', + type: 'collection', + default: {}, + options: [ + { + displayName: 'Sort', + name: 'sort', + type: 'fixedCollection', + typeOptions: { + multipleValues: false, + }, + default: {}, + options: [ + { + displayName: 'Sort', + name: 'value', + values: [ + { + displayName: 'Descending', + name: 'descending', + type: 'boolean', + default: true, + }, + { + displayName: 'Order By', + name: 'ordering', + type: 'options', + default: 'date', + options: [ + { + name: 'Date', + value: 'date', + }, + { + name: 'Name', + value: 'name', + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + nodeValues: { + options: { + sort: { + value: { + descending: true, + ordering: 'date', + }, + }, + }, + }, + }, + output: { + noneDisplayedFalse: { + defaultsFalse: { + options: { + sort: { + value: {}, + }, + }, + }, + defaultsTrue: { + options: { + sort: { + value: { + descending: true, + ordering: 'date', + }, + }, + }, + }, + }, + noneDisplayedTrue: { + defaultsFalse: { + options: { + sort: { + value: {}, + }, + }, + }, + defaultsTrue: { + options: { + sort: { + value: { + descending: true, + ordering: 'date', + }, + }, + }, + }, + }, + }, + }, ]; for (const testData of tests) {