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

HttpApi doesn't write empty strings #1213

Closed
GerardSmit opened this issue Feb 23, 2021 · 9 comments · Fixed by aspnet/AspLabs#334
Closed

HttpApi doesn't write empty strings #1213

GerardSmit opened this issue Feb 23, 2021 · 9 comments · Fixed by aspnet/AspLabs#334
Labels
bug Something isn't working

Comments

@GerardSmit
Copy link

Not sure if I need to make an issue about HttpApi here. #167 is for feedback, however there are 47 people subscribed to the thread which I don't want to notify. Also https://github.com/aspnet/AspLabs has issues disabled which brings me here.

What version of gRPC and what language are you using?

Grpc.AspNetCore						: 2.32.0
Microsoft.AspNetCore.Grpc.HttpApi	: 0.1.0-alpha.20580.2
Microsoft.AspNetCore.Grpc.Swagger	: 0.1.0-alpha.20580.2

What operating system (Linux, Windows,...) and version?

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64

What runtime / compiler are you using (e.g. .NET Core SDK version dotnet --info)

.NET SDK (reflecting any global.json):
 Version:   5.0.100
 Commit:    5044b93829

Host (useful for support):
  Version: 5.0.0
  Commit:  cf258a14b7

What did you do?

I've created a gRPC service that returns the following message:

message GuildUser {
    uint64 id = 1;
    string username = 2;
    string discriminator = 3;
    string nickname = 4;
}

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 (or undefined in JavaScript), while it should be an empty string.

Screenshot:
image

@GerardSmit GerardSmit added the bug Something isn't working label Feb 23, 2021
@JamesNK
Copy link
Member

JamesNK commented Feb 23, 2021

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 StringValue.

@GerardSmit
Copy link
Author

I see, with StringValue I get the expected result.

default values aren't written by Protobuf serializer or is equivilent JSON serializer

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 undefined, even with numbers.

A workaround would be to change everything to XValue (e.g. int32 to Int32Value) but I'm not sure if that's the most prettiest solution...

@JamesNK
Copy link
Member

JamesNK commented Feb 24, 2021

Is this the expected behavior for HttpApi

I think so but I haven't compared against grpc-gateway to see what it does (HttpApi is based on it).

@JamesNK
Copy link
Member

JamesNK commented Mar 1, 2021

Closing as answered

@JamesNK JamesNK closed this as completed Mar 1, 2021
@GerardSmit
Copy link
Author

GerardSmit commented Mar 1, 2021

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:
Issue: grpc-ecosystem/grpc-gateway#233
V2 release planning: grpc-ecosystem/grpc-gateway#1223
PR enabling default values: grpc-ecosystem/grpc-gateway#1377.


If HttpApi doesn't want to omit default values by default: currently the UnaryServerCallHandler always uses the default formatter (JsonFormatter.Default):

https://github.com/aspnet/AspLabs/blob/07ebdf55d59814a85ec0efaded572e88729cc04c/src/GrpcHttpApi/src/Microsoft.AspNetCore.Grpc.HttpApi/UnaryServerCallHandler.cs#L278

If it were possible to provide your own JsonFormatter, then the settings FormatDefaultValues could be enabled:

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

@JamesNK JamesNK reopened this Mar 1, 2021
@JamesNK
Copy link
Member

JamesNK commented Mar 1, 2021

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.

@JamesNK
Copy link
Member

JamesNK commented Mar 1, 2021

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

@GerardSmit
Copy link
Author

Alright, thanks! 😄

@ghost
Copy link

ghost commented Jul 29, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants