mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 17:46:45 +00:00
feat(core): Improve nodeNameToToolName special characters normalization (#15126)
This commit is contained in:
@@ -251,6 +251,10 @@ export function unwrapNestedOutput(output: Record<string, unknown>): Record<stri
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a node name to a valid tool name by replacing special characters with underscores
|
||||||
|
* and collapsing consecutive underscores into a single one.
|
||||||
|
*/
|
||||||
export function nodeNameToToolName(node: INode): string {
|
export function nodeNameToToolName(node: INode): string {
|
||||||
return node.name.replace(/ /g, '_');
|
return node.name.replace(/[\s.?!=+#@&*()[\]{}:;,<>\/\\'"^%$]/g, '_').replace(/_+/g, '_');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,62 @@ import { NodeOperationError } from 'n8n-workflow';
|
|||||||
import type { ISupplyDataFunctions, IExecuteFunctions, INode } from 'n8n-workflow';
|
import type { ISupplyDataFunctions, IExecuteFunctions, INode } from 'n8n-workflow';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { escapeSingleCurlyBrackets, getConnectedTools, unwrapNestedOutput } from '../helpers';
|
import {
|
||||||
|
escapeSingleCurlyBrackets,
|
||||||
|
getConnectedTools,
|
||||||
|
nodeNameToToolName,
|
||||||
|
unwrapNestedOutput,
|
||||||
|
} from '../helpers';
|
||||||
import { N8nTool } from '../N8nTool';
|
import { N8nTool } from '../N8nTool';
|
||||||
|
|
||||||
|
describe('nodeNameToToolName', () => {
|
||||||
|
const getNodeWithName = (name: string): INode => ({
|
||||||
|
id: 'test-node',
|
||||||
|
name,
|
||||||
|
type: 'test',
|
||||||
|
typeVersion: 1,
|
||||||
|
position: [0, 0] as [number, number],
|
||||||
|
parameters: {},
|
||||||
|
});
|
||||||
|
it('should replace spaces with underscores', () => {
|
||||||
|
expect(nodeNameToToolName(getNodeWithName('Test Node'))).toBe('Test_Node');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should replace dots with underscores', () => {
|
||||||
|
expect(nodeNameToToolName(getNodeWithName('Test.Node'))).toBe('Test_Node');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should replace question marks with underscores', () => {
|
||||||
|
expect(nodeNameToToolName(getNodeWithName('Test?Node'))).toBe('Test_Node');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should replace exclamation marks with underscores', () => {
|
||||||
|
expect(nodeNameToToolName(getNodeWithName('Test!Node'))).toBe('Test_Node');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should replace equals signs with underscores', () => {
|
||||||
|
expect(nodeNameToToolName(getNodeWithName('Test=Node'))).toBe('Test_Node');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should replace multiple special characters with underscores', () => {
|
||||||
|
expect(nodeNameToToolName(getNodeWithName('Test.Node?With!Special=Chars'))).toBe(
|
||||||
|
'Test_Node_With_Special_Chars',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle names that already have underscores', () => {
|
||||||
|
expect(nodeNameToToolName(getNodeWithName('Test_Node'))).toBe('Test_Node');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle names with consecutive special characters', () => {
|
||||||
|
expect(nodeNameToToolName(getNodeWithName('Test..!!??==Node'))).toBe('Test_Node');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should replace various special characters with underscores', () => {
|
||||||
|
expect(nodeNameToToolName(getNodeWithName('Test#+*()[]{}:;,<>/\\\'"%$Node'))).toBe('Test_Node');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('escapeSingleCurlyBrackets', () => {
|
describe('escapeSingleCurlyBrackets', () => {
|
||||||
it('should return undefined when input is undefined', () => {
|
it('should return undefined when input is undefined', () => {
|
||||||
expect(escapeSingleCurlyBrackets(undefined)).toBeUndefined();
|
expect(escapeSingleCurlyBrackets(undefined)).toBeUndefined();
|
||||||
@@ -166,7 +219,7 @@ describe('getConnectedTools', () => {
|
|||||||
name: 'Test Node',
|
name: 'Test Node',
|
||||||
type: 'test',
|
type: 'test',
|
||||||
typeVersion: 1,
|
typeVersion: 1,
|
||||||
position: [0, 0],
|
position: [0, 0] as [number, number],
|
||||||
parameters: {},
|
parameters: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -108,8 +108,13 @@ function makeDescription(node: INode, nodeType: INodeType): string {
|
|||||||
return (node.parameters.toolDescription as string) ?? nodeType.description.description;
|
return (node.parameters.toolDescription as string) ?? nodeType.description.description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a node name to a valid tool name by replacing special characters with underscores
|
||||||
|
* and collapsing consecutive underscores into a single one.
|
||||||
|
* This method is copied from `packages/@n8n/nodes-langchain/utils/helpers.ts`.
|
||||||
|
*/
|
||||||
export function nodeNameToToolName(node: INode): string {
|
export function nodeNameToToolName(node: INode): string {
|
||||||
return node.name.replace(/ /g, '_');
|
return node.name.replace(/[\s.?!=+#@&*()[\]{}:;,<>\/\\'"^%$]/g, '_').replace(/_+/g, '_');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user