Skip to content

fix(storage): add null check for $stream->getMetadata#9071

Open
mateuszanella wants to merge 5 commits intogoogleapis:mainfrom
mateuszanella:fix/foreach-null-bug
Open

fix(storage): add null check for $stream->getMetadata#9071
mateuszanella wants to merge 5 commits intogoogleapis:mainfrom
mateuszanella:fix/foreach-null-bug

Conversation

@mateuszanella
Copy link
Copy Markdown

In the instance that a Stream object does not contain a wrapper_data metatada entry, the function returns null and throws the following exception when trying to read the size of the stream:

foreach() argument must be of type array|object, null given "exception":"[object] (ErrorException(code: 0): foreach() argument must be of type array|object, null given at /var/www/api/vendor/google/cloud-storage/src/ReadStream.php:63)

This PR introduces a simple check to see if the response from the metadata call is in fact an array, and returns 0 otherwise (following the default function behavior).

To be honest, I am not really sure why there is no size metadata for the stream, but I believe that this case should be handled, either by just returning 0 or by throwing a proper exception.

@mateuszanella mateuszanella requested review from a team as code owners April 1, 2026 17:50
@product-auto-label product-auto-label bot added the api: storage Issues related to the Cloud Storage API. label Apr 1, 2026
@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 1, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@mateuszanella
Copy link
Copy Markdown
Author

Perhaps is_iterable() would be best, but I'm open to feedback.

@Hectorhammett
Copy link
Copy Markdown
Collaborator

Hello @mateuszanella!

Thanks so much for your contribution. I think this is a simple fix, although we are missing the CLA. If we wish to merge this into the code, we require a signed CLA, more info here:
https://github.com/googleapis/google-cloud-php/blob/main/CONTRIBUTING.md

Other than that, it does look like a good and simple fix, but may I suggest if we can add a unit test for this to keep it consistent?

@mateuszanella
Copy link
Copy Markdown
Author

Done :)

foreach ($this->stream->getMetadata('wrapper_data') as $value) {
$metadata = $this->stream->getMetadata('wrapper_data');

if (!is_array($metadata)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be better to call this is_null. We should be more specific with our handling. If something that isn't null and isn't an array is returned, it's okay if the function errors (because that case is unknown, and so we should handle it).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah, that makes sense. Fixed it!

@mateuszanella mateuszanella requested a review from bshaffer April 2, 2026 01:34
Copy link
Copy Markdown

@luizautentique luizautentique left a comment

Choose a reason for hiding this comment

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

very cool implementation @mateuszanella.

I think this will help so much.

Copy link
Copy Markdown

@guilherme-luiz-cella guilherme-luiz-cella left a comment

Choose a reason for hiding this comment

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

このプルリクエストはとても良いです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants