Skip to content

TextFormat and JsonFormat ignore experimental proto3 optional enums #7480

Closed

Description

@thesamet

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.

Activity

  1. changed the title [-]TextFormat and JsonFormat ignores experimental proto3 optional enums[/-] [+]TextFormat and JsonFormat ignore experimental proto3 optional enums[/+] on May 8, 2020
  2. haberman commented on May 10, 2020

    @haberman
    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.

  3. haberman commented on May 13, 2020

    @haberman
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions