Nullable value in gRPC, PROTO3, .NET 7.0
Asked Answered
S

2

6

I work on .NET core 7.0

syntax = "proto3";

option csharp_namespace = "IndustryAPI.Protos";

message ListOfStrings {
    repeated string strings = 1;
}

enum VendorMode {
  VendorMode_UNSPECIFIED = 0;
  Shopping = 1;
  Discovery = 2;
  ShoppingAndDiscovery = 3;
}

message VendorModel {
    string Id = 1;
    string Name = 2;
    string Channel  = 3;
    string Address = 4;
    string ContactNummber = 5;
    string Email = 6;
    string Web = 7;
}

service VendorService {
    rpc GetVendorBasicDetails(GetVendorBasicDetailsRequest) returns (GetVendorBasicDetailsResponse);
}

// requests
message GetVendorBasicDetailsRequest {
    string channelId = 1;
    string VendorCreatedDateStart = 2;
    string VendorCreatedDateEnd = 3;
}

message GetVendorBasicDetailsResponse {
    repeated VendorModel Vendor = 1;
}

this is vendor.proto file.

In this model, web field is nullable value.

So, thats return error.

Exception was thrown by handler. ArgumentNullException: Value cannot be null

how to I define nullable field?

Slice answered 19/4, 2023 at 4:46 Comment(0)
S
7

I found the solution.

If we set a nullable field, use Google's Wrapper.proto file:

import "google/protobuf/wrappers.proto";

message MyMessage {
  google.protobuf.StringValue web = 7;
}
Slice answered 19/4, 2023 at 6:2 Comment(0)
T
0

With the use of Wrapper.proto file, the result would have escaped characters in the response actual response.

So it does not resolve the handling of nullable field particularly in "string" type.

gRPC protobuf highly suggest to explicitly use different strategy to handle such scenarios in proper handling of null

Tadashi answered 31/12, 2023 at 1:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.