Skip to content

Commit

Permalink
Remove explicit syntax check on open enum zero value validation.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 540359843
  • Loading branch information
mkruskal-google authored and copybara-github committed Jun 14, 2023
1 parent d1d7502 commit aba0341
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,7 @@ TEST_F(ParserValidationErrorTest, Proto3EnumError) {
ExpectHasValidationErrors(
"syntax = 'proto3';\n"
"enum Foo {A = 1;}\n",
"1:14: The first enum value must be zero in proto3.\n");
"1:14: The first enum value must be zero for open enums.\n");
}

TEST_F(ParserValidationErrorTest, EnumValueNameError) {
Expand Down
24 changes: 7 additions & 17 deletions src/google/protobuf/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4285,8 +4285,6 @@ class DescriptorBuilder {
const DescriptorProto& proto);
void ValidateProto3Field(const FieldDescriptor* field,
const FieldDescriptorProto& proto);
void ValidateProto3Enum(const EnumDescriptor* enm,
const EnumDescriptorProto& proto);

// Returns true if the map entry message is compatible with the
// auto-generated entry message from map fields syntax.
Expand Down Expand Up @@ -7199,19 +7197,13 @@ void DescriptorBuilder::ValidateProto3(const FileDescriptor* file,
for (int i = 0; i < file->message_type_count(); ++i) {
ValidateProto3Message(file->message_types_ + i, proto.message_type(i));
}
for (int i = 0; i < file->enum_type_count(); ++i) {
ValidateProto3Enum(file->enum_types_ + i, proto.enum_type(i));
}
}

void DescriptorBuilder::ValidateProto3Message(const Descriptor* message,
const DescriptorProto& proto) {
for (int i = 0; i < message->nested_type_count(); ++i) {
ValidateProto3Message(message->nested_types_ + i, proto.nested_type(i));
}
for (int i = 0; i < message->enum_type_count(); ++i) {
ValidateProto3Enum(message->enum_types_ + i, proto.enum_type(i));
}
for (int i = 0; i < message->field_count(); ++i) {
ValidateProto3Field(message->fields_ + i, proto.field(i));
}
Expand Down Expand Up @@ -7269,15 +7261,6 @@ void DescriptorBuilder::ValidateProto3Field(const FieldDescriptor* field,
}
}

void DescriptorBuilder::ValidateProto3Enum(const EnumDescriptor* enm,
const EnumDescriptorProto& proto) {
if (enm->value_count() > 0 && enm->value(0)->number() != 0) {
AddError(enm->full_name(), proto.value(0),
DescriptorPool::ErrorCollector::NUMBER,
"The first enum value must be zero in proto3.");
}
}

void DescriptorBuilder::ValidateOptions(const Descriptor* message,
const DescriptorProto& proto) {
CheckFieldJsonNameUniqueness(proto, message);
Expand Down Expand Up @@ -7385,6 +7368,13 @@ void DescriptorBuilder::ValidateOptions(const EnumDescriptor* enm,
const EnumDescriptorProto& proto) {
CheckEnumValueUniqueness(proto, enm);

if (!enm->is_closed() && enm->value_count() > 0 &&
enm->value(0)->number() != 0) {
AddError(enm->full_name(), proto.value(0),
DescriptorPool::ErrorCollector::NUMBER,
"The first enum value must be zero for open enums.");
}

if (!enm->options().has_allow_alias() || !enm->options().allow_alias()) {
absl::flat_hash_map<int, std::string> used_values;
for (int i = 0; i < enm->value_count(); ++i) {
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/descriptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6880,7 +6880,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Enum) {
" value { name: 'FOO_FOO' number:1 } "
"}",
"foo.proto: FooEnum: NUMBER: The first enum value must be "
"zero in proto3.\n");
"zero for open enums.\n");

BuildFileWithErrors(
"name: 'foo.proto' "
Expand All @@ -6893,7 +6893,7 @@ TEST_F(ValidationErrorTest, ValidateProto3Enum) {
" } "
"}",
"foo.proto: Foo.FooEnum: NUMBER: The first enum value must be "
"zero in proto3.\n");
"zero for open enums.\n");

// valid case.
BuildFile(
Expand Down

0 comments on commit aba0341

Please sign in to comment.