|
|
|
@ -3861,9 +3861,10 @@ class DescriptorBuilder { |
|
|
|
|
|
|
|
|
|
void CheckFieldJsonNameUniqueness(const DescriptorProto& proto, |
|
|
|
|
const Descriptor* result); |
|
|
|
|
void CheckFieldJsonNameUniqueness(std::string message_name, |
|
|
|
|
void CheckFieldJsonNameUniqueness(const std::string& message_name, |
|
|
|
|
const DescriptorProto& message, |
|
|
|
|
bool is_proto2, bool use_custom_names); |
|
|
|
|
FileDescriptor::Syntax syntax, |
|
|
|
|
bool use_custom_names); |
|
|
|
|
void CheckEnumValueUniqueness(const EnumDescriptorProto& proto, |
|
|
|
|
const EnumDescriptor* result); |
|
|
|
|
|
|
|
|
@ -5492,12 +5493,12 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, |
|
|
|
|
|
|
|
|
|
void DescriptorBuilder::CheckFieldJsonNameUniqueness( |
|
|
|
|
const DescriptorProto& proto, const Descriptor* result) { |
|
|
|
|
bool is_proto2 = result->file()->syntax() == FileDescriptor::SYNTAX_PROTO2; |
|
|
|
|
FileDescriptor::Syntax syntax = result->file()->syntax(); |
|
|
|
|
std::string message_name = result->full_name(); |
|
|
|
|
// two passes: one looking only at default JSON names, and one that considers
|
|
|
|
|
// custom JSON names
|
|
|
|
|
CheckFieldJsonNameUniqueness(message_name, proto, is_proto2, false); |
|
|
|
|
CheckFieldJsonNameUniqueness(message_name, proto, is_proto2, true); |
|
|
|
|
CheckFieldJsonNameUniqueness(message_name, proto, syntax, false); |
|
|
|
|
CheckFieldJsonNameUniqueness(message_name, proto, syntax, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
@ -5522,8 +5523,8 @@ struct JsonNameDetails { |
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void DescriptorBuilder::CheckFieldJsonNameUniqueness( |
|
|
|
|
std::string message_name, const DescriptorProto& message, bool is_proto2, |
|
|
|
|
bool use_custom_names) { |
|
|
|
|
const std::string& message_name, const DescriptorProto& message, |
|
|
|
|
FileDescriptor::Syntax syntax, bool use_custom_names) { |
|
|
|
|
|
|
|
|
|
absl::flat_hash_map<std::string, JsonNameDetails> name_to_field; |
|
|
|
|
for (const FieldDescriptorProto& field : message.field()) { |
|
|
|
@ -5531,43 +5532,43 @@ void DescriptorBuilder::CheckFieldJsonNameUniqueness( |
|
|
|
|
std::string name = GetJsonName(field, use_custom_names, &is_custom); |
|
|
|
|
std::string lowercase_name = absl::AsciiStrToLower(name); |
|
|
|
|
auto existing = name_to_field.find(lowercase_name); |
|
|
|
|
if (existing != name_to_field.end()) { |
|
|
|
|
JsonNameDetails& match = existing->second; |
|
|
|
|
if (use_custom_names && !is_custom && !match.is_custom) { |
|
|
|
|
// if this pass is considering custom JSON names, but neither of the
|
|
|
|
|
// names involved in the conflict are custom, don't bother with a
|
|
|
|
|
// message. That will have been reported from other pass (non-custom
|
|
|
|
|
// JSON names).
|
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
absl::string_view this_type = is_custom ? "custom" : "default"; |
|
|
|
|
absl::string_view existing_type = match.is_custom ? "custom" : "default"; |
|
|
|
|
// If the matched name differs (which it can only differ in case), include
|
|
|
|
|
// it in the error message, for maximum clarity to user.
|
|
|
|
|
absl::string_view name_suffix = ""; |
|
|
|
|
if (name != match.orig_name) { |
|
|
|
|
name_suffix = absl::StrCat(" (\"", match.orig_name, "\")"); |
|
|
|
|
} |
|
|
|
|
std::string error_message = |
|
|
|
|
absl::StrFormat("The %s JSON name of field \"%s\" (\"%s\") conflicts " |
|
|
|
|
"with the %s JSON name of field \"%s\"%s.", |
|
|
|
|
this_type, field.name(), name, existing_type, |
|
|
|
|
match.field->name(), name_suffix); |
|
|
|
|
|
|
|
|
|
bool involves_default = !is_custom || !match.is_custom; |
|
|
|
|
if (is_proto2 && involves_default) { |
|
|
|
|
AddWarning(message_name, field, DescriptorPool::ErrorCollector::NAME, |
|
|
|
|
error_message); |
|
|
|
|
} else { |
|
|
|
|
if (involves_default) { |
|
|
|
|
absl::StrAppend(&error_message, " This is not allowed in proto3."); |
|
|
|
|
} |
|
|
|
|
AddError(message_name, field, DescriptorPool::ErrorCollector::NAME, |
|
|
|
|
error_message); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (existing == name_to_field.end()) { |
|
|
|
|
JsonNameDetails details = {&field, name, is_custom}; |
|
|
|
|
name_to_field[lowercase_name] = details; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
JsonNameDetails& match = existing->second; |
|
|
|
|
if (use_custom_names && !is_custom && !match.is_custom) { |
|
|
|
|
// if this pass is considering custom JSON names, but neither of the
|
|
|
|
|
// names involved in the conflict are custom, don't bother with a
|
|
|
|
|
// message. That will have been reported from other pass (non-custom
|
|
|
|
|
// JSON names).
|
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
absl::string_view this_type = is_custom ? "custom" : "default"; |
|
|
|
|
absl::string_view existing_type = match.is_custom ? "custom" : "default"; |
|
|
|
|
// If the matched name differs (which it can only differ in case), include
|
|
|
|
|
// it in the error message, for maximum clarity to user.
|
|
|
|
|
absl::string_view name_suffix = ""; |
|
|
|
|
if (name != match.orig_name) { |
|
|
|
|
name_suffix = absl::StrCat(" (\"", match.orig_name, "\")"); |
|
|
|
|
} |
|
|
|
|
std::string error_message = |
|
|
|
|
absl::StrFormat("The %s JSON name of field \"%s\" (\"%s\") conflicts " |
|
|
|
|
"with the %s JSON name of field \"%s\"%s.", |
|
|
|
|
this_type, field.name(), name, existing_type, |
|
|
|
|
match.field->name(), name_suffix); |
|
|
|
|
|
|
|
|
|
bool involves_default = !is_custom || !match.is_custom; |
|
|
|
|
if (syntax == FileDescriptor::SYNTAX_PROTO2 && involves_default) { |
|
|
|
|
AddWarning(message_name, field, DescriptorPool::ErrorCollector::NAME, |
|
|
|
|
error_message); |
|
|
|
|
} else { |
|
|
|
|
if (involves_default) { |
|
|
|
|
absl::StrAppend(&error_message, " This is not allowed in proto3."); |
|
|
|
|
} |
|
|
|
|
AddError(message_name, field, DescriptorPool::ErrorCollector::NAME, |
|
|
|
|
error_message); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|