-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
jsonpb: support unmarshaling enums as map values #644
Comments
I just realized why existing tests don't catch this. It's only when the enum is the value of a map field. Here's a simple repro: // in golang/protobuf/proto/test_proto/test.proto, edit GoEnum to look like so:
message GoEnum {
required FOO foo = 1;
map<string, FOO> foos = 2;
} // Add these test cases to golang/protobuf/jsonpb/jsonpb_test.go
func Test644Unmarshal(t *testing.T) {
js := `{"foo":"FOO1","foos":{"abc":"FOO1"}}`
var m test_proto.GoEnum
err := UnmarshalString(js, &m)
if err != nil {
t.Fatalf(err.Error())
}
expected := map[string]test_proto.FOO{"abc": test_proto.FOO_FOO1}
if !reflect.DeepEqual(m.Foos, expected) {
t.Errorf("failed to de-serialize as expected: wanted %v; got %v", expected, m.Foos)
}
}
func Test644Marshal(t *testing.T) {
m := &test_proto.GoEnum{
Foo: test_proto.FOO_FOO1.Enum(),
Foos: map[string]test_proto.FOO{"abc": test_proto.FOO_FOO1},
}
var jsm Marshaler // Note: EnumsAsInts has defaulted to *false*
s, err := jsm.MarshalToString(m)
if err != nil {
t.Fatal(err.Error())
}
// how it used to serialize, before PR 630
expected := `{"foo":"FOO1","foos":{"abc":"FOO1"}}`
if s != expected {
t.Errorf("failed to serialize as expected: wanted `%s`; got `%s`", expected, s)
}
} The former (unmarshal) blows up with a parse error where it shouldn't. The latter emits the map value |
According to https://developers.google.com/protocol-buffers/docs/proto3#json, all integers & floats are accepted as strings.
I'll take a look at this as soon as possible. |
@jhump The issue is not only cause by PR #630, actually:
So to fix the unmarshalling issue, IMO, we should fix For the marshalling issue, I think it's due to the I'm not an expert of protobuf & jsonpb, @dsnet how should we handle this? |
Actually |
@kassiansun, ah, sorry for misattributing that to your PR. I was mistakenly thinking that my own protoreflect test's would have failed with that JSON output, and my own tests didn't turn red until #630 landed. I was mistaken: my tests only broke due to the unmarshalling bug. |
@jhump No problem ;-) |
@dsnet: I acknowledged above that the marshaling part of this bug report (where enum in maps are always marshalled as ints, regardless of the However, the unmarshalling bit was newly introduced. My project, which tests JSON serialization (which includes using The cause of my project's test failures was an error during unmarshalling that should not have happened (e.g. the JSON being parsed was valid). So of the two repro test cases I pasted above, only the first was actually caused recently. |
In PR #630, it looks like unmarshalling of enums from name strings was broken.
It began to break JSON-related tests in my [https://github.com/jhump/protoreflect] project. It also looks like that change may have caused enum marshaling to default to numeric values instead of name strings.
The text was updated successfully, but these errors were encountered: