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

[proto3] Default value for message is null? #1973

Closed
biran0079 opened this issue Jun 24, 2016 · 7 comments
Closed

[proto3] Default value for message is null? #1973

biran0079 opened this issue Jun 24, 2016 · 7 comments

Comments

@biran0079
Copy link

From this doc, default value for message field is null, while from the generated code, it seems the DEFAULT_INSTANCE of the message is returned, which is not null.
Is the doc wrong?

@biran0079
Copy link
Author

According to protocolbuffers/protobuf#359, message field can be null.
It seems to me that protobuf for java is not implemented correctly?

@jhspaybar
Copy link
Contributor

For what it's worth, this caught me off guard as well. I just use the hasX() messages now when I want to test if a message was set or not. It may be better to open this issue against the protobuf library as well as gRPC just creates a compiler plugin but isn't the core project for how Java protobuf works.

@biran0079 biran0079 changed the title Default value for message is null? [proto3] Default value for message is null? Jun 27, 2016
@biran0079
Copy link
Author

@jhspaybar: I actually meant proto version 3, sorry for confusion.
hasX() method is not even generated in proto3.
It should work correctly for proto2 though.

@ejona86
Copy link
Member

ejona86 commented Jun 27, 2016

Requests and responses themselves will be non-null, as zero bytes is actually a valid encoding for a message. Nested messages can be null though, because field presence is possible in the wire format (since nested messages a length-prefixed). Field presence (hasx()) is gone in proto3 for primitives but still exists for messages, to align with and gain efficiency in many programming languages (C++, Java, Go, C#).

@biran0079
Copy link
Author

@ejona86: thanks for the detailed explanation!
I didn't notice that hasX() is still present for nested messages.
There is still one thing I am confused: getX() seems never return null even when the field "is null".

message TestBool {
  bool inner = 1;
}
message TestWrapper {
  TestBool outer = 1;
}

generates

public TestBool getOuter() {
  return outer_ == null ? TestBool.getDefaultInstance() : outer_;
}

Is that intended?

@ejona86
Copy link
Member

ejona86 commented Jun 28, 2016

@biran0079, hmmm... I would have expected it to be null. But it does appear to be on purpose. That'd be a question for protobuf.

@biran0079
Copy link
Author

I filed protocolbuffers/protobuf#1718, and it seems the documentation is wrong.

So in proto3, hasX() can tell if a message field is set or not, and getX() will return the default instance of the proto if unset.

@ejona86 ejona86 closed this as completed Aug 22, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Sep 22, 2018
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