Skip to content

Commit

Permalink
reflect/protodesc: don't generate invalid syntax values
Browse files Browse the repository at this point in the history
Fixes golang/protobuf#1575

When converting a protoreflect.FileDescriptor
to a FileDescriptorProto, don't emit an invalid syntax.

This can happen for placeholder files, which represent
missing dependencies of a file.

Change-Id: I1d2ae1bd9f89c8a92e751871ce5c02727505b5e2
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/540455
Reviewed-by: Lasse Folger <[email protected]>
Reviewed-by: Michael Stapelberg <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
  • Loading branch information
jhump authored and lfolger committed Nov 8, 2023
1 parent 9492118 commit a8317fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions reflect/protodesc/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"

"google.golang.org/protobuf/internal/filedesc"
"google.golang.org/protobuf/types/descriptorpb"
)

Expand Down Expand Up @@ -1180,3 +1181,12 @@ func TestSourceLocations(t *testing.T) {
t.Errorf("visited %d descriptor, expected 30", numDescs)
}
}

func TestToFileDescriptorProtoPlaceHolder(t *testing.T) {
// Make sure placeholders produce valid protos.
fileDescriptor := ToFileDescriptorProto(filedesc.PlaceholderFile("foo/test.proto"))
_, err := NewFile(fileDescriptor, &protoregistry.Files{} /* empty files since placeholder has no deps */)
if err != nil {
t.Errorf("placeholder file descriptor proto is not valid: %s", err)
}
}
2 changes: 1 addition & 1 deletion reflect/protodesc/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ToFileDescriptorProto(file protoreflect.FileDescriptor) *descriptorpb.FileD
for i, exts := 0, file.Extensions(); i < exts.Len(); i++ {
p.Extension = append(p.Extension, ToFieldDescriptorProto(exts.Get(i)))
}
if syntax := file.Syntax(); syntax != protoreflect.Proto2 {
if syntax := file.Syntax(); syntax != protoreflect.Proto2 && syntax.IsValid() {
p.Syntax = proto.String(file.Syntax().String())
}
return p
Expand Down

0 comments on commit a8317fb

Please sign in to comment.