Skip to content

Commit

Permalink
fix: fix the issue with input/output trimming when the input/output i…
Browse files Browse the repository at this point in the history
…s not provided
  • Loading branch information
machulav committed Aug 14, 2024
1 parent 0405b7f commit d23dd67
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
14 changes: 13 additions & 1 deletion packages/connery/src/core/utils/__tests__/input-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe('trimInput()', () => {
expect(trimInput(input)).toEqual({ Name: 'John', Age: '25' });
});

it('returns empty object if the input is empty', () => {
it('returns empty object if the input object is empty', () => {
const input: InputObject = {};

expect(trimInput(input)).toEqual({});
Expand All @@ -251,4 +251,16 @@ describe('trimInput()', () => {

expect(trimInput(input)).toEqual(input);
});

it('does not return undefined values', () => {
const input: InputObject = { Age: undefined, FirstName: undefined, LastName: undefined } as any;

expect(trimInput(input)).toEqual({});
});

it('returns object with the same values if the input contains properties with non string values', () => {
const input: InputObject = { Age: 25, FirstName: null, LastName: '' } as any;

expect(trimInput(input)).toEqual({ Age: 25, FirstName: null, LastName: '' });
});
});
14 changes: 13 additions & 1 deletion packages/connery/src/core/utils/__tests__/output-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,21 @@ describe('trimOutput()', () => {
expect(trimOutput(output)).toEqual({});
});

it('returns the same object if the output is already trimmed', () => {
it('returns the same output if the output is already trimmed', () => {
const output: OutputObject = { Name: 'John', Age: '25' };

expect(trimOutput(output)).toEqual(output);
});

it('does not return undefined values', () => {
const output: OutputObject = { Age: undefined, FirstName: undefined, LastName: undefined } as any;

expect(trimOutput(output)).toEqual({});
});

it('returns object with the same values if the output contains properties with non string values', () => {
const output: OutputObject = { Age: 25, FirstName: null, LastName: '' } as any;

expect(trimOutput(output)).toEqual({ Age: 25, FirstName: null, LastName: '' });
});
});
10 changes: 8 additions & 2 deletions packages/connery/src/core/utils/input-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,18 @@ export function validateExtraInputParameters(inputDefinitions: InputParameterDef
export function trimInput(input: InputObject): InputObject {
const trimmedInput: InputObject = {};

if (!input) {
if (!input || Object.keys(input).length === 0) {
return trimmedInput;
}

Object.keys(input).forEach((key) => {
trimmedInput[key] = input[key].trim();
const value = input[key];

if (typeof value === 'string') {
trimmedInput[key] = value.trim();
} else {
trimmedInput[key] = value;
}
});

return trimmedInput;
Expand Down
10 changes: 8 additions & 2 deletions packages/connery/src/core/utils/output-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ export function validateExtraOutputParameters(
export function trimOutput(output: OutputObject): OutputObject {
const trimmedOutput: OutputObject = {};

if (!output) {
if (!output || Object.keys(output).length === 0) {
return trimmedOutput;
}

Object.keys(output).forEach((key) => {
trimmedOutput[key] = output[key].trim();
const value = output[key];

if (typeof value === 'string') {
trimmedOutput[key] = value.trim();
} else {
trimmedOutput[key] = value;
}
});

return trimmedOutput;
Expand Down

0 comments on commit d23dd67

Please sign in to comment.