grpc / grpc-dart

The Dart language implementation of gRPC.
https://pub.dev/packages/grpc
Apache License 2.0
860 stars 271 forks source link

optional in protobuf not working in grpc-dart #599

Closed yingshaoxo closed 1 year ago

yingshaoxo commented 1 year ago

What I wrote

Here is my protobuf code:

syntax = "proto3";
package account_storage_service;

option go_package = "./account_storage_service";

service AccountStorageService {
    rpc CreateUser(CreateUserRequest) returns (CreateUserResponse);
}

message CreateUserRequest {
    optional string email = 1;
}

message CreateUserResponse {
    string result = 1;
    optional string error = 2;
}

Here is my grpc build command:

protoc --dart_out=grpc:lib/generated_grpc --proto_path ../party_protocols/protocols --experimental_allow_proto3_optional account_storage_service.proto

Here is my flutter code:

CreateUserRequest createUserRequest = CreateUserRequest();
createUserRequest.email = updateUserRequest.email;

CreateUserResponse response = await client.createUser(createUserRequest,
          options: get_JWT_CallOptions_for_GRPC());
if (response.error != null && response.error != "") {
    return default_response;
}
image

What I expect

Theemail property in CreateUserRequest should be a null-safe variable, which means it could be null

The error property in CreateUserResponse should be a null-safe variable, which means it could be null

What I see is

They are all not null-able variables, it seems like the compiler ignores the optional string in the protobuf definition.

image image image
yingshaoxo commented 1 year ago

I found someone did a pull, but didn't get merged yet.

https://github.com/google/protobuf.dart/pull/790

mraleph commented 1 year ago

It's a protobuf issue, so it should be handled on protobuf repositiory. I asked protobuf.dart maintainer to take a look at the PR.

That being said: we can't just make these fields nullable because that is going to be massively breaking change. It probably could be an opt-in option for the protoc.