Skip to content
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

PHP: use __debugInfo in Message to fix endless var_dump #12714

Closed
bshaffer opened this issue May 8, 2023 · 2 comments
Closed

PHP: use __debugInfo in Message to fix endless var_dump #12714

bshaffer opened this issue May 8, 2023 · 2 comments
Assignees
Labels
inactive Denotes the issue/PR has not seen activity in the last 90 days.

Comments

@bshaffer
Copy link
Contributor

bshaffer commented May 8, 2023

Debugging a Protobuf Message Object

Calling var_dump or print_r on a protobuf message with the Protobuf C extension enabled results very limited, not very useful output:

// Calling var_dump on a Protobuf message (extension enabled):
php > var_dump(new Google\Protobuf\Timestamp());
object(Google\Protobuf\Timestamp)#1 (0) {
}

// Calling print_r on a Protobuf message (extension enabled):
php > var_dump(new Google\Protobuf\Timestamp());
Google\Protobuf\Timestamp Object

When using the Protobuf native library, calls to var_dump and print_r are hard to read and filled with noise:

// Calling var_dump on a Protobuf message (extension disabled):
php > var_dump(new Google\Protobuf\Timestamp());
object(Google\Protobuf\Timestamp)#2 (4) {
  ["desc":"Google\Protobuf\Internal\Message":private]=>
  object(Google\Protobuf\Internal\Descriptor)#424 (13) {
    ["full_name":"Google\Protobuf\Internal\Descriptor":private]=>
    string(25) "google.protobuf.Timestamp"
    ["field":"Google\Protobuf\Internal\Descriptor":private]=>
    array(2) {
      [1]=>
      object(Google\Protobuf\Internal\FieldDescriptor)#428 (14) {
        ["name":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        string(7) "seconds"
        ["json_name":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        string(7) "seconds"
        ["setter":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        string(10) "setSeconds"
        ["getter":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        string(10) "getSeconds"
        ["number":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        int(1)
        ["label":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        int(1)
        ["type":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        int(3)
        ["message_type":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        NULL
        ["enum_type":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        NULL
        ["packed":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        bool(false)
        ["oneof_index":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        int(-1)
        ["proto3_optional":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        bool(false)
        ["containing_oneof":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        NULL
        ["public_desc":"Google\Protobuf\Internal\FieldDescriptor":private]=>
        object(Google\Protobuf\FieldDescriptor)#429 (1) {
          ["internal_desc":"Google\Protobuf\FieldDescriptor":private]=>
          *RECURSION*
        }
      }
// ...

// Calling print_r on a Protobuf message (extension disabled):
php > var_dump(new Google\Protobuf\Timestamp());
Google\Protobuf\Timestamp Object
(
    [desc:Google\Protobuf\Internal\Message:private] => Google\Protobuf\Internal\Descriptor Object
        (
            [full_name:Google\Protobuf\Internal\Descriptor:private] => google.protobuf.Timestamp
            [field:Google\Protobuf\Internal\Descriptor:private] => Array
                (
                    [1] => Google\Protobuf\Internal\FieldDescriptor Object
                        (
                            [name:Google\Protobuf\Internal\FieldDescriptor:private] => seconds
                            [json_name:Google\Protobuf\Internal\FieldDescriptor:private] => seconds
                            [setter:Google\Protobuf\Internal\FieldDescriptor:private] => setSeconds
                            [getter:Google\Protobuf\Internal\FieldDescriptor:private] => getSeconds
                            [number:Google\Protobuf\Internal\FieldDescriptor:private] => 1
                            [label:Google\Protobuf\Internal\FieldDescriptor:private] => 1
                            [type:Google\Protobuf\Internal\FieldDescriptor:private] => 3
                            [message_type:Google\Protobuf\Internal\FieldDescriptor:private] => 
                            [enum_type:Google\Protobuf\Internal\FieldDescriptor:private] => 
                            [packed:Google\Protobuf\Internal\FieldDescriptor:private] => 
                            [oneof_index:Google\Protobuf\Internal\FieldDescriptor:private] => -1
                            [proto3_optional:Google\Protobuf\Internal\FieldDescriptor:private] => 
                            [containing_oneof:Google\Protobuf\Internal\FieldDescriptor:private] => 
                            [public_desc:Google\Protobuf\Internal\FieldDescriptor:private] => Google\Protobuf\FieldDescriptor Object
                                (
                                    [internal_desc:Google\Protobuf\FieldDescriptor:private] => Google\Protobuf\Internal\FieldDescriptor Object
 *RECURSION*
//...

This problem is even worse when debugging instances of RepeatedField, as the result is never-ending.

The fix for this would involve adding support for __debugInfo in the Message class:

    public function __debugInfo()
    {
        return (array) json_decode($this->serializeToJsonString(), true);
    }

For better handling , we can add support for checking specific types (e.g. Google\Protobuf\Timestamp, which returns a string for serializeToJsonString) and adding __debugInfo() for specific classes such as RepeatedField.

@bshaffer bshaffer added the untriaged auto added to all issues by default when created. label May 8, 2023
copybara-service bot pushed a commit that referenced this issue Oct 17, 2023
addresses #12714 by dumping more concise debug info for protobuf messages and repeated fields via the `serializeToJsonString` function. Additionally, message types which serialize into something other than an array (e.g. `Google\Protobuf\Value`, `Google\Protobuf\Timestamp`, etc) are handled in a special way to make their output consistent with other messages.

```php
$m = new Google\Protobuf\DoubleValue();
$m->setValue(1.5);
var_dump($m);
```
will output
```
object(Google\Protobuf\DoubleValue)#12 (1) {
  ["value"]=>
  float(1.5)
}
```

Closes #12718

COPYBARA_INTEGRATE_REVIEW=#12718 from bshaffer:php-add-debuginfo c40a6f9
PiperOrigin-RevId: 574115431
Copy link

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago.

@github-actions github-actions bot added the inactive Denotes the issue/PR has not seen activity in the last 90 days. label Nov 12, 2023
Copy link

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 27, 2023
@googleberg googleberg removed the untriaged auto added to all issues by default when created. label Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inactive Denotes the issue/PR has not seen activity in the last 90 days.
Projects
None yet
Development

No branches or pull requests

2 participants