Skip to content

fix(NODE-7661): count empty-scope Code as code_with_scope in calculateObjectSize - #911

Open
spokodev wants to merge 1 commit into
mongodb:mainfrom
spokodev:fix/calculate-size-code-empty-scope
Open

fix(NODE-7661): count empty-scope Code as code_with_scope in calculateObjectSize#911
spokodev wants to merge 1 commit into
mongodb:mainfrom
spokodev:fix/calculate-size-code-empty-scope

Conversation

@spokodev

@spokodev spokodev commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

calculateObjectSize disagrees with serialize for a Code whose scope is a present but empty object.

The serializer emits code_with_scope (0x0f) whenever scope is a non-null object, including {}. calculateObjectSize instead required the scope to have at least one key before counting the code_with_scope layout, so it fell back to the plain code (0x0d) size and under-reported by 9 bytes (the 4-byte total-size field plus the 5-byte empty scope subdocument).

const doc = { a: new Code('x', {}) };
calculateObjectSize(doc);   // 14
serialize(doc).byteLength;  // 23

Gate the code_with_scope size branch on scope being a non-null object, matching the serializer; the size-calculation stack already accounts for the empty scope subdocument. Adds a regression test asserting calculateObjectSize === serialize().byteLength for an empty-scope Code.

A Code value with a present but empty scope object serializes as the
BSON code_with_scope (0x0f) type, but calculateObjectSize gated the
code_with_scope branch on the scope having at least one key. As a
result calculateObjectSize under-reported such documents by 9 bytes
versus the bytes serialize actually writes.

Match the serializer, which emits code_with_scope whenever scope is a
non-null object, so the two agree for empty scopes.
@spokodev
spokodev requested a review from a team as a code owner July 8, 2026 17:59
@johnmtll

johnmtll commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution! NODE-7661 will track this request and it will be picked up in the next triage session.

@johnmtll johnmtll added External Submission tracked-in-jira There is a ticket in Mongo's Jira instance tracking this issue/PR labels Jul 9, 2026
@dariakp dariakp changed the title fix: count empty-scope Code as code_with_scope in calculateObjectSize fix(NODE-7661): count empty-scope Code as code_with_scope in calculateObjectSize Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Submission tracked-in-jira There is a ticket in Mongo's Jira instance tracking this issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants