Open TheLostTree opened 12 months ago
Can confirm. toBinary
and toJson
doesn't output default values for required
fields when we use proto2
.
Is this a bug or intended behavior? This should be solvable somehow. One approach is to made an option for those two functions to serialize required
values even in the default state.
There is only partial support for proto2
. The required
label is not supported. At least for toJson
there is an option to emitDefaultValues
, but there is no such option for toBinary
.
The info on partial support for proto2 is a bit outdated. The library is able to serialize and deserialize such fields without issues, there's no any bool
fallback.
From my experiments with other TS libraries this one is a clear winner, but the inability to configure default value writes to support proto2
clients is a deal breaker.
There was actually a very similar request to support this case some time ago: https://github.com/timostamm/protobuf-ts/issues/194
I can think of few approaches to make this work:
BinaryWriteOptions
. This is pretty invasive, adds unnecessary runtime bloat that's not needed for most scenarios, but is clear and understandable.protoc
plugin with a flag like always_emit_default_values
. This is a little bit hacky. The idea here is to keep TS interfaces as is (do not add ?
to fields), but:
!== undefined
check instead of !== 0
and != ""
;opt: true
for reflection serializers to do the same.The protoc
flag looks like a solid solution to support this edge case for users who are forced to support proto2
in their projects, and at the same time don't intervene in the usual proto3
use cases of this library. Thoughts?
Protobuf-ts doesn't serialize the resultCode field even though it is set. Would it be possible for protobuf-ts to emit code that handels the required in proto2 by removing the check for resultCode == 0?