From b37b6365b035149c82a08ad8f4ab666aeda07ea7 Mon Sep 17 00:00:00 2001 From: Marek Hauschwitz <79979498+MrkMrk00@users.noreply.github.com> Date: Fri, 14 Feb 2025 12:24:25 +0100 Subject: [PATCH] fix: do not mutate BoolQuery instance in toArray method (#2241) fix: do not mutate BoolQuery instance for serializing when `$this->_params` is empty - https://github.com/ruflin/Elastica/issues/1450 --------- Co-authored-by: Marek Hauschwitz --- CHANGELOG.md | 1 + src/Query/BoolQuery.php | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 121518180..cff9a7fdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/Query/BoolQuery.php b/src/Query/BoolQuery.php index 89a8325f8..68d0074bf 100644 --- a/src/Query/BoolQuery.php +++ b/src/Query/BoolQuery.php @@ -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; } /**