Skip to content

PHPC-2791: Add tests for duplicate BSON keys - #2191

Open
GromNaN wants to merge 3 commits into
mongodb:v2.5from
GromNaN:PHPC-2791
Open

GromNaN wants to merge 3 commits into
mongodb:v2.5from
GromNaN:PHPC-2791

Conversation

@GromNaN

@GromNaN GromNaN commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Add tests that document how the driver handles BSON documents with duplicate keys, following the prose tests of the BSON corpus spec:

  • 2.1 Test encoding (tests/bson/bson-document-duplicate-keys-001.phpt): PHPC does not expose a BSON builder API, and PHP collapses duplicate keys in array literals, so Document::fromPHP() keeps only the last value.
  • 2.2 Test decoding (tests/bson/bson-document-duplicate-keys-002.phpt): iteration returns both elements, get() and offsetGet() return the first value, toPHP() returns the last value, and Extended JSON keeps both keys.
  • 2.3 Test round-trip (tests/bson/bson-document-duplicate-keys-003.phpt): raw BSON and the Extended JSON round-trip keep duplicates, the PHP round-trip keeps only the last value.
  • 2.4 Test parsing Extended JSON (tests/bson/bson-document-duplicate-keys-004.phpt): Document::fromJSON() keeps duplicate keys in order.

No behavior change. The driver does not validate duplicate keys, so the BSON_VALIDATE_DUPLICATE_KEYS flag added in CDRIVER-6492 is not exposed. PackedArray with duplicate indexes is already covered by bson-packedarray-get-005.phpt.

The API documentation on php.net is updated in php/doc-extensions#50

Jira: PHPC-2791

Document how Document handles duplicate keys when decoding and when
round-tripping, following the BSON corpus spec prose tests 2.2 and 2.3.
@GromNaN GromNaN added the tidy a change not worth a release (e.g. coding standards, CI, typos) label Oct 23, 2026
@GromNaN
GromNaN marked this pull request as ready for review October 23, 2026 17:25
@GromNaN
GromNaN requested a review from a team as a code owner October 23, 2026 17:26
@GromNaN
GromNaN requested review from paulinevos and a balanced review from Copilot and removed request for a team October 23, 2026 17:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

These focused test-only additions have no identified blocking issues; the remaining expectation-portability concern is non-blocking.

1 open issue
What changed in this PR

Adds regression tests documenting how MongoDB\BSON\Document handles duplicate keys, aligned with the BSON corpus prose tests. No driver behavior changes.

Changes:

  • Covers PHP encoding and BSON decoding, including iteration, lookup, and conversion behavior.
  • Verifies duplicate-key preservation through raw BSON and Extended JSON round-trips.
  • Confirms Extended JSON parsing preserves duplicate keys in order.
File Description
tests/​bson/​bson-document-duplicate-keys-004.phpt Tests duplicate-key preservation during Extended JSON parsing.
tests/​bson/​bson-document-duplicate-keys-003.phpt Tests BSON, Extended JSON, and PHP round-trips.
tests/​bson/​bson-document-duplicate-keys-002.phpt Documents duplicate-key behavior across decoding APIs.
tests/​bson/​bson-document-duplicate-keys-001.phpt Verifies PHP collapses duplicate keys before encoding.

Review effort: Balanced

Comment on lines +36 to +49
--EXPECT--
Iteration returns both elements in order
string(3) "foo"
int(1)
string(3) "foo"
int(2)

get() and offsetGet() return the first value
int(1)
int(1)
bool(true)

toPHP() returns the last value
object(stdClass)#3 (1) {

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tidy a change not worth a release (e.g. coding standards, CI, typos)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants