Closed maros closed 11 months ago
TL;DR it's on purpose; you can change this behaviour using the https://metacpan.org/pod/Google::ProtocolBuffers::Dynamic#encode_defaults option
The treatment of scalars and default values in protocol buffers changed slightly between proto2 and proto3; for example proto3 explicitly states that there is no way to know whether a field has been explicitly set to the default value, while for proto2 there are hints that this might be the case after deserialization, but it is not stated explicitly.
When implementing the bindings, I chose to default to the proto3 treatment of default values for proto2 as well (it's one of the possible interpretations anyway), with the option of enabling the behavior you want with the option above.
From the proto2 language definition:
Changing a default value is generally OK, as long as you remember that default values are never sent over the wire.
From the proto3 language defintion:
If a value is missing in the JSON-encoded data or if its value is null, it will be interpreted as
the appropriate default value when parsed into a protocol buffer. If a field has the default
value in the protocol buffer, it will be omitted in the JSON-encoded data by default to save
space. An implementation may provide options to emit fields with default values in the
JSON-encoded output.
Hope this helps, Mattia
Thank you for your prompt reply. I'm aware of the changed semantic between proto2 and the proto3 implementation, However I still believe that this report is justified since
I updated the attached test case to highlight the difference.
Sorry for the delay in getting back.
The issue is that map() silently ignores unknown parameters; your test passes if you change the call to map to
$dynamic->map({
package => 'test',
prefix => 'Test',
options => {
explicit_defaults => 0,
encode_defaults => 1,
},
});
I pushed https://github.com/mbarbon/google-protobuf-dynamic/commit/52481131ee46a85caf0c0a3bf2ad8fe5d6719d88 so it will throw an error on unknown parameters.
Version 0.17 should appear soon on CPAN.
Encoding/decoding roundtrip via protobuf or JSON does produce a different result when the set value is 0. See the the following code for a failing testcase
Tested with version 0.16 and perl 5.22.3