TextFormat and JsonFormat ignore experimental proto3 optional enums #7480
Description
What version of protobuf and what language are you using?
Version: 3.12.0-rc-1
Language: Java
What operating system (Linux, Windows, ...) and version?
Ubuntu 19.10
What runtime / compiler are you using (e.g., python version or gcc version)
openjdk version "1.8.0_252"
What did you do?
Steps to reproduce the behavior:
Define proto:
syntax = "proto3";
package mypkg;
enum V {
A = 0;
B = 1;
C = 2;
}
message Msg {
optional V v = 1;
}
In code:
import mypkg.Np.Msg;
import com.google.protobuf.TextFormat;
import com.google.protobuf.util.JsonFormat;
import java.lang.String;
public class Main {
public static void main(String[] args) throws Exception {
Msg withV = Msg.newBuilder().setV(mypkg.Np.V.B).build();
// prints false, excepted true
System.out.println(TextFormat.parse(withV.toString(), Msg.class).hasV());
String js = JsonFormat.printer().print(withV);
Msg.Builder b = Msg.newBuilder();
JsonFormat.parser().merge(js, b);
// prints false, excepted true
System.out.println(b.build().hasV());
}
}
Generated protos using protoc with --experimental_allow_proto3_optional
.
What did you expect to see
When serializing the message withV
above into ascii format or json, the optional field correctly shows up. The expectation is that v
would be present when the json/ascii is parsed back.
What did you see instead?
However, when parsing both the json or text representation, the optional field gets ignored: hasV
returns false in both cases, despite that the enum appears in the textual presentation.