-
Notifications
You must be signed in to change notification settings - Fork 367
Resolve ignore comments for group fields #4684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Copyright 2020-2026 Buf Technologies, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package bufcheck | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/bufbuild/protocompile" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/protobuf/reflect/protoreflect" | ||
| ) | ||
|
|
||
| const groupsProtoFileContent = `syntax = "proto2"; | ||
|
|
||
| package a; | ||
|
|
||
| message Foo { | ||
| optional group Group = 1 { | ||
| optional string id = 1; | ||
| } | ||
| optional string scalar = 2; | ||
| message Nested { | ||
| optional group NestedGroup = 1 { | ||
| optional string id = 1; | ||
| } | ||
| } | ||
| extend Bar { | ||
| optional group MessageExtensionGroup = 101 { | ||
| optional string id = 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| message Bar { | ||
| extensions 100 to 200; | ||
| } | ||
|
|
||
| extend Bar { | ||
| optional group FileExtensionGroup = 100 { | ||
| optional string id = 1; | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| func TestGroupFieldSyntheticMessage(t *testing.T) { | ||
| t.Parallel() | ||
| fileDescriptor := testCompileFileDescriptor(t, groupsProtoFileContent) | ||
| sourceLocations := fileDescriptor.SourceLocations() | ||
| fooMessageDescriptor := fileDescriptor.Messages().ByName("Foo") | ||
| require.NotNil(t, fooMessageDescriptor) | ||
| nestedMessageDescriptor := fooMessageDescriptor.Messages().ByName("Nested") | ||
| require.NotNil(t, nestedMessageDescriptor) | ||
| for _, testCase := range []struct { | ||
| name string | ||
| fieldDescriptor protoreflect.FieldDescriptor | ||
| expectedSyntheticMessageName protoreflect.FullName | ||
| expectedNoSyntheticMessageFound bool | ||
| }{ | ||
| { | ||
| name: "group field", | ||
| fieldDescriptor: fooMessageDescriptor.Fields().ByName("group"), | ||
| expectedSyntheticMessageName: "a.Foo.Group", | ||
| }, | ||
| { | ||
| name: "group field in nested message", | ||
| fieldDescriptor: nestedMessageDescriptor.Fields().ByName("nestedgroup"), | ||
| expectedSyntheticMessageName: "a.Foo.Nested.NestedGroup", | ||
| }, | ||
| { | ||
| name: "group extension field in message", | ||
| fieldDescriptor: fooMessageDescriptor.Extensions().ByName("messageextensiongroup"), | ||
| expectedSyntheticMessageName: "a.Foo.MessageExtensionGroup", | ||
| }, | ||
| { | ||
| name: "group extension field in file", | ||
| fieldDescriptor: fileDescriptor.Extensions().ByName("fileextensiongroup"), | ||
| expectedSyntheticMessageName: "a.FileExtensionGroup", | ||
| }, | ||
| { | ||
| name: "non-group field", | ||
| fieldDescriptor: fooMessageDescriptor.Fields().ByName("scalar"), | ||
| expectedNoSyntheticMessageFound: true, | ||
| }, | ||
| } { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| require.NotNil(t, testCase.fieldDescriptor) | ||
| sourcePath := sourceLocations.ByDescriptor(testCase.fieldDescriptor).Path | ||
| require.NotEmpty(t, sourcePath) | ||
| syntheticMessageDescriptor := groupFieldSyntheticMessage(fileDescriptor, sourcePath) | ||
| if testCase.expectedNoSyntheticMessageFound { | ||
| require.Nil(t, syntheticMessageDescriptor) | ||
| return | ||
| } | ||
| require.NotNil(t, syntheticMessageDescriptor) | ||
| require.Equal(t, testCase.expectedSyntheticMessageName, syntheticMessageDescriptor.FullName()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Source path traversal itself is covered by protosourcepath, this covers the source paths | ||
| // that resolve to something other than a group field. | ||
| func TestGroupFieldSyntheticMessageNonGroupFieldSourcePaths(t *testing.T) { | ||
| t.Parallel() | ||
| fileDescriptor := testCompileFileDescriptor(t, groupsProtoFileContent) | ||
| for _, sourcePath := range []protoreflect.SourcePath{ | ||
| // The file. | ||
| nil, | ||
| {}, | ||
| // .message_type(0), a message. | ||
| {4, 0}, | ||
| // .message_type(0).nested_type(0), the synthetic message of a group field. | ||
| {4, 0, 3, 0}, | ||
| // .message_type(0).field(0).name, an attribute of a field. | ||
| {4, 0, 2, 0, 1}, | ||
| } { | ||
| require.Nil( | ||
| t, | ||
| groupFieldSyntheticMessage(fileDescriptor, sourcePath), | ||
| "source path %v", sourcePath, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func testCompileFileDescriptor(t *testing.T, fileContent string) protoreflect.FileDescriptor { | ||
| t.Helper() | ||
| compiler := protocompile.Compiler{ | ||
| Resolver: &protocompile.SourceResolver{ | ||
| Accessor: protocompile.SourceAccessorFromMap(map[string]string{"a.proto": fileContent}), | ||
| }, | ||
| SourceInfoMode: protocompile.SourceInfoStandard, | ||
| } | ||
| files, err := compiler.Compile(t.Context(), "a.proto") | ||
| require.NoError(t, err) | ||
| require.Len(t, files, 1) | ||
| return files[0] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
private/bufpkg/bufcheck/testdata/lint/comment_ignores_group/a.proto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| syntax = "proto2"; | ||
|
|
||
| package a; | ||
|
|
||
| message Foo { | ||
| // buf:lint:ignore FIELD_NOT_REQUIRED | ||
| required group Ignored = 1 { | ||
| optional string id = 1; | ||
| } | ||
| required group NotIgnored = 2 { | ||
| optional string id = 1; | ||
| } | ||
| message Nested { | ||
| // buf:lint:ignore FIELD_NOT_REQUIRED | ||
| required group Ignored = 1 { | ||
| optional string id = 1; | ||
| } | ||
| required group NotIgnored = 2 { | ||
| optional string id = 1; | ||
| } | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
private/bufpkg/bufcheck/testdata/lint/comment_ignores_group/buf.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| version: v2 | ||
| lint: | ||
| use: | ||
| - FIELD_NOT_REQUIRED |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.