Provide an escape hatch for the new JSON name conflict handling at the DescriptorPool level.

This is most useful for generated protos where the descriptors can't be easily modified.

PiperOrigin-RevId: 498451864
pull/11426/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent dea15165c6
commit 4950dce199
  1. 15
      src/google/protobuf/descriptor.cc
  2. 8
      src/google/protobuf/descriptor.h

@ -1825,7 +1825,8 @@ DescriptorPool::DescriptorPool()
lazily_build_dependencies_(false),
allow_unknown_(false),
enforce_weak_(false),
disallow_enforce_utf8_(false) {}
disallow_enforce_utf8_(false),
deprecated_legacy_json_field_conflicts_(false) {}
DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database,
ErrorCollector* error_collector)
@ -1838,7 +1839,8 @@ DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database,
lazily_build_dependencies_(false),
allow_unknown_(false),
enforce_weak_(false),
disallow_enforce_utf8_(false) {}
disallow_enforce_utf8_(false),
deprecated_legacy_json_field_conflicts_(false) {}
DescriptorPool::DescriptorPool(const DescriptorPool* underlay)
: mutex_(nullptr),
@ -1850,7 +1852,8 @@ DescriptorPool::DescriptorPool(const DescriptorPool* underlay)
lazily_build_dependencies_(false),
allow_unknown_(false),
enforce_weak_(false),
disallow_enforce_utf8_(false) {}
disallow_enforce_utf8_(false),
deprecated_legacy_json_field_conflicts_(false) {}
DescriptorPool::~DescriptorPool() {
if (mutex_ != nullptr) delete mutex_;
@ -5527,7 +5530,8 @@ void DescriptorBuilder::CheckFieldJsonNameUniqueness(
const DescriptorProto& proto, const Descriptor* result) {
FileDescriptor::Syntax syntax = result->file()->syntax();
std::string message_name = result->full_name();
if (IsLegacyJsonFieldConflictEnabled(result->options())) {
if (pool_->deprecated_legacy_json_field_conflicts_ ||
IsLegacyJsonFieldConflictEnabled(result->options())) {
if (syntax == FileDescriptor::SYNTAX_PROTO3) {
// Only check default JSON names for conflicts in proto3. This is legacy
// behavior that will be removed in a later version.
@ -6061,7 +6065,8 @@ void DescriptorBuilder::CheckEnumValueUniqueness(
value->name(), insert_result.first->second->name());
// There are proto2 enums out there with conflicting names, so to preserve
// compatibility we issue only a warning for proto2.
if (IsLegacyJsonFieldConflictEnabled(result->options()) &&
if ((pool_->deprecated_legacy_json_field_conflicts_ ||
IsLegacyJsonFieldConflictEnabled(result->options())) &&
result->file()->syntax() == FileDescriptor::SYNTAX_PROTO2) {
AddWarning(value->full_name(), proto.value(i),
DescriptorPool::ErrorCollector::NAME, error_message);

@ -63,6 +63,7 @@
#include "google/protobuf/stubs/common.h"
#include "google/protobuf/port.h"
#include "absl/base/attributes.h"
#include "absl/base/call_once.h"
#include "absl/container/flat_hash_map.h"
#include "google/protobuf/stubs/logging.h"
@ -1991,6 +1992,12 @@ class PROTOBUF_EXPORT DescriptorPool {
// Disallow [enforce_utf8 = false] in .proto files.
void DisallowEnforceUtf8() { disallow_enforce_utf8_ = true; }
// Use the deprecated legacy behavior for handling JSON field name conflicts.
ABSL_DEPRECATED("Deprecated treatment of field name conflicts is enabled.")
void UseDeprecatedLegacyJsonFieldConflicts() {
deprecated_legacy_json_field_conflicts_ = true;
}
// For internal use only: Gets a non-const pointer to the generated pool.
// This is called at static-initialization time only, so thread-safety is
@ -2119,6 +2126,7 @@ class PROTOBUF_EXPORT DescriptorPool {
bool allow_unknown_;
bool enforce_weak_;
bool disallow_enforce_utf8_;
bool deprecated_legacy_json_field_conflicts_;
// Set of files to track for unused imports. The bool value when true means
// unused imports are treated as errors (and as warnings when false).

Loading…
Cancel
Save