Repository navigation
fix: keep objectMechanism 'native' identical to the default codegen - #925
Merged
Merged
Conversation
Tony133
marked this pull request as ready for review
November 10, 2026 15:39
3 of 4 tasks
Member
Author
|
@mcollina @gurgunday when you both have a moment, could you please review this PR? |
Open
13 tasks
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.
Proposal:
objectMechanism: 'native'does not produce the same output as the default codegen.Problem:
objectMechanism: 'native'is documented as byte-identical to the default codegen. I ran the two mechanisms against each other on a wider set than the existing differential test (14 leaf schemas, each as a property, a required property, an array item and a property of an object in a list, with 36 awkward inputs) and found four cases where they differ.Re-entrancy. The scratch objects are shared by every call of a serializer. If a getter or a
toJSON()of the input calls the same serializer, the inner call overwrites the values the outer call has already projected, and the outer output mixes the two objects:format: 'date'andformat: 'time'given as a string. The string is escaped twice:'a"b'is serialized as"a\\\"b"instead of"a\"b".type: 'integer'given as a BigInt outside the safe integer range. The value goes throughNumber()and loses its low digits:2n ** 64nis serialized as18446744073709552000instead of18446744073709551616.A required property with a
default. When the value is missing the default codegen uses the default, the projection throws"x" is required!.Change:
All in
lib/projection.js.finally, so an error thrown by the input does not leave the serializer locked.date/timegoes to the scratch as it is and is escaped once, byJSON.stringify.JSON.stringifypath is unchanged for every other call.defaultis checked beforerequired, in the same order as the default codegen.Tests:
test/object-mechanism-native.test.js: the new inputs are added to the existing differential test, plus three tests for required + default, BigInt array items and re-entrancy (nested up to three levels, atoJSON()on an array item, an error thrown inside the nested call and inside the outer call). The four groups fail onmainand pass with this change.Benchmark:
The hot path gains one boolean check and a
try/finally, with no measurable cost. Node.js 26.12.0 (V8 14.6), Apple Silicon,objectMechanism: 'native'on both sides, median ops/sec over 15 alternating rounds, output consumed withBuffer.byteLength:The first three rows are the
benchmark/fixtures/api-responsesschemas.Note: