Skip to content

Commit ae3cf25

Browse files
siimsamsdhmlau
authored andcommitted
fix: null value not persisted for properties of type JSON, Any, or Object
Signed-off-by: Siim Sams <[email protected]>
1 parent c905f02 commit ae3cf25

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

lib/dao.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,12 @@ DataAccessObject._forDB = function(data) {
179179
const res = {};
180180
for (const propName in data) {
181181
const type = this.getPropertyType(propName);
182-
if (type === 'JSON' || type === 'Any' || type === 'Object' || data[propName] instanceof Array) {
183-
res[propName] = JSON.stringify(data[propName]);
182+
const value = data[propName];
183+
if (value !== null && (type === 'JSON' || type === 'Any' ||
184+
type === 'Object' || value instanceof Array)) {
185+
res[propName] = JSON.stringify(value);
184186
} else {
185-
res[propName] = data[propName];
187+
res[propName] = value;
186188
}
187189
}
188190
return res;

test/loopback-dl.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,58 @@ describe('Model define with scopes configuration', function() {
13631363
});
13641364
});
13651365

1366+
describe('DataAccessObject._forDB', function() {
1367+
const ds = new DataSource('memory');
1368+
const dao = ds.DataAccessObject;
1369+
1370+
it('should return input data if dataSource is not relational', function() {
1371+
const inputData = {testKey: 'testValue'};
1372+
dao.getDataSource = () => ({isRelational: () => false});
1373+
1374+
const outputData = dao._forDB(inputData);
1375+
1376+
assert.deepEqual(outputData, inputData);
1377+
});
1378+
1379+
it('should return JSON stringified values for appropriate types', function() {
1380+
const inputData = {
1381+
key1: [1, 2, 3],
1382+
key2: {subKey: 'value'},
1383+
key3: 'nonJSONvalue',
1384+
};
1385+
dao.getDataSource = () => ({isRelational: () => true});
1386+
dao.getPropertyType = (propName) => (propName !== 'key3' ? 'JSON' : 'String');
1387+
1388+
const outputData = dao._forDB(inputData);
1389+
1390+
assert.deepEqual(outputData, {
1391+
key1: JSON.stringify([1, 2, 3]),
1392+
key2: JSON.stringify({subKey: 'value'}),
1393+
key3: 'nonJSONvalue',
1394+
});
1395+
});
1396+
1397+
it('should return original value for non JSON, non Array types', function() {
1398+
const inputData = {key1: 'string', key2: 123, key3: true};
1399+
dao.getDataSource = () => ({isRelational: () => true});
1400+
dao.getPropertyType = () => 'String';
1401+
1402+
const outputData = dao._forDB(inputData);
1403+
1404+
assert.deepEqual(outputData, inputData);
1405+
});
1406+
1407+
it('should not process null values', function() {
1408+
const inputData = {key1: 'value', key2: null};
1409+
dao.getDataSource = () => ({isRelational: () => true});
1410+
dao.getPropertyType = (propName) => 'JSON';
1411+
1412+
const outputData = dao._forDB(inputData);
1413+
1414+
assert.deepEqual(outputData, {key1: JSON.stringify('value'), key2: null});
1415+
});
1416+
});
1417+
13661418
describe('DataAccessObject', function() {
13671419
let ds, model, where, error, filter;
13681420

0 commit comments

Comments
 (0)