From b06ab38f4b16892dc112e729ca9d4ed0f488b0ba Mon Sep 17 00:00:00 2001 From: Joshua Humphries <2035234+jhump@users.noreply.github.com> Date: Fri, 8 Dec 2023 10:45:34 -0500 Subject: [PATCH] Fix formatter issue with message literals with any syntax (#2650) --- CHANGELOG.md | 3 +++ private/buf/bufformat/formatter.go | 4 ++++ .../option/v1/option_message_field.golden.proto | 13 +++++++++++++ .../proto2/option/v1/option_message_field.proto | 8 ++++++++ 4 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 582a45f492..8e43868166 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ `use_enum_numbers`. This affects output serialization. Some examples: - `buf convert --type foo.Bar --from input.binpb --to output.yaml#use_proto_names=true` - `buf convert --type foo.Bar --from input.binpb --to -#format=yaml,use_enum_numbers=true` +- Fix issue where `buf format` would inadvertently mangle files that used + the [expanded `Any` syntax](https://protobuf.com/docs/language-spec#any-messages) + in option values. ## [v1.28.1] - 2023-11-15 diff --git a/private/buf/bufformat/formatter.go b/private/buf/bufformat/formatter.go index b3d9a1d14b..64049cfb3f 100644 --- a/private/buf/bufformat/formatter.go +++ b/private/buf/bufformat/formatter.go @@ -707,6 +707,10 @@ func (f *formatter) writeMessageFieldPrefix(messageFieldNode *ast.MessageFieldNo fieldReferenceNode := messageFieldNode.Name if fieldReferenceNode.Open != nil { f.writeStart(fieldReferenceNode.Open) + if fieldReferenceNode.URLPrefix != nil { + f.writeInline(fieldReferenceNode.URLPrefix) + f.writeInline(fieldReferenceNode.Slash) + } f.writeInline(fieldReferenceNode.Name) } else { f.writeStart(fieldReferenceNode.Name) diff --git a/private/buf/bufformat/testdata/proto2/option/v1/option_message_field.golden.proto b/private/buf/bufformat/testdata/proto2/option/v1/option_message_field.golden.proto index 66817e5355..4a9669591d 100644 --- a/private/buf/bufformat/testdata/proto2/option/v1/option_message_field.golden.proto +++ b/private/buf/bufformat/testdata/proto2/option/v1/option_message_field.golden.proto @@ -2,6 +2,7 @@ syntax = "proto2"; package foo.bar; +import "google/protobuf/any.proto"; import "google/protobuf/descriptor.proto"; message Simple { @@ -26,6 +27,17 @@ message Test { extensions 249, 300 to 350, 500 to 550, 20000 to max [(label) = "jazz"]; + option (any) = { + [type.googleapis.com/foo.bar.Test]: { + foo: "abc" + array: [ + 1, + 2, + 3 + ] + } + }; + message Nested { extend google.protobuf.MessageOptions { optional int32 fooblez = 20003; @@ -70,4 +82,5 @@ message Test { extend google.protobuf.MessageOptions { repeated Test rept = 20002; optional Test.Nested._NestedNested.EEE eee = 20010; + optional google.protobuf.Any any = 20300; } diff --git a/private/buf/bufformat/testdata/proto2/option/v1/option_message_field.proto b/private/buf/bufformat/testdata/proto2/option/v1/option_message_field.proto index 83b2ad491c..4013d5192e 100644 --- a/private/buf/bufformat/testdata/proto2/option/v1/option_message_field.proto +++ b/private/buf/bufformat/testdata/proto2/option/v1/option_message_field.proto @@ -2,6 +2,7 @@ syntax = "proto2"; package foo.bar; +import "google/protobuf/any.proto"; import "google/protobuf/descriptor.proto"; message Simple { @@ -28,6 +29,12 @@ message Test { extensions 249, 300 to 350, 500 to 550, 20000 to max [(label) = "jazz"]; + option (any) = { + [ type . googleapis . com/ foo.bar.Test ]: { + foo: "abc" array: [1,2,3] + } + }; + message Nested { extend google.protobuf.MessageOptions { optional int32 fooblez = 20003; @@ -69,4 +76,5 @@ message Test { extend google.protobuf.MessageOptions { repeated Test rept = 20002; optional Test.Nested._NestedNested.EEE eee = 20010; + optional google.protobuf.Any any = 20300; }