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

optional nullable is not working #9868

Closed
yingshaoxo opened this issue Feb 2, 2023 · 3 comments
Closed

optional nullable is not working #9868

yingshaoxo opened this issue Feb 2, 2023 · 3 comments

Comments

@yingshaoxo
Copy link

Generator version: 1.52.1

https://repo1.maven.org/maven2/io/grpc/protoc-gen-grpc-java/1.52.1/protoc-gen-grpc-java-1.52.1-osx-x86_64.exe

Generate command:

cd .gradle
wget https://repo1.maven.org/maven2/io/grpc/protoc-gen-grpc-java/1.52.1/protoc-gen-grpc-java-1.52.1-osx-x86_64.exe
chmod 777 protoc-gen-grpc-java-1.52.1-osx-x86_64.exe
cd ..

mkdir -p app/src/main/java/generated_grpc

protoc --experimental_allow_proto3_optional --plugin=protoc-gen-grpc-java=.gradle/protoc-gen-grpc-java-1.52.1-osx-x86_64.exe \
--java_out=app/src/main/java --grpc-java_out=app/src/main/java \
--proto_path=../party_protocols/protocols free_map_service.proto

Protocols

syntax = "proto3";
package free_map_service;

option go_package = "./free_map_service";

option java_package = "generated_grpc.free_map_service_grpc";
option java_outer_classname = "free_map_service_grpc_types";

service FreeMapService {
    rpc DeletePlace(DeleteRequest) returns (DeleteResponse);
}

message DeleteRequest {
    int32 location_id = 1;
}

message DeleteResponse {
    optional string error = 1;
    bool success = 2;
}

Kotlin code

    override fun deletePlace(
        request: DeleteRequest?,
        responseObserver: StreamObserver<DeleteResponse?>?
    ) {
        responseObserver?.onNext(DeleteResponse.newBuilder()
            .setError(null).setSuccess(true)
            .build())
        responseObserver?.onCompleted()
    }

image

Error

As I tested out, if I use setError(""), there would have no error.

But if I use setError(null), error happens.

I also tested --experimental_allow_proto3_optional, not working.

Feb 02, 2023 11:11:50 AM io.grpc.internal.SerializingExecutor run
SEVERE: Exception while executing runnable io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$1HalfClosed@587ff7a5
java.lang.NullPointerException
	at generated_grpc.free_map_service_grpc.free_map_service_grpc_types$DeleteResponse$Builder.setError(free_map_service_grpc_types.java:5740)
	at kotlin_free_map_backend_system.FreeMapService.deletePlace(FreeMapService.kt:32)
	at generated_grpc.free_map_service_grpc.FreeMapServiceGrpc$MethodHandlers.invoke(FreeMapServiceGrpc.java:351)
	at io.grpc.stub.ServerCalls$UnaryServerCallHandler$UnaryServerCallListener.onHalfClose(ServerCalls.java:182)
	at io.grpc.internal.ServerCallImpl$ServerStreamListenerImpl.halfClosed(ServerCallImpl.java:340)
	at io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$1HalfClosed.runInContext(ServerImpl.java:866)
	at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
	at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)

image

image

@sanjaypujare
Copy link
Contributor

optional in proto3 does not imply nullable for the equivalent field in the generated Java (or Kotlin) code. Specifically you should not call setError(null) just because it is marked optional.

optional in proto3 means you can check on the receiving side to see if the value was explicitly set as stated in Specifying Field Rules.

@yingshaoxo
Copy link
Author

yingshaoxo commented Feb 2, 2023 via email

@ejona86
Copy link
Member

ejona86 commented Feb 2, 2023

This seems really about the protobuf message codegen, so https://github.com/protocolbuffers/protobuf is the place you'd need to file an issue. (If it is an RPC issue then it is gRPC, if it is a message issue it is protobuf.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants