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

Marshaling empty message in oneof is incompatible with proto.Marshal #61

Closed
jzelinskie opened this issue Sep 17, 2022 · 0 comments · Fixed by #130
Closed

Marshaling empty message in oneof is incompatible with proto.Marshal #61

jzelinskie opened this issue Sep 17, 2022 · 0 comments · Fixed by #130

Comments

@jzelinskie
Copy link

An empty message used in a oneof is not marshaled the same as proto.Marshal().

Steps to reproduce:

syntax = "proto3";
package repro;

message Repro {
  message Empty {}
  oneof str_or_empty {
    string str = 1;
    Empty empty = 2;
  }
}
func TestEmptyOneOf(t *testing.T) {
	m := &pb.Repro{StrOrEmpty: &pb.Repro_Empty_{}}

	protobuf, err := proto.Marshal(m)
	if err != nil {
		t.Fatal(err)
	}

	vtprotobuf, err := m.MarshalVT()
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("protobuf: %#v\n", protobuf)
	fmt.Printf("vtprotobuf: %#v\n", vtprotobuf)

	require.True(t, bytes.Equal(protobuf, vtprotobuf))
}

Output:

protobuf: []byte{0x12, 0x0}
vtprotobuf: []byte{}
--- FAIL: TestEmptyOneOf (0.00s)
    main_test.go:129: 
        	Error Trace:	main_test.go:129
        	Error:      	Should be true
        	Test:       	TestEmptyOneOf
FAIL
exit status 1
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

Successfully merging a pull request may close this issue.

1 participant