Skip to content

Commit

Permalink
Ensure operation-level type map gets precedence over collection-level…
Browse files Browse the repository at this point in the history
… codec
  • Loading branch information
alcaeus committed Jul 25, 2023
1 parent 5af77bf commit 577fd81
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function aggregate(array $pipeline, array $options = [])
$options['readConcern'] = $this->readConcern;
}

if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down Expand Up @@ -264,7 +264,7 @@ public function aggregate(array $pipeline, array $options = [])
*/
public function bulkWrite(array $operations, array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down Expand Up @@ -648,7 +648,7 @@ public function explain(Explainable $explainable, array $options = [])
*/
public function find($filter = [], array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down Expand Up @@ -685,7 +685,7 @@ public function find($filter = [], array $options = [])
*/
public function findOne($filter = [], array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down Expand Up @@ -725,7 +725,7 @@ public function findOne($filter = [], array $options = [])
*/
public function findOneAndDelete($filter, array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down Expand Up @@ -766,7 +766,7 @@ public function findOneAndDelete($filter, array $options = [])
*/
public function findOneAndReplace($filter, $replacement, array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down Expand Up @@ -807,7 +807,7 @@ public function findOneAndReplace($filter, $replacement, array $options = [])
*/
public function findOneAndUpdate($filter, $update, array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down Expand Up @@ -922,7 +922,7 @@ public function getWriteConcern()
*/
public function insertMany(array $documents, array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand All @@ -949,7 +949,7 @@ public function insertMany(array $documents, array $options = [])
*/
public function insertOne($document, array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down Expand Up @@ -1079,7 +1079,7 @@ public function rename(string $toCollectionName, ?string $toDatabaseName = null,
*/
public function replaceOne($filter, $replacement, array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down Expand Up @@ -1154,7 +1154,7 @@ public function updateOne($filter, $update, array $options = [])
*/
public function watch(array $pipeline = [], array $options = [])
{
if (! array_key_exists('codec', $options)) {
if (! array_key_exists('codec', $options) && ! isset($options['typeMap'])) {
$options['codec'] = $this->codec;
}

Expand Down
82 changes: 81 additions & 1 deletion tests/Collection/CodecCollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MongoDB\Tests\Collection;

use Generator;
use MongoDB\BSON\Document;
use MongoDB\BulkWriteResult;
use MongoDB\Collection;
use MongoDB\Driver\BulkWrite;
Expand Down Expand Up @@ -42,6 +43,14 @@ public static function provideAggregateOptions(): Generator
],
'options' => ['codec' => null],
];

yield 'BSON type map' => [
'expected' => [
Document::fromPHP(self::createFixtureResult(2)),
Document::fromPHP(self::createFixtureResult(3)),
],
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/** @dataProvider provideAggregateOptions */
Expand Down Expand Up @@ -81,6 +90,16 @@ public static function provideBulkWriteOptions(): Generator
],
'options' => ['codec' => null],
];

yield 'BSON type map' => [
'expected' => [
Document::fromPHP(self::createFixtureResult(1)),
Document::fromPHP(self::createFixtureResult(2)),
Document::fromPHP($replacedObject),
Document::fromPHP(self::createObjectFixtureResult(4, true)),
],
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/** @dataProvider provideBulkWriteOptions */
Expand Down Expand Up @@ -108,6 +127,12 @@ public function testBulkWrite($expected, $options): void
$expected[3]->_id = $result->getInsertedIds()[0];
}

if ($expected[3] instanceof Document && $expected[3]->get('_id') === null) {
$inserted = $expected[3]->toPHP();
$inserted->_id = $result->getInsertedIds()[0];
$expected[3] = Document::fromPHP($inserted);
}

$this->assertEquals(
$expected,
$this->collection->find([], $options)->toArray(),
Expand All @@ -125,6 +150,11 @@ public function provideFindOneAndModifyOptions(): Generator
'expected' => self::createFixtureResult(1),
'options' => ['codec' => null],
];

yield 'BSON type map' => [
'expected' => Document::fromPHP(self::createFixtureResult(1)),
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/** @dataProvider provideFindOneAndModifyOptions */
Expand Down Expand Up @@ -164,6 +194,11 @@ public static function provideFindOneAndReplaceOptions(): Generator
'expected' => $replacedObject,
'options' => ['codec' => null],
];

yield 'BSON type map' => [
'expected' => Document::FromPHP($replacedObject),
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/** @dataProvider provideFindOneAndReplaceOptions */
Expand Down Expand Up @@ -202,6 +237,15 @@ public static function provideFindOptions(): Generator
],
'options' => ['codec' => null],
];

yield 'BSON type map' => [
'expected' => [
Document::fromPHP(self::createFixtureResult(1)),
Document::fromPHP(self::createFixtureResult(2)),
Document::fromPHP(self::createFixtureResult(3)),
],
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/** @dataProvider provideFindOptions */
Expand All @@ -225,6 +269,11 @@ public static function provideFindOneOptions(): Generator
'expected' => self::createFixtureResult(1),
'options' => ['codec' => null],
];

yield 'BSON type map' => [
'expected' => Document::fromPHP(self::createFixtureResult(1)),
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/** @dataProvider provideFindOneOptions */
Expand Down Expand Up @@ -256,6 +305,15 @@ public static function provideInsertManyOptions(): Generator
],
'options' => ['codec' => null],
];

yield 'BSON type map' => [
'expected' => [
Document::fromPHP(self::createObjectFixtureResult(1, true)),
Document::fromPHP(self::createObjectFixtureResult(2, true)),
Document::fromPHP(self::createObjectFixtureResult(3, true)),
],
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/** @dataProvider provideInsertManyOptions */
Expand All @@ -270,10 +328,16 @@ public function testInsertMany($expected, $options): void
$result = $this->collection->insertMany($documents, $options);
$this->assertSame(3, $result->getInsertedCount());

foreach ($expected as $index => $expectedDocument) {
foreach ($expected as $index => &$expectedDocument) {
if ($expectedDocument instanceof BSONDocument && $expectedDocument->_id === null) {
$expectedDocument->_id = $result->getInsertedIds()[$index];
}

if ($expectedDocument instanceof Document && $expectedDocument->get('_id') === null) {
$inserted = $expectedDocument->toPHP();
$inserted->_id = $result->getInsertedIds()[$index];
$expectedDocument = Document::fromPHP($inserted);
}
}

$this->assertEquals($expected, $this->collection->find([], $options)->toArray());
Expand All @@ -290,6 +354,11 @@ public static function provideInsertOneOptions(): Generator
'expected' => self::createObjectFixtureResult(1, true),
'options' => ['codec' => null],
];

yield 'BSON type map' => [
'expected' => Document::fromPHP(self::createObjectFixtureResult(1, true)),
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/** @dataProvider provideInsertOneOptions */
Expand All @@ -302,6 +371,12 @@ public function testInsertOne($expected, $options): void
$expected->_id = $result->getInsertedId();
}

if ($expected instanceof Document && $expected->get('_id') === null) {
$inserted = $expected->toPHP();
$inserted->_id = $result->getInsertedId();
$expected = Document::fromPHP($inserted);
}

$this->assertEquals($expected, $this->collection->findOne([], $options));
}

Expand All @@ -322,6 +397,11 @@ public static function provideReplaceOneOptions(): Generator
'expected' => $replacedObject,
'options' => ['codec' => null],
];

yield 'BSON type map' => [
'expected' => Document::fromPHP($replacedObject),
'options' => ['typeMap' => ['root' => 'bson']],
];
}

/** @dataProvider provideReplaceOneOptions */
Expand Down

0 comments on commit 577fd81

Please sign in to comment.