Protobuf: allow unpopulated required fields when `comparingExpectedFieldsOnly`
See original GitHub issueCurrent behaviour
Right now, when a ProtoSubject.comparingExpectedFieldsOnly() is called, the actual message will be rebuilt before an assertion. This is logical and expected.
However, the message is rebuilt using the build() method. The method ensures that all the required fields are set. In the case of Proto 3, this causes no actual checks. But in the case of Proto 2, there may be some missing fields that were simply not the point of the assertion. The check will fail and the assertion causes an UninitializedMessageException.
Note that the check is meaningless. It runs on a subset of fields which were selected from a message that is already built, i.e. the check has either already been done or was intentionally skipped.
Suggestion
Use buildPartial() instead of build(). This way the required field check is skipped.
If the general idea of this proposal is approved, I will be happy to implement it and submit a pull request.
More context
I am actually using Proto 3. I’m modifying the generated code with custom validation logic. It would sound reasonable that build() runs the validation, while buildPartial() skips it.
However, as I’ve mentioned, even without any customization, it’s fairly sensible to use buildPartial() when rebuilding a partial message.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)

 Top Related Medium Post
Top Related Medium Post Top Related StackOverflow Question
Top Related StackOverflow Question
Great, it works now! Thank you.
https://github.com/google/truth/releases/tag/release_1_1_3