From 5db078168446143df95acae73e0522f796584ab4 Mon Sep 17 00:00:00 2001 From: Doria Keung Date: Thu, 19 Mar 2026 16:37:05 -0400 Subject: [PATCH 1/3] Fix default values for bytes fields --- experimental/fdp/generator.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/experimental/fdp/generator.go b/experimental/fdp/generator.go index ce0d0334..927aae7b 100644 --- a/experimental/fdp/generator.go +++ b/experimental/fdp/generator.go @@ -15,6 +15,7 @@ package fdp import ( + "bytes" "math" "slices" "strconv" @@ -577,7 +578,14 @@ func (g *generator) field(f ir.Member, fdp *descriptorpb.FieldDescriptorProto, s fdp.DefaultValue = addr(strconv.FormatFloat(v, 'g', -1, 64)) } } else if v, ok := d.AsString(); ok { - fdp.DefaultValue = addr(v) + // For bytes fields, the default value needs to be escaped. + if fdp.GetType() == descriptorpb.FieldDescriptorProto_TYPE_BYTES { + var buf bytes.Buffer + internal.WriteEscapedBytes(&buf, []byte(v)) + fdp.DefaultValue = addr(buf.String()) + } else { + fdp.DefaultValue = addr(v) + } } g.addSourceLocationWithSourcePathElements( From aa15cd67689c365364221966582b5fac1572a4c1 Mon Sep 17 00:00:00 2001 From: Doria Keung Date: Thu, 19 Mar 2026 16:41:10 -0400 Subject: [PATCH 2/3] Add test case --- experimental/ir/testdata/fields/default.proto | 2 ++ .../ir/testdata/fields/default.proto.fds.yaml | 6 ++++++ .../testdata/fields/default.proto.stderr.txt | 18 +++++++++--------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/experimental/ir/testdata/fields/default.proto b/experimental/ir/testdata/fields/default.proto index 6e935505..a0417391 100644 --- a/experimental/ir/testdata/fields/default.proto +++ b/experimental/ir/testdata/fields/default.proto @@ -43,6 +43,8 @@ message M { bool b2 = 32 [default = 1]; bool b3 = 33 [default = false]; + bytes b = 40 [default = "\0\1\2\3\4\5\6\7fubar!"]; + enum E { E0 = 0; E1 = 1; diff --git a/experimental/ir/testdata/fields/default.proto.fds.yaml b/experimental/ir/testdata/fields/default.proto.fds.yaml index 752b5e3f..f99b7f09 100644 --- a/experimental/ir/testdata/fields/default.proto.fds.yaml +++ b/experimental/ir/testdata/fields/default.proto.fds.yaml @@ -116,6 +116,12 @@ file: type: TYPE_BOOL default_value: "false" json_name: "b3" + - name: "b" + number: 40 + label: LABEL_OPTIONAL + type: TYPE_BYTES + default_value: "\\000\\001\\002\\003\\004\\005\\006\\007fubar!" + json_name: "b" - name: "e1" number: 41 label: LABEL_OPTIONAL diff --git a/experimental/ir/testdata/fields/default.proto.stderr.txt b/experimental/ir/testdata/fields/default.proto.stderr.txt index b8d52149..855ea386 100644 --- a/experimental/ir/testdata/fields/default.proto.stderr.txt +++ b/experimental/ir/testdata/fields/default.proto.stderr.txt @@ -109,18 +109,18 @@ warning: redundant custom default making this option redundant warning: redundant custom default - --> testdata/fields/default.proto:53:26 + --> testdata/fields/default.proto:55:26 | -53 | E e3 = 43 [default = E0]; +55 | E e3 = 43 [default = E0]; | ^^ this is the zero value for `buf.test.M.E` | = help: fields without a custom default will default to the zero value, making this option redundant error: expected singular scalar- or enum-typed field, found scalar type `int32` - --> testdata/fields/default.proto:55:14 + --> testdata/fields/default.proto:57:14 | -55 | repeated int32 w1 = 101 [default = 1, default = 2]; +57 | repeated int32 w1 = 101 [default = 1, default = 2]; | ^^^^^ ------- custom default specified here | = help: custom defaults are only for non-repeated fields that have a @@ -128,9 +128,9 @@ error: expected singular scalar- or enum-typed field, found scalar type `int32` error: expected singular scalar- or enum-typed field, found message type `buf.test.M` - --> testdata/fields/default.proto:56:5 + --> testdata/fields/default.proto:58:5 | -56 | M w2 = 102 [default = { +58 | M w2 = 102 [default = { | ^ ------- custom default specified here | = help: custom defaults are only for non-repeated fields that have a @@ -138,11 +138,11 @@ error: expected singular scalar- or enum-typed field, found message type error: expected singular scalar- or enum-typed field, found message type `buf.test.M` - --> testdata/fields/default.proto:60:5 + --> testdata/fields/default.proto:62:5 | -60 | M w3 = 103 [ +62 | M w3 = 103 [ | ^ -61 | default.i1 = 42, +63 | default.i1 = 42, | ------- custom default specified here | = help: custom defaults are only for non-repeated fields that have a From ffc87aecff45715fe5c2b0d17ccdccc91234423f Mon Sep 17 00:00:00 2001 From: Doria Keung Date: Fri, 20 Mar 2026 17:23:42 -0400 Subject: [PATCH 3/3] Add reference to docs on default value encoding --- experimental/fdp/generator.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/experimental/fdp/generator.go b/experimental/fdp/generator.go index 927aae7b..42630614 100644 --- a/experimental/fdp/generator.go +++ b/experimental/fdp/generator.go @@ -578,8 +578,10 @@ func (g *generator) field(f ir.Member, fdp *descriptorpb.FieldDescriptorProto, s fdp.DefaultValue = addr(strconv.FormatFloat(v, 'g', -1, 64)) } } else if v, ok := d.AsString(); ok { - // For bytes fields, the default value needs to be escaped. if fdp.GetType() == descriptorpb.FieldDescriptorProto_TYPE_BYTES { + // For bytes fields, the default value needs to be escaped. + // Reference for default value encoding: + // https://protobuf.com/docs/descriptors#encoding-default-values var buf bytes.Buffer internal.WriteEscapedBytes(&buf, []byte(v)) fdp.DefaultValue = addr(buf.String())