fix(setDefaultsOnInsert): run setters on default values during upsert#16093
Merged
vkarpov15 merged 1 commit intoAutomattic:9.4from Mar 15, 2026
Merged
Conversation
When using `setDefaultsOnInsert` with upsert, default values were not being passed through setters. This happened because `getDefault()` was called with `init = true`, which causes `_applySetters` to skip all setters. Changed `init` from `true` to `false` so setters run on defaults, matching the behavior of normal document creation. Fixes Automattic#16051
vkarpov15
approved these changes
Mar 15, 2026
Collaborator
vkarpov15
left a comment
There was a problem hiding this comment.
LGTM although for caution I'll merge into 9.4. I don't expect this to be backwards breaking because setters already run on non-default values so I think it is reasonable to assume that setters already work on queries.
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.
Problem
When using
setDefaultsOnInsertwith upsert operations, default values are not passed through setters. This means a schema like:will store
slugas'Hello World!'when upserted, instead of'hello-world'(what you get with normal document creation).Root Cause
In
lib/helpers/setDefaultsOnInsert.js,schemaType.getDefault()was called withinit = true. This causes_applySettersto skip all setters, sinceinitis meant to signal that the value is being initialized and shouldn't go through setter transformations.Solution
Changed
initfromtruetofalsein thegetDefault()call so setters run on default values, matching the behavior of normal document creation vianew Model().Also updated the existing test from gh-16025 which was asserting the old (buggy) behavior, and added a unit test in
test/helpers/setDefaultsOnInsert.test.js.How to Test
Fixes #16051