Skip to content

Commit

Permalink
Add test cases for Protobuf Editions (#225)
Browse files Browse the repository at this point in the history
Whew! This one is a doozy.

The vast majority of the work is fairly mechanical. In order to test the
gauntlet of new semantics that might occur as a result of editions, a
new set of test cases are created that mostly mirror the existing tests
that already differ between proto2 and proto3. These test cases cover
the existing tests that cover edge cases, with the possible set of field
presence variants (implicit, explicit, and legacy-required; note that
not all of these are valid in all cases) and with both default and
expanded repeated field representation.

Most of these changes are transparent or invisible even at the
`protoreflect` level and mostly just ensure that protovalidate
implementations are not tripping up over the wire format or resulting
descriptors.

One wrinkle is that getting a new enough version of the protobuf
toolchain in Bazel for protobuf editions support is tricky; with
upstream rules_proto, the latest toolchain you get does not support
editions at all. I ran into a couple of very tricky issues getting a
newer toolchain to work, but thankfully the resulting changes to work
around those issues are relatively simple:

- It seems like the `rules_python` repo needs to be defined for protobuf
v27+, but the `protobuf_deps` function does not define it. We must
define it ourselves.

- For unknown reasons, it is necessary to specify both
`--proto_compiler` and `--proto_toolchain_for_cc` (and presumably other
`--proto_toolchain_for_*` options). The values specified were actually
supposedly the default values for these options, so it is unclear why
they need to be specified. I suspect this has something to do with
refactoring of Bazel's protobuf rules but didn't want to spend more time
investigating, as it took a fair bit of time to figure out that it
worked in the first place.
  • Loading branch information
jchadwick-buf authored Jul 16, 2024
1 parent 4f56342 commit bb1d618
Show file tree
Hide file tree
Showing 44 changed files with 9,829 additions and 649 deletions.
4 changes: 3 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
build --experimental_proto_descriptor_sets_include_source_info
build --experimental_proto_descriptor_sets_include_source_info
build --proto_compiler=@com_google_protobuf//:protoc
build --proto_toolchain_for_cc=@com_google_protobuf//:cc_toolchain
23 changes: 22 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ workspace(name = "com_github_bufbuild_protovalidate")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# This is needed due to an unresolved issue with protobuf v27+.
# https://github.com/protocolbuffers/protobuf/issues/17200
http_archive(
name = "rules_python",
sha256 = "0a8003b044294d7840ac7d9d73eef05d6ceb682d7516781a4ec62eeb34702578",
strip_prefix = "rules_python-0.24.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.24.0/rules_python-0.24.0.tar.gz",
)

# Use a newer protobuf toolchain for editions support.
http_archive(
name = "com_google_protobuf",
sha256 = "e4ff2aeb767da6f4f52485c2e72468960ddfe5262483879ef6ad552e52757a77",
strip_prefix = "protobuf-27.2",
urls = ["https://github.com/protocolbuffers/protobuf/archive/v27.2.tar.gz"],
)

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

http_archive(
name = "rules_buf",
sha256 = "523a4e06f0746661e092d083757263a249fedca535bd6dd819a8c50de074731a",
Expand Down Expand Up @@ -54,4 +75,4 @@ gazelle_dependencies()

load("@rules_buf//gazelle/buf:repositories.bzl", "gazelle_buf_dependencies")

gazelle_buf_dependencies()
gazelle_buf_dependencies()
2 changes: 1 addition & 1 deletion buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ managed:
except:
- buf.build/envoyproxy/protoc-gen-validate
plugins:
- plugin: buf.build/protocolbuffers/go:v1.31.0
- plugin: buf.build/protocolbuffers/go:v1.34.2
out: tools/internal/gen
opt: paths=source_relative
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ proto_library(
"filename-with-dash.proto",
"ignore_empty_proto2.proto",
"ignore_empty_proto3.proto",
"ignore_empty_proto_editions.proto",
"ignore_proto2.proto",
"ignore_proto3.proto",
"ignore_proto_editions.proto",
"kitchen_sink.proto",
"maps.proto",
"messages.proto",
Expand All @@ -34,6 +36,7 @@ proto_library(
"repeated.proto",
"required_field_proto2.proto",
"required_field_proto3.proto",
"required_field_proto_editions.proto",
"strings.proto",
"wkt_any.proto",
"wkt_duration.proto",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2023 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.

edition = "2023";

package buf.validate.conformance.cases;

import "buf/validate/validate.proto";

message IgnoreEmptyEditionsScalarExplicitPresence {
int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}

message IgnoreEmptyEditionsScalarExplicitPresenceWithDefault {
int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0,
default = 42
];
}

message IgnoreEmptyEditionsScalarImplicitPresence {
int32 val = 1 [
features.field_presence = IMPLICIT,
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}

message IgnoreEmptyEditionsScalarLegacyRequired {
int32 val = 1 [
features.field_presence = LEGACY_REQUIRED,
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}

message IgnoreEmptyEditionsScalarLegacyRequiredWithDefault {
int32 val = 1 [
features.field_presence = LEGACY_REQUIRED,
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0,
default = 42
];
}

message IgnoreEmptyEditionsMessageExplicitPresence {
Msg val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).cel = {
id: "ignore_empty.editions.message"
message: "foobar"
expression: "this.val == 'foo'"
}
];
message Msg {
string val = 1;
}
}

message IgnoreEmptyEditionsMessageExplicitPresenceDelimited {
Msg val = 1 [
features.message_encoding = DELIMITED,
(buf.validate.field).ignore_empty = true,
(buf.validate.field).cel = {
id: "ignore_empty.editions.message"
message: "foobar"
expression: "this.val == 'foo'"
}
];
message Msg {
string val = 1;
}
}

message IgnoreEmptyEditionsMessageLegacyRequired {
Msg val = 1 [
features.field_presence = LEGACY_REQUIRED,
(buf.validate.field).ignore_empty = true,
(buf.validate.field).cel = {
id: "ignore_empty.editions.message"
message: "foobar"
expression: "this.val == 'foo'"
}
];
message Msg {
string val = 1;
}
}

message IgnoreEmptyEditionsMessageLegacyRequiredDelimited {
Msg val = 1 [
features.message_encoding = DELIMITED,
features.field_presence = LEGACY_REQUIRED,
(buf.validate.field).ignore_empty = true,
(buf.validate.field).cel = {
id: "ignore_empty.editions.message"
message: "foobar"
expression: "this.val == 'foo'"
}
];
message Msg {
string val = 1;
}
}

message IgnoreEmptyEditionsOneof {
oneof o {
int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}
}

message IgnoreEmptyEditionsRepeated {
repeated int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).repeated.min_items = 3
];
}

message IgnoreEmptyEditionsRepeatedExpanded {
repeated int32 val = 1 [
features.repeated_field_encoding = EXPANDED,
(buf.validate.field).ignore_empty = true,
(buf.validate.field).repeated.min_items = 3
];
}

message IgnoreEmptyEditionsMap {
map<int32, int32> val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).map.min_pairs = 3
];
}
Loading

0 comments on commit bb1d618

Please sign in to comment.