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

Empty json array is passed as nil #1673

Closed
ivanjaros opened this issue Sep 16, 2020 · 2 comments
Closed

Empty json array is passed as nil #1673

ivanjaros opened this issue Sep 16, 2020 · 2 comments

Comments

@ivanjaros
Copy link

ivanjaros commented Sep 16, 2020

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?

message SetBlacklistRequest {
	repeated string names = 1;
}

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: " "}),

@johanbrandhorst
Copy link
Collaborator

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;

  1. Wrap it in a message
  2. Write your own marshaler.

I hope that helps!

@ivanjaros
Copy link
Author

ivanjaros commented Sep 17, 2020

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

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