File tree 3 files changed +41
-2
lines changed
3 files changed +41
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## Unreleased
4
4
5
+ ### Bug Fixes
6
+
7
+ - Fix the logic path that merges plain objects ([ #68579 ] ( https://github.com/WordPress/gutenberg/pull/68579 ) ).
8
+
5
9
## 6.15.0 (2025-01-02)
6
10
7
11
### Enhancements
Original file line number Diff line number Diff line change @@ -340,7 +340,9 @@ const deepMergeRecursive = (
340
340
341
341
// Handle nested objects
342
342
} else if ( isPlainObject ( source [ key ] ) ) {
343
- if ( isNew || ( override && ! isPlainObject ( target [ key ] ) ) ) {
343
+ const targetValue = Object . getOwnPropertyDescriptor ( target , key )
344
+ ?. value ;
345
+ if ( isNew || ( override && ! isPlainObject ( targetValue ) ) ) {
344
346
// Create a new object if the property is new or needs to be overridden
345
347
target [ key ] = { } ;
346
348
if ( propSignal ) {
@@ -350,9 +352,10 @@ const deepMergeRecursive = (
350
352
proxifyState ( ns , target [ key ] as Object )
351
353
) ;
352
354
}
355
+ deepMergeRecursive ( target [ key ] , source [ key ] , override ) ;
353
356
}
354
357
// Both target and source are plain objects, merge them recursively
355
- if ( isPlainObject ( target [ key ] ) ) {
358
+ else if ( isPlainObject ( targetValue ) ) {
356
359
deepMergeRecursive ( target [ key ] , source [ key ] , override ) ;
357
360
}
358
361
Original file line number Diff line number Diff line change @@ -455,6 +455,38 @@ describe( 'Interactivity API', () => {
455
455
expect ( target . message . fontStyle ) . toBeUndefined ( ) ;
456
456
} ) ;
457
457
458
+ it ( 'should not overwrite getters that become objects if `override` is false' , ( ) => {
459
+ const target : any = proxifyState ( 'test' , {
460
+ get message ( ) {
461
+ return 'hello' ;
462
+ } ,
463
+ } ) ;
464
+
465
+ const getterSpy = jest . spyOn ( target , 'message' , 'get' ) ;
466
+
467
+ let message : any ;
468
+ const spy = jest . fn ( ( ) => ( message = target . message ) ) ;
469
+ effect ( spy ) ;
470
+
471
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
472
+ expect ( message ) . toBe ( 'hello' ) ;
473
+
474
+ deepMerge (
475
+ target ,
476
+ { message : { content : 'hello' , fontStyle : 'italic' } } ,
477
+ false
478
+ ) ;
479
+
480
+ // The effect callback reads `target.message`, so the getter is executed once as well.
481
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
482
+ expect ( getterSpy ) . toHaveBeenCalledTimes ( 1 ) ;
483
+
484
+ expect ( message ) . toBe ( 'hello' ) ;
485
+ expect ( target . message ) . toBe ( 'hello' ) ;
486
+ expect ( target . message . content ) . toBeUndefined ( ) ;
487
+ expect ( target . message . fontStyle ) . toBeUndefined ( ) ;
488
+ } ) ;
489
+
458
490
it ( 'should keep reactivity of arrays that are initially undefined' , ( ) => {
459
491
const target : any = proxifyState ( 'test' , { } ) ;
460
492
You can’t perform that action at this time.
0 commit comments