mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-02-11 15:40:44 +00:00
This fixes a bug introduced by CL/182360. Overview of the problem: * CL/182360 removes the internal/prototype package, such that protodesc was re-implemented using internal/filedesc. * As a result of that change, resolving internal dependencies became the responsibility of protodesc. * Dependency resolution used the following two-pass algorithm: 1) first pass derives the full name of all declarations 2) second pass fully initializes each descriptor declaration, now being able to resolve local dependencies from the previous step. * When the second pass looks up a local dependency, it is guaranteed to find it, but it is not guaranteed that the dependency has been initialized (since it may appear later on). This is problematic for default enum values since it implies that the enum dependency may not be sufficiently initialized to be able to query its set of values, leading to panics. * CL/182360 recognized the problem and attempted to enforce an initialization ordering where nested enums were always initialized before the body of the message declaration itself. * However, that ordering fails to enforce that that enum declarations outside the parent tree are initialized beforehand. For example, referring to an enum value that is declared within a sibling of the parent message. * This CL fixes the problem with a three-pass algorithm: 1) first pass derives the full name *and* fully initialize the entire descriptor *except* for dependency references (i.e., type_name). 2) second pass only resolves dependency references, where we do not need to worry about initialization ordering. 3) third pass validates the descriptors are well-formed. This can now depend on all information being fully initialized. * While a lot of code moves, this change is actually very mechanical. Other than split things apart, no new logic is introduced nor removed. Change-Id: Ia91d4aade8f6187c19d704d43ae96b3b9d276792 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/184297 Reviewed-by: Damien Neil <dneil@google.com>
646 lines
22 KiB
Go
646 lines
22 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package protodesc
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
|
"google.golang.org/protobuf/internal/scalar"
|
|
"google.golang.org/protobuf/reflect/protoregistry"
|
|
|
|
"google.golang.org/protobuf/types/descriptorpb"
|
|
)
|
|
|
|
func mustParseFile(s string) *descriptorpb.FileDescriptorProto {
|
|
pb := new(descriptorpb.FileDescriptorProto)
|
|
if err := prototext.Unmarshal([]byte(s), pb); err != nil {
|
|
panic(err)
|
|
}
|
|
return pb
|
|
}
|
|
|
|
// Tests validation logic for malformed descriptors.
|
|
func TestNewFile_ValidationErrors(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
deps []*descriptorpb.FileDescriptorProto
|
|
fd *descriptorpb.FileDescriptorProto
|
|
wantErr string
|
|
}{{
|
|
name: "field number reserved",
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("field-number-reserved.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("BadMessage"),
|
|
ReservedRange: []*descriptorpb.DescriptorProto_ReservedRange{{
|
|
Start: scalar.Int32(3),
|
|
End: scalar.Int32(4),
|
|
}},
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("good_field"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
|
|
}, {
|
|
Name: scalar.String("bad_field"),
|
|
Number: scalar.Int32(3),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
|
|
}},
|
|
}},
|
|
},
|
|
wantErr: "reserved number 3",
|
|
}, {
|
|
name: "field name reserved",
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("field-name-reserved.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("BadMessage"),
|
|
ReservedName: []string{"bad_field", "baz"},
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("good_field"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
|
|
}, {
|
|
Name: scalar.String("bad_field"),
|
|
Number: scalar.Int32(3),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
|
|
}},
|
|
}},
|
|
},
|
|
wantErr: `reserved name "bad_field"`,
|
|
}, {
|
|
name: "normal field with extendee",
|
|
deps: []*descriptorpb.FileDescriptorProto{{
|
|
Name: scalar.String("extensible.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("ExtensibleMessage"),
|
|
ExtensionRange: []*descriptorpb.DescriptorProto_ExtensionRange{{
|
|
Start: scalar.Int32(1000),
|
|
End: scalar.Int32(2000),
|
|
}},
|
|
}},
|
|
}},
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("field-with-extendee.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
Dependency: []string{"extensible.proto"},
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("BadMessage"),
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("good_field"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
|
|
}, {
|
|
Name: scalar.String("bad_field"),
|
|
Number: scalar.Int32(3),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
|
|
Extendee: scalar.String(".foo.ExtensibleMessage"),
|
|
}},
|
|
}},
|
|
},
|
|
wantErr: "may not have extendee",
|
|
}, {
|
|
name: "type_name on int32 field",
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("int32-with-type-name.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
EnumType: []*descriptorpb.EnumDescriptorProto{{
|
|
Name: scalar.String("AnEnum"),
|
|
Value: []*descriptorpb.EnumValueDescriptorProto{{
|
|
Name: scalar.String("UNKNOWN"),
|
|
Number: scalar.Int32(0),
|
|
}},
|
|
}},
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("BadMessage"),
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("good_field"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
|
|
}, {
|
|
Name: scalar.String("bad_field"),
|
|
Number: scalar.Int32(3),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
|
|
TypeName: scalar.String("AnEnum"),
|
|
}},
|
|
}},
|
|
},
|
|
wantErr: "type_name",
|
|
}, {
|
|
name: "type_name on string extension",
|
|
deps: []*descriptorpb.FileDescriptorProto{{
|
|
Name: scalar.String("extensible.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("ExtensibleMessage"),
|
|
ExtensionRange: []*descriptorpb.DescriptorProto_ExtensionRange{{
|
|
Start: scalar.Int32(1000),
|
|
End: scalar.Int32(2000),
|
|
}},
|
|
}},
|
|
}},
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("string-ext-with-type-name.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("bar"),
|
|
Dependency: []string{"extensible.proto"},
|
|
EnumType: []*descriptorpb.EnumDescriptorProto{{
|
|
Name: scalar.String("AnEnum"),
|
|
Value: []*descriptorpb.EnumValueDescriptorProto{{
|
|
Name: scalar.String("UNKNOWN"),
|
|
Number: scalar.Int32(0),
|
|
}},
|
|
}},
|
|
Extension: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("my_ext"),
|
|
Number: scalar.Int32(1000),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
|
|
Extendee: scalar.String(".foo.ExtensibleMessage"),
|
|
TypeName: scalar.String("AnEnum"),
|
|
}},
|
|
},
|
|
wantErr: "type_name",
|
|
}, {
|
|
name: "oneof_index on extension",
|
|
deps: []*descriptorpb.FileDescriptorProto{{
|
|
Name: scalar.String("extensible.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("ExtensibleMessage"),
|
|
ExtensionRange: []*descriptorpb.DescriptorProto_ExtensionRange{{
|
|
Start: scalar.Int32(1000),
|
|
End: scalar.Int32(2000),
|
|
}},
|
|
}},
|
|
}},
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("ext-with-oneof-index.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("bar"),
|
|
Dependency: []string{"extensible.proto"},
|
|
Extension: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("my_ext"),
|
|
Number: scalar.Int32(1000),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
|
|
Extendee: scalar.String(".foo.ExtensibleMessage"),
|
|
OneofIndex: scalar.Int32(0),
|
|
}},
|
|
},
|
|
wantErr: "oneof_index",
|
|
}, {
|
|
name: "enum with reserved number",
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("enum-with-reserved-number.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
EnumType: []*descriptorpb.EnumDescriptorProto{{
|
|
Name: scalar.String("AnEnum"),
|
|
ReservedRange: []*descriptorpb.EnumDescriptorProto_EnumReservedRange{{
|
|
Start: scalar.Int32(5),
|
|
End: scalar.Int32(6),
|
|
}, {
|
|
Start: scalar.Int32(10),
|
|
End: scalar.Int32(12),
|
|
}},
|
|
Value: []*descriptorpb.EnumValueDescriptorProto{{
|
|
Name: scalar.String("UNKNOWN"),
|
|
Number: scalar.Int32(0),
|
|
}, {
|
|
Name: scalar.String("FOO"),
|
|
Number: scalar.Int32(1),
|
|
}, {
|
|
Name: scalar.String("BAR"),
|
|
Number: scalar.Int32(2),
|
|
}, {
|
|
Name: scalar.String("BAD"),
|
|
Number: scalar.Int32(11),
|
|
}},
|
|
}},
|
|
},
|
|
wantErr: "reserved number 11",
|
|
}, {
|
|
name: "enum with reserved number",
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("enum-with-reserved-name.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("ParentMessage"),
|
|
EnumType: []*descriptorpb.EnumDescriptorProto{{
|
|
Name: scalar.String("AnEnum"),
|
|
ReservedName: []string{"ABC", "XYZ"},
|
|
Value: []*descriptorpb.EnumValueDescriptorProto{{
|
|
Name: scalar.String("UNKNOWN"),
|
|
Number: scalar.Int32(0),
|
|
}, {
|
|
Name: scalar.String("FOO"),
|
|
Number: scalar.Int32(1),
|
|
}, {
|
|
Name: scalar.String("BAR"),
|
|
Number: scalar.Int32(2),
|
|
}, {
|
|
Name: scalar.String("XYZ"),
|
|
Number: scalar.Int32(3),
|
|
}},
|
|
}},
|
|
}},
|
|
},
|
|
wantErr: `reserved name "XYZ"`,
|
|
}, {
|
|
name: "message dependency without import",
|
|
deps: []*descriptorpb.FileDescriptorProto{{
|
|
Name: scalar.String("foo.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("Foo"),
|
|
}},
|
|
}},
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("message-dependency-without-import.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("bar"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("Bar"),
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("id"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
|
|
}, {
|
|
Name: scalar.String("foo"),
|
|
Number: scalar.Int32(2),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
|
|
TypeName: scalar.String(".foo.Foo"),
|
|
}},
|
|
}},
|
|
},
|
|
wantErr: "foo.Foo without import of foo.proto",
|
|
}, {
|
|
name: "enum dependency without import",
|
|
deps: []*descriptorpb.FileDescriptorProto{{
|
|
Name: scalar.String("foo.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
EnumType: []*descriptorpb.EnumDescriptorProto{{
|
|
Name: scalar.String("Foo"),
|
|
Value: []*descriptorpb.EnumValueDescriptorProto{{
|
|
Name: scalar.String("UNKNOWN"),
|
|
Number: scalar.Int32(0),
|
|
}},
|
|
}},
|
|
}},
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("enum-dependency-without-import.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("bar"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("Bar"),
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("id"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
|
|
}, {
|
|
Name: scalar.String("foo"),
|
|
Number: scalar.Int32(2),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_ENUM.Enum(),
|
|
TypeName: scalar.String(".foo.Foo"),
|
|
}},
|
|
}},
|
|
},
|
|
wantErr: "foo.Foo without import of foo.proto",
|
|
}, {
|
|
name: "message dependency on without import on file imported by a public import",
|
|
deps: []*descriptorpb.FileDescriptorProto{{
|
|
Name: scalar.String("foo.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("Foo"),
|
|
}},
|
|
}, {
|
|
Name: scalar.String("baz.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
Dependency: []string{"foo.proto"},
|
|
}, {
|
|
Name: scalar.String("old-baz.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
Dependency: []string{"baz.proto"},
|
|
PublicDependency: []int32{0},
|
|
}},
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("message-dependency-without-import.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("bar"),
|
|
Dependency: []string{"old-baz.proto"},
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("Bar"),
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("id"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
|
|
}, {
|
|
Name: scalar.String("foo"),
|
|
Number: scalar.Int32(2),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
|
|
TypeName: scalar.String(".foo.Foo"),
|
|
}},
|
|
}},
|
|
},
|
|
wantErr: "foo.Foo without import of foo.proto",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
r := new(protoregistry.Files)
|
|
for _, dep := range tc.deps {
|
|
f, err := NewFile(dep, r)
|
|
if err != nil {
|
|
t.Fatalf("Error creating dependency: %v", err)
|
|
}
|
|
if err := r.Register(f); err != nil {
|
|
t.Fatalf("Error adding dependency: %v", err)
|
|
}
|
|
}
|
|
if _, err := NewFile(tc.fd, r); err == nil || !strings.Contains(err.Error(), tc.wantErr) {
|
|
t.Errorf("NewFile: got err = %v; want error containing %q", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Sanity checks for well-formed descriptors. Most behavior with well-formed descriptors is covered
|
|
// by other tests that rely on generated descriptors.
|
|
func TestNewFile_ValidationOK(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
deps []*descriptorpb.FileDescriptorProto
|
|
fd *descriptorpb.FileDescriptorProto
|
|
}{{
|
|
name: "self contained file",
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("self-contained.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
EnumType: []*descriptorpb.EnumDescriptorProto{{
|
|
Name: scalar.String("TopLevelEnum"),
|
|
Value: []*descriptorpb.EnumValueDescriptorProto{{
|
|
Name: scalar.String("UNKNOWN"),
|
|
Number: scalar.Int32(0),
|
|
}},
|
|
}},
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("TopLevelMessage"),
|
|
EnumType: []*descriptorpb.EnumDescriptorProto{{
|
|
Name: scalar.String("NestedEnum"),
|
|
Value: []*descriptorpb.EnumValueDescriptorProto{{
|
|
Name: scalar.String("UNKNOWN"),
|
|
Number: scalar.Int32(0),
|
|
}},
|
|
}},
|
|
NestedType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("NestedMessage"),
|
|
}},
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("id"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
|
|
}, {
|
|
Name: scalar.String("top_level_enum"),
|
|
Number: scalar.Int32(2),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_ENUM.Enum(),
|
|
TypeName: scalar.String(".foo.TopLevelEnum"),
|
|
}, {
|
|
Name: scalar.String("nested_enum"),
|
|
Number: scalar.Int32(3),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_ENUM.Enum(),
|
|
TypeName: scalar.String(".foo.TopLevelMessage.NestedEnum"),
|
|
}, {
|
|
Name: scalar.String("nested_message"),
|
|
Number: scalar.Int32(4),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
|
|
TypeName: scalar.String(".foo.TopLevelMessage.NestedMessage"),
|
|
}},
|
|
}},
|
|
},
|
|
}, {
|
|
name: "external types with explicit import",
|
|
deps: []*descriptorpb.FileDescriptorProto{{
|
|
Name: scalar.String("foo.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("FooMessage"),
|
|
}},
|
|
EnumType: []*descriptorpb.EnumDescriptorProto{{
|
|
Name: scalar.String("BarEnum"),
|
|
Value: []*descriptorpb.EnumValueDescriptorProto{{
|
|
Name: scalar.String("UNKNOWN"),
|
|
Number: scalar.Int32(0),
|
|
}},
|
|
}},
|
|
}},
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("external-types-with-explicit-import.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("bar"),
|
|
Dependency: []string{"foo.proto"},
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("Bar"),
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("id"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
|
|
}, {
|
|
Name: scalar.String("foo"),
|
|
Number: scalar.Int32(2),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
|
|
TypeName: scalar.String(".foo.FooMessage"),
|
|
}, {
|
|
Name: scalar.String("bar"),
|
|
Number: scalar.Int32(3),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_ENUM.Enum(),
|
|
TypeName: scalar.String(".foo.BarEnum"),
|
|
}},
|
|
}},
|
|
},
|
|
}, {
|
|
name: "external types with transitive public imports",
|
|
deps: []*descriptorpb.FileDescriptorProto{{
|
|
Name: scalar.String("quux.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("QuuxMessage"),
|
|
}},
|
|
}, {
|
|
Name: scalar.String("foo.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
Dependency: []string{"quux.proto"},
|
|
PublicDependency: []int32{0},
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("FooMessage"),
|
|
}},
|
|
EnumType: []*descriptorpb.EnumDescriptorProto{{
|
|
Name: scalar.String("BarEnum"),
|
|
Value: []*descriptorpb.EnumValueDescriptorProto{{
|
|
Name: scalar.String("UNKNOWN"),
|
|
Number: scalar.Int32(0),
|
|
}},
|
|
}},
|
|
}, {
|
|
Name: scalar.String("old-name.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
Dependency: []string{"foo.proto"},
|
|
PublicDependency: []int32{0},
|
|
}},
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("external-types-with-public-import.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("bar"),
|
|
Dependency: []string{"old-name.proto"},
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("Bar"),
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("id"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
|
|
}, {
|
|
Name: scalar.String("foo"),
|
|
Number: scalar.Int32(2),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
|
|
TypeName: scalar.String(".foo.FooMessage"),
|
|
}, {
|
|
Name: scalar.String("bar"),
|
|
Number: scalar.Int32(3),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_ENUM.Enum(),
|
|
TypeName: scalar.String(".foo.BarEnum"),
|
|
}, {
|
|
Name: scalar.String("quux"),
|
|
Number: scalar.Int32(4),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
|
|
TypeName: scalar.String(".foo.QuuxMessage"),
|
|
}},
|
|
}},
|
|
},
|
|
}, {
|
|
name: "external type from weak import",
|
|
deps: []*descriptorpb.FileDescriptorProto{{
|
|
Name: scalar.String("weak.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("foo"),
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("WeakMessage"),
|
|
}},
|
|
}},
|
|
fd: &descriptorpb.FileDescriptorProto{
|
|
Name: scalar.String("external-type-from-weak-import.proto"),
|
|
Syntax: scalar.String("proto2"),
|
|
Package: scalar.String("bar"),
|
|
Dependency: []string{"weak.proto"},
|
|
WeakDependency: []int32{0},
|
|
MessageType: []*descriptorpb.DescriptorProto{{
|
|
Name: scalar.String("Bar"),
|
|
Field: []*descriptorpb.FieldDescriptorProto{{
|
|
Name: scalar.String("id"),
|
|
Number: scalar.Int32(1),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
|
|
}, {
|
|
Name: scalar.String("weak_message"),
|
|
Number: scalar.Int32(2),
|
|
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
|
|
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
|
|
TypeName: scalar.String(".foo.WeakMessage"),
|
|
Options: &descriptorpb.FieldOptions{
|
|
Weak: scalar.Bool(true),
|
|
},
|
|
}},
|
|
}},
|
|
},
|
|
}, {
|
|
name: "local enum value dependency in sibling to parent message",
|
|
fd: mustParseFile(`
|
|
name: "test.proto"
|
|
message_type: [{
|
|
name: "M1"
|
|
field: [{
|
|
name: "F"
|
|
number: 1
|
|
label: LABEL_OPTIONAL
|
|
type: TYPE_ENUM
|
|
type_name: ".M2.E"
|
|
default_value: "V2"
|
|
}]
|
|
}, {
|
|
name: "M2"
|
|
enum_type: [{
|
|
name: "E"
|
|
value: [{name:"V1" number:1}, {name:"V2" number:2}]
|
|
}]
|
|
}]
|
|
`),
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
r := new(protoregistry.Files)
|
|
for _, dep := range tc.deps {
|
|
f, err := NewFile(dep, r)
|
|
if err != nil {
|
|
t.Fatalf("error creating dependency: %v", err)
|
|
}
|
|
if err := r.Register(f); err != nil {
|
|
t.Fatalf("error adding dependency: %v", err)
|
|
}
|
|
}
|
|
if _, err := NewFile(tc.fd, r); err != nil {
|
|
t.Errorf("unexpected NewFile error: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|