Enable new JSON field name conflict handling.

This will apply uniformly in both proto2 and proto3, taking into account `json_name` options.  See https://github.com/protocolbuffers/protobuf/pull/10750 for more details.

PiperOrigin-RevId: 502972769
pull/11378/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 3aaaf7d7ce
commit f60f478f45
  1. 3
      java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto
  2. 2
      python/google/protobuf/internal/descriptor_test.py
  3. 8
      src/google/protobuf/descriptor.cc
  4. 4
      src/google/protobuf/descriptor_unittest.cc

@ -177,6 +177,9 @@ service TestConflictingMethodNames {
}
message TestConflictingFieldNames {
// TODO(b/261750190) Remove these tests once this behavior is removed.
option deprecated_legacy_json_field_conflicts = true;
enum TestEnum {
UNKNOWN = 0;
FOO = 1;

@ -1186,6 +1186,7 @@ class MakeDescriptorTest(unittest.TestCase):
def testCamelcaseName(self):
descriptor_proto = descriptor_pb2.DescriptorProto()
descriptor_proto.options.deprecated_legacy_json_field_conflicts = True
descriptor_proto.name = 'Bar'
names = ['foo_foo', 'FooBar', 'fooBaz', 'fooFoo', 'foobar']
camelcase_names = ['fooFoo', 'fooBar', 'fooBaz', 'fooFoo', 'foobar']
@ -1200,6 +1201,7 @@ class MakeDescriptorTest(unittest.TestCase):
def testJsonName(self):
descriptor_proto = descriptor_pb2.DescriptorProto()
descriptor_proto.options.deprecated_legacy_json_field_conflicts = True
descriptor_proto.name = 'TestJsonName'
names = ['field_name', 'fieldName', 'FieldName',
'_field_name', 'FIELD_NAME', 'json_name']

@ -5617,18 +5617,10 @@ void DescriptorBuilder::CheckFieldJsonNameUniqueness(
this_type, field.name(), details.orig_name, existing_type,
match.field->name(), name_suffix);
bool involves_default = !details.is_custom || !match.is_custom;
if (syntax == FileDescriptor::SYNTAX_PROTO2 && involves_default) {
// TODO(b/261750676) Upgrade this to an error once downstream proto2 files
// have been fixed.
AddWarning(message_name, field, DescriptorPool::ErrorCollector::NAME,
error_message);
} else {
AddError(message_name, field, DescriptorPool::ErrorCollector::NAME,
error_message);
}
}
}
namespace {
bool IsAllowedReservedField(const FieldDescriptorProto& field) {

@ -1277,6 +1277,8 @@ class StylizedFieldNamesTest : public testing::Test {
AddExtensionRange(AddMessage(&file, "ExtendableMessage"), 1, 1000);
DescriptorProto* message = AddMessage(&file, "TestMessage");
message->mutable_options()->set_deprecated_legacy_json_field_conflicts(
true);
AddField(message, "foo_foo", 1, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message, "FooBar", 2, FieldDescriptorProto::LABEL_OPTIONAL,
@ -7012,7 +7014,7 @@ TEST_F(ValidationErrorTest, ValidateJsonNameConflictProto3) {
}
TEST_F(ValidationErrorTest, ValidateJsonNameConflictProto2) {
BuildFileWithWarnings(
BuildFileWithErrors(
"name: 'foo.proto' "
"syntax: 'proto2' "
"message_type {"

Loading…
Cancel
Save