Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Set Node): Preserve binary data by default #9668

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/editor-ui/src/components/NodeIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const iconSource = computed<NodeIconSource>(() => {
// Otherwise, extract it from icon prop
if (nodeType.icon) {
const icon = getNodeIcon(nodeType, uiStore.appliedTheme);

if (icon) {
const [type, path] = icon.split(':');
if (type === 'file') {
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Set/Set.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Set extends VersionedNodeType {
icon: 'fa:pen',
group: ['input'],
description: 'Add or edit fields on an input item and optionally remove other fields',
defaultVersion: 3.3,
defaultVersion: 3.4,
};

const nodeVersions: IVersionedNodeType['nodeVersions'] = {
Expand All @@ -22,6 +22,7 @@ export class Set extends VersionedNodeType {
3.1: new SetV2(baseDescription),
3.2: new SetV2(baseDescription),
3.3: new SetV2(baseDescription),
3.4: new SetV2(baseDescription),
};

super(nodeVersions, baseDescription);
Expand Down
120 changes: 118 additions & 2 deletions packages/nodes-base/nodes/Set/test/v2/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('test Set2, composeReturnItem', () => {
include: 'none',
};

const result = composeReturnItem.call(fakeExecuteFunction, 0, inputItem, newData, options);
const result = composeReturnItem.call(fakeExecuteFunction, 0, inputItem, newData, options, 3.4);

expect(result).toEqual({
json: {
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('test Set2, composeReturnItem', () => {
include: 'selected',
};

const result = composeReturnItem.call(fakeExecuteFunction, 0, inputItem, newData, options);
const result = composeReturnItem.call(fakeExecuteFunction, 0, inputItem, newData, options, 3.4);

expect(result).toEqual({
json: {
Expand All @@ -132,6 +132,122 @@ describe('test Set2, composeReturnItem', () => {
},
});
});

it('should include binary when expected in version <3.4', () => {
const fakeExecuteFunction = createMockExecuteFunction({});

const inputItem = {
json: {
input1: 'value1',
input2: 2,
input3: [1, 2, 3],
},
pairedItem: {
item: 0,
input: undefined,
},
binary: {
data: {
data: 'content',
mimeType: 'image/jpg',
},
},
};

const newData = {
num1: 55,
str1: '42',
arr1: ['foo', 'bar'],
obj: {
key: 'value',
},
};

const resultWithIncludeBinary = composeReturnItem.call(
fakeExecuteFunction,
0,
inputItem,
newData,
{
include: 'all',
includeBinary: true,
},
3.3,
);

expect(resultWithIncludeBinary.binary).toEqual(inputItem.binary);

const resultWithoutIncludeBinary = composeReturnItem.call(
fakeExecuteFunction,
0,
inputItem,
newData,
{
include: 'all',
},
3.3,
);

expect(resultWithoutIncludeBinary.binary).toBeUndefined();
});

it('should include binary when expected in version >=3.4', () => {
const fakeExecuteFunction = createMockExecuteFunction({});

const inputItem = {
json: {
input1: 'value1',
input2: 2,
input3: [1, 2, 3],
},
pairedItem: {
item: 0,
input: undefined,
},
binary: {
data: {
data: 'content',
mimeType: 'image/jpg',
},
},
};

const newData = {
num1: 55,
str1: '42',
arr1: ['foo', 'bar'],
obj: {
key: 'value',
},
};

const resultWithStripBinary = composeReturnItem.call(
fakeExecuteFunction,
0,
inputItem,
newData,
{
include: 'all',
stripBinary: true,
},
3.4,
);

expect(resultWithStripBinary.binary).toBeUndefined();

const resultWithoutStripBinary = composeReturnItem.call(
fakeExecuteFunction,
0,
inputItem,
newData,
{
include: 'all',
},
3.4,
);

expect(resultWithoutStripBinary.binary).toEqual(inputItem.binary);
});
});

describe('test Set2, parseJsonParameter', () => {
Expand Down
21 changes: 20 additions & 1 deletion packages/nodes-base/nodes/Set/v2/SetV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const versionDescription: INodeTypeDescription = {
name: 'set',
iconColor: 'blue',
group: ['input'],
version: [3, 3.1, 3.2, 3.3],
version: [3, 3.1, 3.2, 3.3, 3.4],
description: 'Modify, add, or remove item fields',
subtitle: '={{$parameter["mode"]}}',
defaults: {
Expand Down Expand Up @@ -208,8 +208,27 @@ const versionDescription: INodeTypeDescription = {
name: 'includeBinary',
type: 'boolean',
default: true,
displayOptions: {
hide: {
'@version': [{ _cnd: { gte: 3.4 } }],
},
},
description: 'Whether binary data should be included if present in the input item',
},
{
displayName: 'Strip Binary Data',
name: 'stripBinary',
type: 'boolean',
default: true,
description:
'Whether binary data should be stripped from the input item. Only applies when "Include Other Input Fields" is enabled.',
displayOptions: {
show: {
'@version': [{ _cnd: { gte: 3.4 } }],
'/includeOtherFields': [true],
},
},
},
{
displayName: 'Ignore Type Conversion Errors',
name: 'ignoreConversionErrors',
Expand Down
1 change: 1 addition & 0 deletions packages/nodes-base/nodes/Set/v2/helpers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type SetNodeOptions = {
ignoreConversionErrors?: boolean;
include?: IncludeMods;
includeBinary?: boolean;
stripBinary?: boolean;
};

export type SetField = {
Expand Down
6 changes: 5 additions & 1 deletion packages/nodes-base/nodes/Set/v2/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ export function composeReturnItem(
inputItem: INodeExecutionData,
newFields: IDataObject,
options: SetNodeOptions,
nodeVersion: number,
) {
const newItem: INodeExecutionData = {
json: {},
pairedItem: { item: itemIndex },
};

if (options.includeBinary && inputItem.binary !== undefined) {
const includeBinary =
(nodeVersion >= 3.4 && !options.stripBinary && options.include !== 'none') ||
(nodeVersion < 3.4 && !!options.includeBinary);
if (includeBinary && inputItem.binary !== undefined) {
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Set/v2/manual.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export async function execute(
newData[name] = value;
}

return composeReturnItem.call(this, i, item, newData, options);
return composeReturnItem.call(this, i, item, newData, options, node.typeVersion);
}

const assignmentCollection = this.getNodeParameter(
Expand All @@ -247,7 +247,7 @@ export async function execute(
return [name, value];
}),
);
return composeReturnItem.call(this, i, item, newData, options);
return composeReturnItem.call(this, i, item, newData, options, node.typeVersion);
} catch (error) {
if (this.continueOnFail()) {
return { json: { error: (error as Error).message, pairedItem: { item: i } } };
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Set/v2/raw.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function execute(
);
}

return composeReturnItem.call(this, i, item, newData, options);
return composeReturnItem.call(this, i, item, newData, options, node.typeVersion);
} catch (error) {
if (this.continueOnFail()) {
return { json: { error: (error as Error).message }, pairedItem: { item: i } };
Expand Down
Loading