-
-
Notifications
You must be signed in to change notification settings - Fork 505
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 The CreateSearchIndexes operation and SearchIndexInput, which it utilizes, do not validate this.
On a separate note, the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
$this->searchIndexes[] = [ | ||
'definition' => $definition, | ||
'name' => $name ?? self::DEFAULT_SEARCH_INDEX_NAME, | ||
'name' => $name, | ||
]; | ||
} | ||
|
||
|
There was a problem hiding this comment.
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 specifyingdynamic: false
and empty fields.There was a problem hiding this comment.
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']
isfalse
also.The condition would be:
It don't think this makes the code more explicit, secure or performant.
There was a problem hiding this comment.
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 prohibitingdynamic: true
with a non-emptyfields
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 emptyfields
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.There was a problem hiding this comment.
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)