Skip to content

Commit

Permalink
protoparse: extensions are not allowed to use json_name option (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhump authored Feb 5, 2022
1 parent 38023d3 commit 4ced19e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 10 additions & 0 deletions desc/protoparse/linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,16 @@ func TestLinkerValidation(t *testing.T) {
},
"foo.proto:9:6: message foo.bar.Baz: option (foo.bar.any): could not resolve type reference type.googleapis.com/Foo",
},
{
map[string]string{
"foo.proto": "syntax = \"proto3\";\n" +
"import \"google/protobuf/descriptor.proto\";\n" +
"extend google.protobuf.MessageOptions {\n" +
" string foobar = 10001 [json_name=\"FooBar\"];\n" +
"}\n",
},
"foo.proto:4:26: field foobar: option json_name is not allowed on extensions",
},
}
for i, tc := range testCases {
acc := func(filename string) (io.ReadCloser, error) {
Expand Down
12 changes: 8 additions & 4 deletions desc/protoparse/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ func interpretFileOptions(l *linker, r *parseResult, fd fileDescriptorish) error
}
}
for _, fld := range fd.GetExtensions() {
if err := interpretFieldOptions(l, r, fld); err != nil {
if err := interpretFieldOptions(l, r, fld, true); err != nil {
return err
}
}
Expand Down Expand Up @@ -740,7 +740,7 @@ func interpretMessageOptions(l *linker, r *parseResult, md msgDescriptorish) err
}
}
for _, fld := range md.GetFields() {
if err := interpretFieldOptions(l, r, fld); err != nil {
if err := interpretFieldOptions(l, r, fld, false); err != nil {
return err
}
}
Expand All @@ -755,7 +755,7 @@ func interpretMessageOptions(l *linker, r *parseResult, md msgDescriptorish) err
}
}
for _, fld := range md.GetNestedExtensions() {
if err := interpretFieldOptions(l, r, fld); err != nil {
if err := interpretFieldOptions(l, r, fld, true); err != nil {
return err
}
}
Expand All @@ -782,7 +782,7 @@ func interpretMessageOptions(l *linker, r *parseResult, md msgDescriptorish) err
return nil
}

func interpretFieldOptions(l *linker, r *parseResult, fld fldDescriptorish) error {
func interpretFieldOptions(l *linker, r *parseResult, fld fldDescriptorish, isExtension bool) error {
opts := fld.GetFieldOptions()
if len(opts.GetUninterpretedOption()) > 0 {
uo := opts.UninterpretedOption
Expand All @@ -804,6 +804,10 @@ func interpretFieldOptions(l *linker, r *parseResult, fld fldDescriptorish) erro
if err := r.errs.handleErrorWithPos(optNode.GetValue().Start(), "%s: expecting string value for json_name option", scope); err != nil {
return err
}
} else if isExtension {
if err := r.errs.handleErrorWithPos(optNode.GetName().Start(), "%s: option json_name is not allowed on extensions", scope); err != nil {
return err
}
} else {
fld.AsFieldDescriptorProto().JsonName = proto.String(string(opt.StringValue))
}
Expand Down

0 comments on commit 4ced19e

Please sign in to comment.