Been looking for this for a few and I am not getting a definitive answer, I am kinda confused and I want to make sure I can make a good implementation moving forward. I am using Nest.js and gRPC as transport between microservices. The issue arises on the responses. My service x calls to service y for a list of items and when I do a console log on my service y I get the following answer:
{
"data": [
{
"name": "user 1",
"email": "test@email.com",
"phone": null
}
]
}
and this gets passed as it to the gRPC protobuf file that looks like this
message userModel {
string name = 1;
string email = 2;
string phone = 3;
}
message userResponse {
repeated userModel user = 1;
}
When I receive the answer on service x I receive it like this:
{
"data": [
{
"name": "user 1",
"email": "test@email.com"
}
]
}
it happens with all null values, I never get them in the response, so I was wondering why is this happening?
This is just a small example to explain what is happening, my models are pretty large and it's a weird behavior. Thank you in advance to anyone that can offer a solution or light on this matter.
By default, Protobuf scalar fields cannot be null
. Instead, the null
value will be mapped to the default value, which is the empty string for string fields. Default values may be omitted from deserialized objects, which would explain what you are seeing here. Depending on what protobuf implementation you are using, there may be an option to include default values in deserialized messages. Alternatively, you can declare that the field is optional
to preserve the null
value.