@@ -406,4 +406,64 @@ describe('transform', () => {
406406 expect ( flattenValuesBefore [ i ] ) . toBe ( value . toHexString ( ) )
407407 } )
408408 } )
409+
410+ it ( 'should strip null elements from array and blocks fields on read' , ( ) => {
411+ const data : Record < string , any > = {
412+ _id : new Types . ObjectId ( ) ,
413+ array : [ null , { rel_1 : new Types . ObjectId ( ) } ] ,
414+ arrayLocalized : {
415+ en : [ null , { rel_1 : new Types . ObjectId ( ) } ] ,
416+ } ,
417+ blocks : [ null , { blockType : 'block' , rel_1 : new Types . ObjectId ( ) } ] ,
418+ }
419+
420+ const mockAdapter = {
421+ allowAdditionalKeys : false ,
422+ payload : {
423+ config,
424+ } ,
425+ } as MongooseAdapter
426+
427+ expect ( ( ) =>
428+ transform ( {
429+ adapter : mockAdapter ,
430+ operation : 'read' ,
431+ data,
432+ fields : config . collections [ 0 ] . fields ,
433+ } ) ,
434+ ) . not . toThrow ( )
435+
436+ expect ( data . array ) . toHaveLength ( 1 )
437+ expect ( data . array [ 0 ] ) . toMatchObject ( { } )
438+ expect ( data . arrayLocalized . en ) . toHaveLength ( 1 )
439+ expect ( data . blocks ) . toHaveLength ( 1 )
440+ expect ( data . blocks [ 0 ] . blockType ) . toBe ( 'block' )
441+ } )
442+
443+ it ( 'should skip null entries in hasMany relationship arrays without coercing them' , ( ) => {
444+ const validId = new Types . ObjectId ( )
445+ const data : Record < string , any > = {
446+ _id : new Types . ObjectId ( ) ,
447+ rel_2 : [ null , validId ] ,
448+ }
449+
450+ const mockAdapter = {
451+ allowAdditionalKeys : false ,
452+ payload : {
453+ config,
454+ } ,
455+ } as MongooseAdapter
456+
457+ expect ( ( ) =>
458+ transform ( {
459+ adapter : mockAdapter ,
460+ operation : 'read' ,
461+ data,
462+ fields : config . collections [ 0 ] . fields ,
463+ } ) ,
464+ ) . not . toThrow ( )
465+
466+ expect ( data . rel_2 [ 0 ] ) . toBeNull ( )
467+ expect ( data . rel_2 [ 1 ] ) . toBe ( validId . toHexString ( ) )
468+ } )
409469} )
0 commit comments