Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextFormat and JsonFormat ignore experimental proto3 optional enums #7480

Closed
thesamet opened this issue May 8, 2020 · 2 comments
Closed

Comments

@thesamet
Copy link
Contributor

thesamet commented May 8, 2020

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.

@thesamet thesamet changed the title TextFormat and JsonFormat ignores experimental proto3 optional enums TextFormat and JsonFormat ignore experimental proto3 optional enums May 8, 2020
@haberman
Copy link
Member

Thank you for the report, and for trying out a prerelease version! I have a fix for this coming shortly. I will make sure it is in the 3.12.0 release.

@haberman
Copy link
Member

The fix for this is released in https://github.com/protocolbuffers/protobuf/releases/tag/v3.12.0-rc2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants