hasOptionalKeyword in FieldDescriptor not working in PHP

71 views
Skip to first unread message

job vink

unread,
May 19, 2023, 3:13:04 AM5/19/23
to Protocol Buffers
Hi,

I am trying to use the FieldDescriptor class in my PHP project, and try to determine which fields of the protobuf message are optional. I'm using the hasOptionalKeyword function on the Google\Protobuf\FieldDescriptor class
 but i'm running into the following error:

 Attempted to call an undefined method named "hasOptionalKeyword" of class "Google\Protobuf\Internal\FieldDescriptor".

It seems the hasOptionalKeyword function calls the internal descriptor with the same hasOptionalKeyword function, but that function is not implemented in the internal class. 

I'm using package google/protobuf version 3.23

Anything I am missing?

Regards,

Job

habe...@google.com

unread,
May 23, 2023, 11:47:03 AM5/23/23
to Protocol Buffers
It looks like the PHP APIs are incomplete. It is definitely a bug that `hasOptionalKeyword()` would return this error.

That said, we are guiding users away from `hasOptionalKeyword()`. Note that it is deprecated in C++: https://github.com/protocolbuffers/protobuf/blob/06e232dcd85ab52dc5e45aa1ea8bcd03f9a9a245/src/google/protobuf/descriptor.h#L976-L981

Instead you should be using something more like `has_presence()` in C++. This function is more future-proof because it tests a semantic property of the field (whether it has presence), rather than a syntactic detail: https://github.com/protocolbuffers/protobuf/blob/06e232dcd85ab52dc5e45aa1ea8bcd03f9a9a245/src/google/protobuf/descriptor.h#L808-L815

So I think the real resolution here is to add `hasPresence()` to PHP. Feel free to file a GitHub issue for that.

Reply all
Reply to author
Forward
0 new messages