fix: skip empty object write for untouched localized fields in mergeLocalizedData#16649
Open
rachit367 wants to merge 1 commit into
Open
fix: skip empty object write for untouched localized fields in mergeLocalizedData#16649rachit367 wants to merge 1 commit into
rachit367 wants to merge 1 commit into
Conversation
…in mergeLocalizedData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Stop
mergeLocalizedDatafrom writing{}for a localized field that has no value in any locale and no incoming value.In
packages/payload/src/utilities/mergeLocalizedData.ts, thedefaultcase of the localized branch:existingValuefrom the doc and only treats it as locale data when it is a plain object (not arrays, not nullish, not other primitives).elsethat unconditionally assignedexistingValue(which had been defaulted to{}). The preservation path now only runs when there is real existing locale data.newValueis not a valid object and there is no existing locale data, the field is left absent on the result instead of being written as{}.Two unit tests added under the existing
simple fieldsgroup:should not write an empty object when localized field has no value in any localeshould preserve existing locale data on a localized field when new value is undefinedWhy?
Fixes #16296.
When
publishSpecificLocaleis used on a document where a localized field has never been written in any locale, the merge path tookdocWithLocales[field.name] || {}and then fell through toresult[field.name] = existingValue. That wrote{}to the DB for the untouched field, so a later read withlocale: 'all'returnedsubtitle: {}instead of the field being absent.The repro from the issue exercises this through
_communityintegration tests; the unit-level behavior is the same and is now covered directly inmergeLocalizedData.spec.ts.How?
Scope is limited to the
defaultcase of the localized branch inmergeLocalizedData. Behavior in the other branches (arrays, blocks, groups, tabs) is unchanged.Walk-through of cases in the modified branch:
{}and writes the new locales — unchanged.{}, now leaves the field absent.No other call sites needed to change.
mergeLocalizedDataalways returns a plain object whosefield.namemay or may not be present; callers already handle the absent case the same way they would have handledundefined.