[Go] Write required string fields into the buffer when using Object API (#8402)

* [Go] Write required string fields into the buffer when using Object API

In C++, CreateX allows to write the default "" value of a required
string, when the string is not explicitly set. Object API Pack method
uses this implementation of CreateX.

However, in go, despite whether the field is optional or required, it is
always checked against empty string allowing to create messages that
fail to pass the verifier. This commits partially reverts #7719 when the
string field is required.

* Add test for serializing required string fields using Object API

* Update generated code

The Monster schema contains a key string field. For historical
convenience reasons, string keys are assumed required.
This commit is contained in:
razvanalex
2025-12-01 16:50:03 +02:00
committed by GitHub
parent e4775aa3fe
commit 31ab0bf6c8
4 changed files with 65 additions and 9 deletions

View File

@@ -76,10 +76,7 @@ func (t *MonsterT) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
if t == nil {
return 0
}
nameOffset := flatbuffers.UOffsetT(0)
if t.Name != "" {
nameOffset = builder.CreateString(t.Name)
}
nameOffset := builder.CreateString(t.Name)
inventoryOffset := flatbuffers.UOffsetT(0)
if t.Inventory != nil {
inventoryOffset = builder.CreateByteString(t.Inventory)