Describe the Bug
When using "Publish in [locale]" on a document where a localized field has no value saved for any locale yet, the field is written to the database as an empty object {} instead of being absent or null. In practice this affects optional fields most visibly, since required fields are blocked by validation before reaching this code path, but the root cause is not the required flag.
This happens in mergeLocalizedData.ts (default case). When newValue is undefined (field was never touched), the || {} fallback produces {} which gets written to the DB. Fetching the document with locale=all then returns subtitle: {}.
Link to the code that reproduces this issue
https://github.com/acrusella/payload/tree/publish-specific-locale-empty-object/test/_community
Reproduction Steps
- Clone the repo and check out branch
publish-specific-locale-empty-object
- Run
pnpm install
- Start MongoDB:
docker compose -f test/docker-compose.yml --profile mongodb up -d --wait
- Run
pnpm test:int _community
- Observe the test fail with
AssertionError: expected {} to not deeply equal {}
The test creates a document leaving an optional localized field empty, then publishes with publishSpecificLocale: 'en' (the "Publish in en" dropdown in the admin UI), then fetches with locale: 'all', the field comes back as {} instead of being absent.
Which area(s) are affected?
area: core
Environment Info
| Payload | 3.83.0 |
| Next.js | 16.2.3 |
| Node.js | 23.11.0 |
| Database | MongoDB |
Describe the Bug
When using "Publish in [locale]" on a document where a localized field has no value saved for any locale yet, the field is written to the database as an empty object
{}instead of being absent ornull. In practice this affects optional fields most visibly, since required fields are blocked by validation before reaching this code path, but the root cause is not the required flag.This happens in
mergeLocalizedData.ts(default case). WhennewValueisundefined(field was never touched), the|| {}fallback produces{}which gets written to the DB. Fetching the document withlocale=allthen returnssubtitle: {}.Link to the code that reproduces this issue
https://github.com/acrusella/payload/tree/publish-specific-locale-empty-object/test/_community
Reproduction Steps
publish-specific-locale-empty-objectpnpm installdocker compose -f test/docker-compose.yml --profile mongodb up -d --waitpnpm test:int _communityAssertionError: expected {} to not deeply equal {}The test creates a document leaving an optional localized field empty, then publishes with
publishSpecificLocale: 'en'(the "Publish in en" dropdown in the admin UI), then fetches withlocale: 'all', the field comes back as{}instead of being absent.Which area(s) are affected?
area: core
Environment Info