Skip to content

PHPLIB-499: Handle absence of 'ns' field in index specifications returned from listIndexes #718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Model/IndexInfoIteratorIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
namespace MongoDB\Model;

use IteratorIterator;
use Traversable;
use function array_key_exists;

/**
* IndexInfoIterator for both listIndexes command and legacy query results.
Expand All @@ -34,6 +36,19 @@
*/
class IndexInfoIteratorIterator extends IteratorIterator implements IndexInfoIterator
{
/** @var string|null $ns */
private $ns;

/**
* @param string|null $ns
*/
public function __construct(Traversable $iterator, $ns = null)
{
parent::__construct($iterator);

$this->ns = $ns;
}

/**
* Return the current element as an IndexInfo instance.
*
Expand All @@ -43,6 +58,12 @@ class IndexInfoIteratorIterator extends IteratorIterator implements IndexInfoIte
*/
public function current()
{
return new IndexInfo(parent::current());
$info = parent::current();

if (! array_key_exists('ns', $info) && $this->ns !== null) {
$info['ns'] = $this->ns;
}

return new IndexInfo($info);
}
}
2 changes: 1 addition & 1 deletion src/Operation/ListIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ private function executeCommand(Server $server)

$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);

return new IndexInfoIteratorIterator(new CachingIterator($cursor));
return new IndexInfoIteratorIterator(new CachingIterator($cursor), $this->databaseName . '.' . $this->collectionName);
}
}
1 change: 1 addition & 0 deletions tests/Operation/ListIndexesFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function testListIndexesForNewlyCreatedCollection()
foreach ($indexes as $index) {
$this->assertInstanceOf(IndexInfo::class, $index);
$this->assertEquals(['_id' => 1], $index->getKey());
$this->assertSame($this->getNamespace(), $index->getNamespace());
}
}

Expand Down