Skip to content

Commit

Permalink
handle a scene merge edge case where null values would cause the diff…
Browse files Browse the repository at this point in the history
… to crash
  • Loading branch information
sdumetz committed Feb 23, 2024
1 parent 2890077 commit 56321a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions source/server/utils/merge/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,19 @@ describe("merge.diff()", function(){
it("throws when diffing an array with an object", function(){
expect(()=>diff({a:[]}, {a:{}})).to.throw("Can't diff an array with an object");
});

it("handles null in arrays", function(){
expect(diff({a:[0, null, 0]}, {a:[0,0, null]})).to.deep.equal({a:{1:0, 2:null}});
});
});

describe("objects", function(){
it("deep merge objects", function(){
expect(diff<any>({v: {a: 1 }}, {v: {a:1, b:2}})).to.deep.equal({v:{b:2}});
});

it("handles object creation", function(){
expect(diff({name:"A", articles: null}, {name:"A", articles: {id:"1"}})).to.deep.equal({articles: {id:"1"}});
})
});
});
6 changes: 6 additions & 0 deletions source/server/utils/merge/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export default function diff<T extends Record<string,any>>(from :T, to :T) :Diff
continue;
}

if(from[key] == null){
//We already verified that to[key] is not null
r[key] = to[key];
continue;
}

const d = diff(from[key] as any, to[key] as any);
if(Object.keys(d).length){
//console.log("Diffing", key, from[key], to[key]);
Expand Down

0 comments on commit 56321a3

Please sign in to comment.