Skip to content

Commit

Permalink
reflect/protodesc: remove obsolete JSON name check from desc validator
Browse files Browse the repository at this point in the history
This check was removed from protoc in [1] and the comment in
desc_validate.go mentioned that it was there to emulate the protoc
behavior.

[1] protocolbuffers/protobuf@535069e

fixes golang/protobuf#1616


Change-Id: I8cd6a28a4b2f2b807cdd4432b096cfce8e1f28c8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/585736
Reviewed-by: Nicolas Hillegeer <aktau@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Lasse Folger <lassefolger@google.com>
  • Loading branch information
lfolger authored and gopherbot committed May 16, 2024
1 parent cbc3dd6 commit b3f1c7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
12 changes: 0 additions & 12 deletions reflect/protodesc/desc_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,6 @@ func validateMessageDeclarations(file *filedesc.File, ms []filedesc.Message, mds
if m.ExtensionRanges().Len() > 0 {
return errors.New("message %q using proto3 semantics cannot have extension ranges", m.FullName())
}
// Verify that field names in proto3 do not conflict if lowercased
// with all underscores removed.
// See protoc v3.8.0: src/google/protobuf/descriptor.cc:5830-5847
names := map[string]protoreflect.FieldDescriptor{}
for i := 0; i < m.Fields().Len(); i++ {
f1 := m.Fields().Get(i)
s := strings.Replace(strings.ToLower(string(f1.Name())), "_", "", -1)
if f2, ok := names[s]; ok {
return errors.New("message %q using proto3 semantics has conflict: %q with %q", m.FullName(), f1.Name(), f2.Name())
}
names[s] = f1
}
}

for j, fd := range md.GetField() {
Expand Down
38 changes: 24 additions & 14 deletions reflect/protodesc/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,30 @@ func TestNewFile(t *testing.T) {
name: "test.proto"
package: "fizz"
`),
}, {
label: "proto3 message fields conflict",
inDesc: mustParseFile(`
syntax: "proto3"
name: "test.proto"
message_type: [{name:"M" nested_type:[{
name: "M"
field: [
{name:"_b_a_z_" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
{name:"baz" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}
]
}]}]
`),
wantDesc: mustParseFile(`
syntax: "proto3"
name: "test.proto"
message_type: [{name:"M" nested_type:[{
name: "M"
field: [
{name:"_b_a_z_" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
{name:"baz" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}
]
}]}]
`),
}, {
label: "namespace conflict on enum value",
inDesc: mustParseFile(`
Expand Down Expand Up @@ -817,20 +841,6 @@ func TestNewFile(t *testing.T) {
}]}]
`),
wantErr: `message "M.M" using proto3 semantics cannot have extension ranges`,
}, {
label: "proto3 message fields conflict",
inDesc: mustParseFile(`
syntax: "proto3"
name: "test.proto"
message_type: [{name:"M" nested_type:[{
name: "M"
field: [
{name:"_b_a_z_" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
{name:"baz" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}
]
}]}]
`),
wantErr: `message "M.M" using proto3 semantics has conflict: "baz" with "_b_a_z_"`,
}, {
label: "proto3 message fields",
inDesc: mustParseFile(`
Expand Down

0 comments on commit b3f1c7a

Please sign in to comment.