Skip to content

Commit

Permalink
Search index mapping must be dynamic or specify a field mapping. (doc…
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 19, 2024
1 parent 50be0f9 commit ba4c740
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -1220,9 +1220,15 @@ public function hasIndexes(): bool
*/
public function addSearchIndex(array $definition, ?string $name = null): void
{
$name ??= self::DEFAULT_SEARCH_INDEX_NAME;

if (empty($definition['mappings']['dynamic']) && empty($definition['mappings']['fields'])) {
throw MappingException::emptySearchIndexDefinition($this->name, $name);
}

$this->searchIndexes[] = [
'definition' => $definition,
'name' => $name ?? self::DEFAULT_SEARCH_INDEX_NAME,
'name' => $name,
];
}

Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,9 @@ public static function nonBackedEnumMapped(string $className, string $fieldName,
$fieldName,
));
}

public static function emptySearchIndexDefinition(string $className, string $indexName): self
{
return new self(sprintf('%s search index "%s" must be dynamic or specify a field mapping', $className, $indexName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,15 @@ public function testDefaultValueForValidationLevel(): void
$cm = new ClassMetadata('stdClass');
self::assertEquals(ClassMetadata::SCHEMA_VALIDATION_LEVEL_STRICT, $cm->getValidationLevel());
}

public function testEmptySearchIndexDefinition(): void
{
$cm = new ClassMetadata('stdClass');

$this->expectException(MappingException::class);
$this->expectExceptionMessage('stdClass search index "default" must be dynamic or specify a field mapping');
$cm->addSearchIndex(['mappings' => []]);
}
}

/** @template-extends DocumentRepository<self> */
Expand Down

0 comments on commit ba4c740

Please sign in to comment.