Skip to content
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

Prevent search index creation with dynamic: false and empty fields mapping #2690

Merged
merged 1 commit into from
Oct 17, 2024
Merged
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
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'])) {
Copy link
Member

Choose a reason for hiding this comment

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

Using array_key_exists() here would most directly address the original bug. Both XmlDriver an Attribute driver should only set these keys if the mappings actually specified a value, so I don't think we need to prevent users from specifying dynamic: false and empty fields.

Copy link
Member Author

@GromNaN GromNaN Oct 16, 2024

Choose a reason for hiding this comment

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

I want to check if $definition['mappings']['dynamic'] is false also.
The condition would be:

Suggested change
if (empty($definition['mappings']['dynamic']) && empty($definition['mappings']['fields'])) {
if (! array_key_exists('mappings', $definition) || ((! array_key_exists('dynamics', $definition['mappings']) || ! $definition['mappings']) && (! array_key_exists('fields', $definition['mappings']) || ! is_array($definition['mappings']['fields']) || count($definition['mappings']['fields']) === 0)) {

It don't think this makes the code more explicit, secure or performant.

Copy link
Member

Choose a reason for hiding this comment

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

My understanding was that the original bug only cared about both options being unspecified, so two array_key_exists() checks would be the minimum change to implement. ODM wasn't prohibiting dynamic: true with a non-empty fields array (I assume the server reports an error there), so I assumed the most minimal validation would be OK.

If you instead want to prohibit dynamic: false and an empty fields array, the original condition was fine (and certainly more readable). Since we know the server doesn't report an error for that, I suppose having ODM validate this will protect users from creating a useless index.

Copy link
Member Author

@GromNaN GromNaN Oct 17, 2024

Choose a reason for hiding this comment

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

I totally messed with github UI patches. I've reverted this change to validate the mappings is correct (dynamic or fields)

throw MappingException::emptySearchIndexDefinition($this->name, $name);
}
Copy link
Member

@jmikola jmikola Oct 15, 2024

Choose a reason for hiding this comment

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

Similar to my question in mongodb/mongo-php-library#1482 (comment), I'm not sure what use case we're supporting here. If dynamic: false is the default (per createSearchIndexes docs), why should we allow creating a search index with no mappings at all?

The CreateSearchIndexes operation and SearchIndexInput, which it utilizes, do not validate this.

By default, when definition.mappings is an empty object, the server fallbacks (sic) to dynamic: true. But we don't want to implement this behavior client-side.

On a separate note, the createSearchIndexes docs themselves say that definition.mappings is an optional document and dynamic: false is the default. If that's inaccurate I think a DOCSP ticket may be warranted.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've completely reworked the PR to throw an exception in case none of dynamic or fields is set.

Copy link
Member

Choose a reason for hiding this comment

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

In this case, should it go back to targeting the 2.9.x branch as a bug fix? I wouldn't consider this new functionality.

Copy link
Member

Choose a reason for hiding this comment

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

@GromNaN: Bumping this. If you're just adding extra validation, is 2.9.x appropriate?

Copy link
Member Author

Choose a reason for hiding this comment

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

Since I'm adding a new validation, I think 2.10.x is better. Currently, indexes with #[SearchIndex(dynamic: false)] are accepted (even if they are useless).


$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