Skip to content

Commit

Permalink
fix: do not mutate BoolQuery instance in toArray method (#2241)
Browse files Browse the repository at this point in the history
fix: do not mutate BoolQuery instance for serializing when `$this->_params` is empty - #1450

---------

Co-authored-by: Marek Hauschwitz <[email protected]>
  • Loading branch information
MrkMrk00 and hauschwitz authored Feb 14, 2025
1 parent 6d122c6 commit b37b636
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated
### Removed
### Fixed
* `Elastica\Query\BoolQuery::toArray` no longer changes `$this->_params` to \stdClass when empty [#2241](https://github.com/ruflin/Elastica/pull/2241)
### Security

## [8.1.0](https://github.com/ruflin/Elastica/compare/8.0.0...8.1.0)
Expand Down
10 changes: 7 additions & 3 deletions src/Query/BoolQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ public function setMinimumShouldMatch($minimum): self

public function toArray(): array
{
if (!$this->_params) {
$this->_params = new \stdClass();
$data = parent::toArray();

// When the query has no params, ensure that the query
// serializes into a JSON object instead of an empty array.
if (!$data[$this->_getBaseName()]) {
$data[$this->_getBaseName()] = new \stdClass();
}

return parent::toArray();
return $data;
}

/**
Expand Down

0 comments on commit b37b636

Please sign in to comment.