fix(n8n Form Node): Use binary response from latest node in execution (#14842)

This commit is contained in:
Dana
2025-04-28 12:32:18 +02:00
committed by GitHub
parent bfd85dd3c9
commit 9672a6db0a
2 changed files with 54 additions and 4 deletions

View File

@@ -19,9 +19,9 @@ export const binaryResponse = async (
): Promise<{ data: string | Buffer; fileName: string; type: string }> => {
const inputDataFieldName = context.getNodeParameter('inputDataFieldName', '') as string;
const parentNodes = context.getParentNodes(context.getNode().name);
const binaryNode = parentNodes.find((node) =>
getBinaryDataFromNode(context, node?.name)?.hasOwnProperty(inputDataFieldName),
);
const binaryNode = parentNodes
.reverse()
.find((node) => getBinaryDataFromNode(context, node?.name)?.hasOwnProperty(inputDataFieldName));
if (!binaryNode) {
throw new OperationalError(`No binary data with field ${inputDataFieldName} found.`);
}

View File

@@ -2,7 +2,7 @@ import { type Response } from 'express';
import { type MockProxy, mock } from 'jest-mock-extended';
import { type INode, type IWebhookFunctions } from 'n8n-workflow';
import { renderFormCompletion } from '../formCompletionUtils';
import { binaryResponse, renderFormCompletion } from '../formCompletionUtils';
describe('formCompletionUtils', () => {
let mockWebhookFunctions: MockProxy<IWebhookFunctions>;
@@ -251,4 +251,54 @@ describe('formCompletionUtils', () => {
}
});
});
describe('binaryResponse', () => {
it('should get the latest binary data from the parent nodes', async () => {
const expectedBinaryResponse = {
inputData: {
data: 'IyAxLiBHbyBpbiBwb3N0Z3',
fileExtension: 'txt',
fileName: 'file.txt',
fileSize: '458 B',
fileType: 'text',
mimeType: 'text/plain',
},
};
const notExpectedBinaryResponse = {
inputData: {
data: 'notexpected',
fileExtension: 'txt',
fileName: 'file.txt',
fileSize: '458 B',
fileType: 'text',
mimeType: 'text/plain',
},
};
mockWebhookFunctions.getNodeParameter.mockImplementation((parameterName: string) => {
const params: { [key: string]: any } = {
inputDataFieldName: 'inputData',
};
return params[parameterName];
});
mockWebhookFunctions.getParentNodes.mockReturnValueOnce(parentNodesWithMultipleBinaryFiles);
mockWebhookFunctions.evaluateExpression.mockImplementation((arg) => {
if (arg === `{{ $('${nodeNameWithFile}').first().binary }}`) {
return expectedBinaryResponse;
} else {
return notExpectedBinaryResponse;
}
});
const result = await binaryResponse(mockWebhookFunctions);
expect(result).toEqual({
data: atob(expectedBinaryResponse.inputData.data),
fileName: expectedBinaryResponse.inputData.fileName,
type: expectedBinaryResponse.inputData.mimeType,
});
});
});
});