fix: avoid calling property default factory twice in Value.Default for objects#1612
Merged
sinclairzx81 merged 1 commit intoJun 10, 2026
Conversation
…r objects When `Value.Default` processes an object schema, it called `FromType` twice for each property that needed a default: once to check whether the result was an unassignable undefined, and again to actually assign the value. For static defaults this was harmless (same value both times), but for function-based defaults (`default: () => …`) the factory ran twice, wasting a call and—more importantly—discarding the first return value while using the second. This caused incorrect results for generators that produce unique values (auto-increment IDs, timestamps, etc.). The fix reuses the `propertyValue` already computed on the first call instead of invoking `FromType` a second time.
Owner
|
@chatman-media Hi, thanks for the PR! This was a excellent find! The update is both a fix and a optimization!! :) Nice work! Will merge through and publish on |
Merged
Owner
|
@chatman-media Updates have been published out to Thanks again :) All the best! |
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
Value.Defaultprocesses object schemas by iterating each property and resolving a default when the current value isundefined. The implementation calledFromTypetwice for each property that needed a default:propertyValue(used only to check whether the result is an unassignable undefined)value[key]For static defaults (
default: 42) this double-call was harmless. For function-based defaults (default: () => …) the factory ran twice per absent property: the first return value was silently discarded and the second was assigned.This is a real bug for any factory that produces unique values on each call — auto-increment IDs, timestamps, UUIDs, or any stateful generator:
Fix
Reuse the
propertyValuealready computed on the first call instead of invokingFromTypea second time:Tests
Two new test cases added to
test/typebox/runtime/value/default/object.ts:Both tests fail before the fix and pass after. All 34,879 existing tests continue to pass.