Skip to content

Commit 499c8db

Browse files
authored
fix(flattener): fields null assignment is now properly handled (#796)
1 parent d765b63 commit 499c8db

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/services/flattener.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ module.exports = class Flattener {
249249
const flattenedRecord = {};
250250

251251
Object.keys(record).forEach((attribute) => {
252-
if (typeof record[attribute] === 'object') {
252+
if (record[attribute] !== null && typeof record[attribute] === 'object') {
253253
const flattenedPath = (flattenComposedKey) ? `${flattenComposedKey}${FLATTEN_SEPARATOR}${attribute}` : attribute;
254254

255255
if (flattenedFields.find((flattenedField) => flattenedField === flattenedPath)) {

test/tests/services/flattener.test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,7 @@ describe('service > Flattener', () => {
898898
engine: {
899899
horsePower: '78',
900900
identification: {
901-
manufacturer: 'Renault'
902-
+ '',
901+
manufacturer: 'Renault',
903902
},
904903
},
905904
name: 'Car',
@@ -918,6 +917,33 @@ describe('service > Flattener', () => {
918917
name: 'Car',
919918
});
920919
});
920+
921+
it('should correctly set values to null', () => {
922+
expect.assertions(1);
923+
924+
const record = {
925+
engine: {
926+
horsePower: '78',
927+
identification: {
928+
manufacturer: null,
929+
},
930+
},
931+
name: null,
932+
};
933+
934+
const flattenedFields = [
935+
`engine${FLATTEN_SEPARATOR}horsePower`,
936+
`engine${FLATTEN_SEPARATOR}identification${FLATTEN_SEPARATOR}manufacturer`,
937+
];
938+
939+
const flattenedRecord = Flattener.flattenRecordDataForUpdates(record, null, flattenedFields);
940+
941+
expect(flattenedRecord).toStrictEqual({
942+
'engine.horsePower': '78',
943+
'engine.identification.manufacturer': null,
944+
name: null,
945+
});
946+
});
921947
});
922948

923949
describe('getFlattenedFieldsName', () => {

0 commit comments

Comments
 (0)