fix: localized tabs with empty data and an array field inside lead to crash in afterChange (#10410)

Previously, this config:
```ts
import type { CollectionConfig } from 'payload'

export const tabSlug = 'tabs'

export const Tab: CollectionConfig = {
  slug: tabSlug,
  fields: [
    {
      type: 'tabs',
      tabs: [
        {
          name: 'tabLocalized',
          localized: true,
          fields: [
            {
              name: 'title',
              type: 'text',
            },
            {
              name: 'array',
              type: 'array',
              fields: [
                {
                  name: 'title',
                  type: 'text',
                },
              ],
            },
          ],
        },
      ],
    },
  ],
}

```
This call
```ts
const result = await payload.create({
  collection: tabSlug,
  locale: englishLocale,
  data: {
    tabLocalized: {},
  },
})
```

Led to the following crash with the MongoDB adapter
<img width="741" alt="image"
src="https://github.com/user-attachments/assets/8d1d37de-a685-4a30-bd37-58af164108a2"
/>

This is due to how Mongoose, apparently just ignores the `minimize:
false` configuration
a83a430a3a/packages/db-mongodb/src/models/buildSchema.ts (L571)
and we, instead of `tabLocalized: { en: { } }` receive just
`tabLocalized: {}`.

This isn't an issue with group fields because we have fallback for them

a83a430a3a/packages/payload/src/fields/hooks/afterChange/promise.ts (L203)

This PR adds the same for tabs.
This commit is contained in:
Sasha
2025-01-11 00:37:23 +02:00
committed by GitHub
parent 04733f0db1
commit 1af7d8745c
4 changed files with 83 additions and 6 deletions

View File

@@ -522,6 +522,12 @@ export interface Tab {
id: string;
tabLocalized?: {
title?: string | null;
array?:
| {
title?: string | null;
id?: string | null;
}[]
| null;
};
tab?: {
title?: string | null;
@@ -1119,6 +1125,12 @@ export interface TabsSelect<T extends boolean = true> {
| T
| {
title?: T;
array?:
| T
| {
title?: T;
id?: T;
};
};
tab?:
| T