payloadcms / payloadcms/payload

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

Open
#16,296 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area: core stale status: needs-triage
Dominant language
TypeScript
Stars
44.8k
Forks
4.2k
Avg merge
2d 21h
Merged PRs (30d)
53

Description

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 |

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in mergeLocalizedData.ts, especially the default case described in the issue, then run the reproduction under test/_community with pnpm test:int _community after starting MongoDB from test/docker-compose.yml. The fix is complete when publishing a document with an untouched optional localized field no longer stores or returns that field as {} and the integration test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, typescript
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.