Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Made enum name parsing case-insensitive in Python #18437

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading