question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Protobuf: allow unpopulated required fields when `comparingExpectedFieldsOnly`

See original GitHub issue

Current 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dmdashenkovcommented, May 25, 2021

Great, it works now! Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Language Guide | Protocol Buffers - Google Developers
Let's say you want to define a search request message format, ... A second issue with required fields appears when someone adds a...
Read more >
truth/ProtoSubject.java at master · google/truth - GitHub
Truth subject for the full version of Protocol Buffers. *. * <p>{@code ProtoTruth. ... To ignore empty repeated fields as well, use {@link....
Read more >
Why required and optional is removed in Protocol Buffers 3
We dropped required fields in proto3 because required fields are generally considered harmful and violating protobuf's compatibility semantics.
Read more >
ProtoSubject (Truth 0.44)
ProtoFluentAssertion · comparingExpectedFieldsOnly(). Limits the comparison of Protocol buffers to the fields set in the expected proto(s).
Read more >
Message missing required fields exception when parsing a ...
Message missing required fields exception when parsing a message that has required ... to Protocol Buffers ... For example, if you pass protobuf...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found