protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.37k stars 15.46k forks source link

[PHP] Problem with serializing Proto objects using igbinary #17289

Open Keleset-Web opened 3 months ago

Keleset-Web commented 3 months ago

I am using PHP 8.3 with the ext-protobuf 3.21.1 extension in the project. OS: Linux

The problem is that when serializing a generated PHP object based on a Protobuf Message, its content is not read. Serialization in the project is performed using igbinary_serialize.

I have two example code fragments: one interacts with a Protobuf object (which inherits from \Google\Protobuf\Internal\Message), and the other interacts with an object created from a standard PHP class. In the first case, igbinary cannot read the value set in the Protobuf object during serialization, whereas in the second case, serialization proceeds without any issues.

First case:

<?php

declare(strict_types=1);

$timestamp = new Google\Protobuf\Timestamp();
$timestamp->setSeconds(1719822351);

$serializedData = igbinary_serialize($timestamp);

echo 'Igbinary serialized data: '.$serializedData.PHP_EOL;
echo 'Set seconds value in object: '.$timestamp->getSeconds().PHP_EOL;

Result:

Igbinary serialized data: Google\Protobuf\Timestamp
Set seconds value in object: 1719822351

Second case:

<?php

declare(strict_types=1);

class Timestamp
{
    protected int $seconds = 0;

    public function setSeconds(int $seconds): void
    {
        $this->seconds = $seconds;
    }

    public function getSeconds(): int
    {
        return $this->seconds;
    }
}

$timestamp = new Timestamp();
$timestamp->setSeconds(1719822351);
$serializedData = igbinary_serialize($timestamp);

echo 'Igbinary serialized data: '.$serializedData.PHP_EOL;
echo 'Set seconds value in object: '.$timestamp->getSeconds().PHP_EOL;

Result:

Igbinary serialized data:       Timestamp
*seconds
f�h
Set seconds value in object: 1719822351

Can I achieve the correct serialization result as shown in the second example?

Similar topics: https://github.com/protocolbuffers/protobuf/issues/14872, https://github.com/protocolbuffers/protobuf/issues/12714, https://github.com/protocolbuffers/protobuf/pull/12718

github-actions[bot] commented 5 days ago

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. This issue will be closed and archived after 14 additional days without activity.

Keleset-Web commented 5 days ago

Hello, the problem is still relevant, I would like to figure it out and find a solution