-
Notifications
You must be signed in to change notification settings - Fork 769
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
HttpApi doesn't write empty strings #1213
Comments
Empty string is the default value and default values aren't written by Protobuf serializer or is equivilent JSON serializer. You could try making the property type |
I see, with
Is this the expected behavior for HttpApi? For typed languages (e.g. C#) this doesn't matter since it's using the default value, but for untyped languages (e.g. JavaScript) this means you'll always get A workaround would be to change everything to |
I think so but I haven't compared against grpc-gateway to see what it does (HttpApi is based on it). |
Closing as answered |
I thought you wanted to compare against grpc-gateway to see what it did, excuse me for not answering. grpc-gateway had an open issue about this as well. In v2 they emit default values. References: If HttpApi doesn't want to omit default values by default: currently the If it were possible to provide your own JsonFormatter, then the settings services.AddGrpcSwagger(config =>
{
config.JsonFormatter = new JsonFormatter(new JsonFormatter.Settings(true));
}); According to the docs the default values should then be written: https://developers.google.com/protocol-buffers/docs/reference/csharp/class/google/protobuf/json-formatter/settings |
It looks like emitting default values is on by default in grpc-gateway V2. I didn't know it had changed. In that case it should be turned on in HttpApi. |
Done. I'm not sure when the package on NuGet will next be updated but you should be able to get the changes by adding this feed and using the latest version: https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json |
Alright, thanks! 😄 |
how to turn it on to not emit the default value in json response? i am currently use go and find no way to do this |
What version of gRPC and what language are you using?
What operating system (Linux, Windows,...) and version?
What runtime / compiler are you using (e.g. .NET Core SDK version
dotnet --info
)What did you do?
I've created a gRPC service that returns the following message:
After that I've followed https://docs.microsoft.com/en-us/aspnet/core/grpc/httpapi to create a REST API for the service.
What did you expect to see?
Messages with all their fields.
What did you see instead?
Messages with missing fields.
When an empty string is being written, the field is omitted. On the client-side this would cause the value to be
null
(orundefined
in JavaScript), while it should be an empty string.Screenshot:
The text was updated successfully, but these errors were encountered: