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(editor): Properly handle mapping of dragged expression if it contains hyphen #5703

Merged
merged 1 commit into from
Mar 16, 2023
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
57 changes: 56 additions & 1 deletion packages/editor-ui/src/utils/__tests__/mappingUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { INodeProperties } from 'n8n-workflow';
import { getMappedResult } from '../mappingUtils';
import { getMappedResult, getMappedExpression } from '../mappingUtils';

const RLC_PARAM: INodeProperties = {
displayName: 'Base',
Expand Down Expand Up @@ -199,4 +199,59 @@ describe('Mapping Utils', () => {
expect(getMappedResult(RLC_PARAM, '{{ test }}', '=test')).toEqual('=test {{ test }}');
});
});
describe('getMappedExpression', () => {
it('should generate a mapped expression with simple array path', () => {
const input = {
nodeName: 'nodeName',
distanceFromActive: 2,
path: ['sample', 'path'],
};
const result = getMappedExpression(input);
expect(result).toBe('{{ $node.nodeName.json.sample.path }}');
});

it('should generate a mapped expression with mixed array path', () => {
const input = {
nodeName: 'nodeName',
distanceFromActive: 2,
path: ['sample', 0, 'path'],
};
const result = getMappedExpression(input);
expect(result).toBe('{{ $node.nodeName.json.sample[0].path }}');
});

it('should generate a mapped expression with special characters in array path', () => {
const input = {
nodeName: 'nodeName',
distanceFromActive: 2,
path: ['sample', 'path with-space', 'path-with-hyphen'],
};
const result = getMappedExpression(input);
expect(result).toBe(
'{{ $node.nodeName.json.sample["path with-space"]["path-with-hyphen"] }}',
);
});

it('should generate a mapped expression with various path elements', () => {
const input = {
nodeName: 'nodeName',
distanceFromActive: 1,
path: ['propertyName', 'capitalizedName', 'hyphen-prop'],
};
const result = getMappedExpression(input);
expect(result).toBe('{{ $json.propertyName.capitalizedName["hyphen-prop"] }}');
});

it('should generate a mapped expression with a complex path', () => {
const input = {
nodeName: 'nodeName',
distanceFromActive: 1,
path: ['propertyName', 'capitalizedName', 'stringVal', 'some-value', 'capitalizedProp'],
};
const result = getMappedExpression(input);
expect(result).toBe(
'{{ $json.propertyName.capitalizedName.stringVal["some-value"].capitalizedProp }}',
);
});
});
});
2 changes: 1 addition & 1 deletion packages/editor-ui/src/utils/mappingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function generatePath(root: string, path: Array<string | number>): string
return `${accu}[${part}]`;
}

if (part.includes(' ') || part.includes('.')) {
if (part.includes('-') || part.includes(' ') || part.includes('.')) {
return `${accu}["${part}"]`;
}

Expand Down