Skip to content

Commit 3add0b8

Browse files
authored
fix(Postgres Node): Fix inserting null or undefined in type=json columns (#14672)
1 parent ff47279 commit 3add0b8

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

packages/nodes-base/nodes/Postgres/test/v2/utils.test.ts

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -429,36 +429,47 @@ describe('Test PostgresV2, hasJsonDataType', () => {
429429
});
430430

431431
describe('Test PostgresV2, convertValuesToJsonWithPgp', () => {
432-
it('should use pgp to properly convert values to JSON', () => {
433-
const pgp = pgPromise();
434-
const pgpJsonSpy = jest.spyOn(pgp.as, 'json');
435-
436-
const schema: ColumnInfo[] = [
437-
{ column_name: 'data', data_type: 'json', is_nullable: 'YES' },
438-
{ column_name: 'id', data_type: 'integer', is_nullable: 'NO' },
439-
];
440-
const values = [
441-
{
442-
value: { data: [], id: 1 },
443-
expected: { data: '[]', id: 1 },
444-
},
445-
{
446-
value: { data: [0], id: 1 },
447-
expected: { data: '[0]', id: 1 },
448-
},
449-
{
450-
value: { data: { key: 2 }, id: 1 },
451-
expected: { data: '{"key":2}', id: 1 },
452-
},
453-
];
454-
455-
values.forEach((value) => {
456-
const data = value.value.data;
457-
458-
expect(convertValuesToJsonWithPgp(pgp, schema, value.value)).toEqual(value.expected);
459-
expect(value.value).toEqual(value.expected);
432+
const pgp = pgPromise();
433+
const pgpJsonSpy = jest.spyOn(pgp.as, 'json');
434+
const schema: ColumnInfo[] = [
435+
{ column_name: 'data', data_type: 'json', is_nullable: 'YES' },
436+
{ column_name: 'id', data_type: 'integer', is_nullable: 'NO' },
437+
];
438+
439+
beforeEach(() => {
440+
pgpJsonSpy.mockClear();
441+
});
442+
443+
it.each([
444+
{
445+
value: { data: [], id: 1 },
446+
expected: { data: '[]', id: 1 },
447+
},
448+
{
449+
value: { data: [0], id: 1 },
450+
expected: { data: '[0]', id: 1 },
451+
},
452+
{
453+
value: { data: { key: 2 }, id: 1 },
454+
expected: { data: '{"key":2}', id: 1 },
455+
},
456+
{
457+
value: { data: null, id: 1 },
458+
expected: { data: null, id: 1 },
459+
shouldSkipPgp: true,
460+
},
461+
{
462+
value: { data: undefined, id: 1 },
463+
expected: { data: undefined, id: 1 },
464+
shouldSkipPgp: true,
465+
},
466+
])('should convert $value.data to json correctly', ({ value, expected, shouldSkipPgp }) => {
467+
const data = value.data;
468+
expect(convertValuesToJsonWithPgp(pgp, schema, value)).toEqual(expected);
469+
expect(value).toEqual(expected);
470+
if (!shouldSkipPgp) {
460471
expect(pgpJsonSpy).toHaveBeenCalledWith(data, true);
461-
});
472+
}
462473
});
463474
});
464475

packages/nodes-base/nodes/Postgres/v2/helpers/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,10 @@ export function convertValuesToJsonWithPgp(
414414
values: IDataObject,
415415
) {
416416
schema
417-
.filter(({ data_type }: { data_type: string }) => data_type === 'json')
417+
.filter(
418+
({ data_type, column_name }) =>
419+
data_type === 'json' && values[column_name] !== null && values[column_name] !== undefined,
420+
)
418421
.forEach(({ column_name }) => {
419422
values[column_name] = pgp.as.json(values[column_name], true);
420423
});

0 commit comments

Comments
 (0)