protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Field descriptor isOptional returns true for non-optional marked field

Open clehene opened this issue 1 year ago • 1 comments

What version of protobuf and what language are you using? 3.20.1 Java

What operating system (Linux, Windows, ...) and version? mac

What runtime / compiler are you using (e.g., python version or gcc version) java 18

What did you do?

message x {
fixed64 id = 5;
}
builder.build().getDescriptorForType().findFieldByName("id").isOptional()

What did you expect to see

false

What did you see instead?

true or otherwise there should be a different method to check for fieldPresence. I realize this may be checking for the pb2 optional which in pb3 is by default true?
However how would then check for field presence in field descriptor? I understand optional is implemented with oneof but there's no isOneOf either.

Use case I'm trying to validate if field has been set, but I want to only validate against optional marked fields to avoid ambiguous checks.

clehene avatar Sep 13 '22 23:09 clehene

Have you tried getting the value of https://github.com/protocolbuffers/protobuf/blob/db38a8c2dac34daa2d64bf904216997525617abf/src/google/protobuf/descriptor.proto#L242 ?

shaod2 avatar Sep 20 '22 21:09 shaod2