php-protobuf icon indicating copy to clipboard operation
php-protobuf copied to clipboard

Empty objects

Open sm2017 opened this issue 10 years ago • 6 comments

In this proto

message MyProto {
    User user = 1;
    float id = 2;
}

If I create new object of MyProto and set user and empty User object , when I printDebugString , Ican see empty object in user property But if I serializeToString and parseFromString and then printDebugString , there is no empty object and user is null Why? How can I pass empty object ?

sm2017 avatar Aug 10 '16 06:08 sm2017

Could you please clarify what do you mean when you say 'empty User object'?

serggp avatar Aug 10 '16 07:08 serggp

Assume we have this proto and compile it

message User{
      string name = 1;
      string family= 2;
}

the empty user object is

$user = new User;

not empty user object is

$user = new User;
$user->setName('Joe');

sm2017 avatar Aug 10 '16 09:08 sm2017

Any idea?

sm2017 avatar Aug 13 '16 07:08 sm2017

During serialization empty objects are skipped. I agree with point that you have different object state after parsing which shouldn't be the case. I will fix it.

hjagodzinski avatar Aug 13 '16 10:08 hjagodzinski

@hjagodzinski Skipping such objects during serialization is ok unless it's a required field. This object should be recreated based on default values during parseFromString().

serggp avatar Aug 15 '16 04:08 serggp

So I checked C++ implementation and fixed serialization method using the same approach. See pull request https://github.com/allegro/php-protobuf/pull/87

serggp avatar Aug 17 '16 00:08 serggp