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

fix(core): Allow index as top-level item key for Code node #12469

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('result validation', () => {
['binary', {}],
['pairedItem', {}],
['error', {}],
['index', {}], // temporarily allowed until refactored out
])(
'should not throw an error if the output item has %s key in addition to json',
(key, value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ import type { INodeExecutionData } from 'n8n-workflow';
import { ValidationError } from './errors/validation-error';
import { isObject } from './obj-utils';

export const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem', 'error']);
export const REQUIRED_N8N_ITEM_KEYS = new Set([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we really need to setup a @n8n/constants package to reduce duplication like this.

'json',
'binary',
'pairedItem',
'error',

/**
* The `index` key was added accidentally to Function, FunctionItem, Gong,
* Execute Workflow, and ToolWorkflowV2, so we need to allow it temporarily.
* Once we stop using it in all nodes, we can stop allowing the `index` key.
*/
'index',
]);

function validateTopLevelKeys(item: INodeExecutionData, itemIndex: number) {
for (const key in item) {
Expand Down
14 changes: 13 additions & 1 deletion packages/nodes-base/nodes/Code/Sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ export interface SandboxContext extends IWorkflowDataProxyData {
helpers: IExecuteFunctions['helpers'];
}

export const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem', 'error']);
export const REQUIRED_N8N_ITEM_KEYS = new Set([
'json',
'binary',
'pairedItem',
'error',

/**
* The `index` key was added accidentally to Function, FunctionItem, Gong,
* Execute Workflow, and ToolWorkflowV2, so we need to allow it temporarily.
* Once we stop using it in all nodes, we can stop allowing the `index` key.
*/
'index',
]);

export function getSandboxContext(
this: IExecuteFunctions | ISupplyDataFunctions,
Expand Down
7 changes: 7 additions & 0 deletions packages/nodes-base/nodes/Code/test/Code.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ describe('Code Node unit test', () => {
[{ json: { count: 42 } }],
[{ json: { count: 42 } }],
],

// temporarily allowed until refactored out
'should handle an index key': [
[{ json: { count: 42 }, index: 0 }],
[{ json: { count: 42 }, index: 0 }],
],

'should handle when returned data is not an array': [
{ json: { count: 42 } },
[{ json: { count: 42 } }],
Expand Down
Loading