You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have noticed that when I send empty array as json post request, it will get translated to nil in the grpc handler. Again, i am not sending json null, just empty array([]). The generated struct from proto has omitempty, as normal, but that should come to play only for null value.
I haven't noticed this anywhere else, yet, but this route has only one field so maybe the behavior si different?
edit: when i intercept the http body and read it into string i can clearly see the json being properly formatted and is empty array, not null, so what is being sent by the browser is being received by the http server, so the bug happens when the body is being marshalled by jsonpb and passed along to the grpc handler.
edit: even if I replace the default marshaler with mine, it gets me the same result defaultMarshaler = &JSONPb{OrigName: true} -> runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true, Indent: " "}),
The text was updated successfully, but these errors were encountered:
Hi Ivan, thanks for your interest in the project! As you have surmised, this is down to the JSON marshaller you configure. Note that the protobuf standard doesn't allow you to differentiate an empty array and an unset array. If you want nullability within the standard you have to wrap it in a message. However, I can see that being annoying, so another alternative, as you've probably concluded, is to write your own marshaler. Even if we wanted to change the default marshaller to handle this case, it's not within our domain, as it is maintained by the official Go protobuf team at Google. In any case, this is something the gRPC-gateway lets you do with the configuration option you already found, so if you absolutely must have nullability in this case, your options are;
ah, ok, i see. so this is pb issue, not json. i mean, i have been using pb wrappers for ages but i don't know why it never occurred to me to differentiate empty and nil arrays. i guess it never came up, until now. thanks.
..
ok, so i guess it took some years for this to bite me in the ass 😂 https://play.golang.org/p/Gnif8sKvNJb
I have noticed that when I send empty array as json post request, it will get translated to nil in the grpc handler. Again, i am not sending json null, just empty array([]). The generated struct from proto has omitempty, as normal, but that should come to play only for null value.
I haven't noticed this anywhere else, yet, but this route has only one field so maybe the behavior si different?
edit: when i intercept the http body and read it into string i can clearly see the json being properly formatted and is empty array, not null, so what is being sent by the browser is being received by the http server, so the bug happens when the body is being marshalled by jsonpb and passed along to the grpc handler.
edit: even if I replace the default marshaler with mine, it gets me the same result
defaultMarshaler = &JSONPb{OrigName: true}
->runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true, Indent: " "}),
The text was updated successfully, but these errors were encountered: