From 6e0910e5d1965671dcec493d737fc8e7f0200bfa Mon Sep 17 00:00:00 2001 From: Alexey Volkov Date: Wed, 7 Feb 2024 01:37:10 -0800 Subject: [PATCH] feat: Made enum name parsing case-insensitive in Python The enum names are expected to be case-insensitive. Fixes https://github.com/protocolbuffers/protobuf/issues/15757 PiperOrigin-RevId: 604904921 --- python/google/protobuf/json_format.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/python/google/protobuf/json_format.py b/python/google/protobuf/json_format.py index 8a2e24975bdc2..e7dd5a9742d8e 100644 --- a/python/google/protobuf/json_format.py +++ b/python/google/protobuf/json_format.py @@ -911,6 +911,17 @@ def _ConvertScalarFieldValue(value, field, path, require_str=False): elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM: # Convert an enum value. enum_value = field.enum_type.values_by_name.get(value, None) + if enum_value is None and isinstance(value, str): + # Performing case insensitive search + enum_values = [ + enum_value + for enum_name, enum_value in field.enum_type.values_by_name.items() + if enum_name.upper() == value.upper() + ] + # Note: There can be multiple items where the name only differs in case. + # Let's take the 1st one. + if enum_values: + enum_value = enum_values[0] if enum_value is None: try: number = int(value)