Clear cached response data when resetting Response object #1473
+68
−56
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request introduces a small but important fix to ensure response consistency across multiple calls to setResponse() within the Elastic\Elasticsearch\Response\Elasticsearch class.
Previously, when a new PSR-7 ResponseInterface was passed to setResponse(), any previously cached data in $asArray, $asObject, or $asString persisted. This could lead to incorrect data being returned if the same Elasticsearch instance was reused.
To resolve this, the following change has been applied:
These lines ensure that any previously serialised body representations are cleared whenever a new response is set.
Why
Without clearing the cached serialised data, the same
Elasticsearchinstance could returnstale results from a previous response. This becomes problematic when multiple responses
are handled in sequence — for example, in integration tests or long-running processes that
reuse the same client instance.
Resetting these cached properties ensures that the object always reflects the latest
response body, maintaining consistency and correctness without affecting public APIs.
This change is small, isolated, and backward compatible, yet it prevents subtle and
potentially confusing data inconsistencies.
Tests Results
I hope this fix will help for !!! 🙌