Skip to content

Commit 249f8df

Browse files
authored
fix(db-mongodb): error on null values in arrays and blocks (#17190)
Copy of 3.x PR #17182
1 parent 2a99a01 commit 249f8df

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

packages/db-mongodb/src/utilities/transform.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
})

packages/db-mongodb/src/utilities/transform.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ const sanitizeRelationship = ({
180180

181181
if (Array.isArray(value)) {
182182
result = value.map((val) => {
183+
// safety check in the case of base
184+
if (val === null || typeof val === 'undefined') {
185+
return val
186+
}
187+
183188
// Handle has many - polymorphic
184189
if (isValidRelationObject(val)) {
185190
const relatedCollectionForSingleValue = config.collections?.find(
@@ -355,6 +360,13 @@ const stripFields = ({
355360
let hasNull = false
356361
for (let i = 0; i < localeData.length; i++) {
357362
const data = localeData[i]
363+
364+
if (!data || typeof data !== 'object') {
365+
localeData[i] = null
366+
hasNull = true
367+
continue
368+
}
369+
358370
let fields: FlattenedField[] | null = null
359371

360372
if (field.type === 'array') {
@@ -429,6 +441,13 @@ const stripFields = ({
429441

430442
for (let i = 0; i < fieldData.length; i++) {
431443
const data = fieldData[i]
444+
445+
if (!data || typeof data !== 'object') {
446+
fieldData[i] = null
447+
hasNull = true
448+
continue
449+
}
450+
432451
let fields: FlattenedField[] | null = null
433452

434453
if (field.type === 'array') {

0 commit comments

Comments
 (0)