Skip to content

Commit

Permalink
feat: Made enum name parsing case-insensitive in Python
Browse files Browse the repository at this point in the history
The enum names are expected to be case-insensitive.

Fixes #15757

PiperOrigin-RevId: 604904921
  • Loading branch information
Ark-kun authored and copybara-github committed Sep 20, 2024
1 parent 7d3e80c commit 825952b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions conformance/binary_json_conformance_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,9 @@ void BinaryAndJsonConformanceSuiteImpl<
// Enum fields.
RunValidJsonTest("EnumField", REQUIRED, R"({"optionalNestedEnum": "FOO"})",
"optional_nested_enum: FOO");
RunValidJsonTest("EnumFieldDifferentCase", RECOMMENDED,
R"({"optionalNestedEnum": "fOo"})",
"optional_nested_enum: FOO");

// Enum fields with alias
if (run_proto3_tests_) {
Expand Down
8 changes: 8 additions & 0 deletions conformance/failure_list_cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ Recommended.*.FieldMaskNumbersDontRoundTrip.JsonOutput
Recommended.*.FieldMaskPathsDontRoundTrip.JsonOutput # Should have failed to serialize, but didn't.
Recommended.*.FieldMaskTooManyUnderscore.JsonOutput # Should have failed to serialize, but didn't.
Recommended.*.JsonInput.FieldMaskInvalidCharacter # Should have failed to parse, but didn't.
Recommended.Editions_Proto2.JsonInput.EnumFieldDifferentCase.JsonOutput
Recommended.Editions_Proto2.JsonInput.EnumFieldDifferentCase.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.EnumFieldDifferentCase.JsonOutput
Recommended.Editions_Proto3.JsonInput.EnumFieldDifferentCase.ProtobufOutput
Recommended.Proto2.JsonInput.EnumFieldDifferentCase.JsonOutput
Recommended.Proto2.JsonInput.EnumFieldDifferentCase.ProtobufOutput
Recommended.Proto3.JsonInput.EnumFieldDifferentCase.JsonOutput
Recommended.Proto3.JsonInput.EnumFieldDifferentCase.ProtobufOutput
2 changes: 2 additions & 0 deletions conformance/failure_list_java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Recommended.*.FieldMaskNumbersDontRoundTrip.JsonOutput
Recommended.*.FieldMaskPathsDontRoundTrip.JsonOutput # Should have failed to serialize, but didn't.
Recommended.*.FieldMaskTooManyUnderscore.JsonOutput # Should have failed to serialize, but didn't.
Recommended.*.JsonInput.FieldMaskInvalidCharacter # Should have failed to parse, but didn't.
Recommended.*.JsonInput.EnumFieldDifferentCase.JsonOutput
Recommended.*.JsonInput.EnumFieldDifferentCase.ProtobufOutput
Required.*.JsonInput.EnumFieldNotQuoted # Should have failed to parse, but didn't.
Required.*.JsonInput.Int32FieldLeadingZero # Should have failed to parse, but didn't.
Required.*.JsonInput.Int32FieldNegativeWithLeadingZero # Should have failed to parse, but didn't.
Expand Down
11 changes: 11 additions & 0 deletions python/google/protobuf/json_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,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)
Expand Down

0 comments on commit 825952b

Please sign in to comment.