I am new to Protocol Buffers and currently I see the following problem:
We use proto2
syntax with Protocol Buffers 3.2.0 library and observe that required
fields are not enforced during the serialization.
Here a basic example with Catch C++ tests:
syntax = "proto2";
package simple_test;
message SimpleMessage
{
required string field1 = 1;
required string field2 = 2;
}
#include "simple.pb.h"
#include <catch.hpp>
#include <string>
SCENARIO("Verify that serialization with missing required fields fails", "[protobuf]")
{
GIVEN("a message")
{
simple_test::SimpleMessage m;
WHEN("field1 is set and field2 is not and both are required")
{
m.set_field1("aaa");
THEN("serializing this message to string buffer must fail")
{
std::string buffer;
CHECK_FALSE(m.SerializeToString(&buffer));
REQUIRE(buffer.empty());
}
}
}
}
m.SerializeToString(&buffer)
returns true
and buffer.empty()
is false
.
required
fields were removed in Protobuf v3.
What happens with the use case:
proto2
Message compiled with Protobuf v3 compiler. This message ends up with partially filled required fields and is going to be send to Protobuf v2 enpoint which enforces the required
fields. Does this effectively mean that there were just bytes sent for nothing, because the message is invalid and will be rejected?
Should I downgrade from v3.2.0 to 2.x to disallow sending of incomplete messages at client side?
I came along this documentation which I've overseen before:
Standard Message Methods and there is the documentation for:
Standard Message Methods
Each message class also contains a number of other methods that let you check or manipulate the entire message, including:
bool IsInitialized() const;
: checks if all the required fields have been set.
This is what I needed! Protobuf are great again!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With