Merge pull request #11885 from meteor/fix/oplog-converter-empty-object

Fix flatten object issue when the object is empty on oplog converter
This commit is contained in:
Filipe Névola
2022-02-04 15:29:19 +00:00
committed by GitHub
2 changed files with 11 additions and 1 deletions

View File

@@ -57,7 +57,6 @@ const nestedOplogEntryParsers = (
const setObjectSource = { ...i, ...u };
const $set = Object.keys(setObjectSource).reduce((acc, key) => {
const prefixedKey = `${prefixKey}${key}`;
return {
...acc,
...(!Array.isArray(setObjectSource[key]) && typeof setObjectSource[key] === 'object'
@@ -97,6 +96,7 @@ function flattenObject(ob) {
if (typeof ob[i] == 'object' && ob[i] !== null) {
const flatObject = flattenObject(ob[i]);
if(Object.keys(flatObject).length === 0) { return ob; }
for (const x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;

View File

@@ -50,6 +50,16 @@ Tinytest.add('oplog - v2/v1 conversion', function(test) {
JSON.stringify({ $v: 2, $set: { 'a.b': 2 } })
);
//set a new nested field inside an object
const entry51 = { "$v" : 2, "diff" : { "u" : { "count" : 1 }, "i" : { "nested" : { "state" : { } } } } };
// the correct format for this test, inspecting the mongodb oplog, should be "nested" : { "state" : { } } }
// but this is a case in which we can flatten the object without collateral, so we are considering
// "nested.state" : { } to be valid too
test.equal(
JSON.stringify(oplogV2V1Converter(entry51)),
JSON.stringify( { "$v" : 2, "$set" : { "nested.state": { }, "count" : 1 } })
);
//set an existing nested field inside an object
const entry6 = {
$v: 2,