Skip to content

publishSpecificLocale writes {} for localized fields that have never been saved in any locale #16296

@acrusella

Description

@acrusella

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

  1. Clone the repo and check out branch publish-specific-locale-empty-object
  2. Run pnpm install
  3. Start MongoDB: docker compose -f test/docker-compose.yml --profile mongodb up -d --wait
  4. Run pnpm test:int _community
  5. 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 |

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreCore Payload functionalitystatus: needs-triagePossible bug which hasn't been reproduced yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions