Skip to content
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

Return early if map type mismatch in buf lint protovalidate #2602

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions private/bufpkg/bufcheck/buflint/buflint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ func TestRunProtovalidateRules(t *testing.T) {
bufanalysistesting.NewFileAnnotation(t, "map.proto", 44, 5, 44, 47, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "map.proto", 46, 5, 46, 47, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "map.proto", 50, 5, 50, 57, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "map.proto", 53, 5, 53, 50, "PROTOVALIDATE"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this produce two lint errors l, one for each of the two mismatched annotations?

Copy link
Contributor Author

@oliversun9 oliversun9 Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two lines belong to the same rule, (buf.validate.field).map.

When we pass the path (buf.validate.field).map to OptionExtensionLocation, the location we get back is just the first line. Ideally we get a location back that covers both lines.

I think it's due to the way sourceCodeLocations are generated by the compiler -- there isn't a location that spans both lines exactly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are also not able to tell whether the option is specified in two lines or in the aggregate form.

On a high level, the linter cannot tell

  int32 not_a_map = 14 [
    (buf.validate.field).map.values.int32.lt = 10,
    (buf.validate.field).map.values.int32.gt = 1
  ];

from

  int32 not_a_map = 14 [(buf.validate.field).map.values.int32 = {lt: 10, gt: 1}];

bufanalysistesting.NewFileAnnotation(t, "message.proto", 20, 3, 20, 49, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "message.proto", 27, 5, 27, 51, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "number.proto", 20, 5, 20, 42, "PROTOVALIDATE"),
Expand All @@ -640,6 +641,7 @@ func TestRunProtovalidateRules(t *testing.T) {
bufanalysistesting.NewFileAnnotation(t, "repeated.proto", 45, 5, 45, 48, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "repeated.proto", 49, 28, 49, 71, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "repeated.proto", 51, 38, 51, 92, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "repeated.proto", 53, 26, 53, 74, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "string.proto", 31, 5, 31, 46, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "string.proto", 36, 5, 36, 44, "PROTOVALIDATE"),
bufanalysistesting.NewFileAnnotation(t, "string.proto", 41, 5, 41, 44, "PROTOVALIDATE"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func checkMapRules(
baseAdder.fieldName(),
baseAdder.getFieldRuleName(mapRulesFieldNumber),
)
return nil
}
if mapRules.MinPairs != nil && mapRules.MaxPairs != nil && *mapRules.MinPairs > *mapRules.MaxPairs {
baseAdder.addForPathf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ message MapTest {
(buf.validate.field).map.min_pairs = 1,
(buf.validate.field).map.values.string.pattern = "["
];
int32 not_a_map = 14 [
(buf.validate.field).map.values.int32.lt = 10,
(buf.validate.field).map.values.int32.gt = 1
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ message RepeatedTest {
int32 not_repeated = 15 [(buf.validate.field).repeated.unique = true];
// map value is also not repeated
map<int32, string> map_field = 16 [(buf.validate.field).map.values.repeated.unique = true];
// int64 does not match int32
int32 wrong_type = 17 [(buf.validate.field).repeated.items.int64.lt = 1];
}