Skip to content

Commit

Permalink
Fix python JSON parser to accept string fields containing integer-equ…
Browse files Browse the repository at this point in the history
…ivalent floats.

This fixes non-conformant behavior in python.

PiperOrigin-RevId: 704529468
  • Loading branch information
mkruskal-google authored and copybara-github committed Dec 10, 2024
1 parent f69ea1c commit f2ec951
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
1 change: 0 additions & 1 deletion conformance/failure_list_python.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output.
1 change: 0 additions & 1 deletion conformance/failure_list_python_cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
#
# TODO: insert links to corresponding bugs tracking the issue.
# Should we use GitHub issues or the Google-internal bug tracker?
Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output.
1 change: 0 additions & 1 deletion conformance/failure_list_python_upb.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output.
19 changes: 19 additions & 0 deletions python/google/protobuf/internal/json_format_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ def testIntegersRepresentedAsFloat(self):
json_format.Parse('{"int32Value": 1.0}', message)
self.assertEqual(message.int32_value, 1)

def testIntegersRepresentedAsFloatStrings(self):
message = json_format_proto3_pb2.TestMessage()
json_format.Parse('{"int32Value": "-2.147483648e9"}', message)
self.assertEqual(message.int32_value, -2147483648)
json_format.Parse('{"int32Value": "1e5"}', message)
self.assertEqual(message.int32_value, 100000)
json_format.Parse('{"int32Value": "1.0"}', message)
self.assertEqual(message.int32_value, 1)

def testMapFields(self):
message = json_format_proto3_pb2.TestNestedMap()
self.assertEqual(
Expand Down Expand Up @@ -1167,6 +1176,16 @@ def testInvalidIntegerValue(self):
'Failed to parse int32Value field: '
"Couldn't parse integer: 1.5 at TestMessage.int32Value.",
)
self.CheckError(
'{"int32Value": "1.5"}',
'Failed to parse int32Value field: '
'Couldn\'t parse non-integer string: "1.5" at TestMessage.int32Value.',
)
self.CheckError(
'{"int32Value": "foo"}',
'Failed to parse int32Value field: invalid literal for int\(\) with'
" base 10: 'foo'.",
)
self.CheckError(
'{"int32Value": 012345}',
(r'Failed to load JSON: Expecting \'?,\'? delimiter: ' r'line 1.'),
Expand Down
15 changes: 14 additions & 1 deletion python/google/protobuf/json_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,20 @@ def _ConvertInteger(value):
'Bool value {0} is not acceptable for integer field'.format(value)
)

return int(value)
try:
return int(value)
except ValueError as e:
# Attempt to parse as an integer-valued float.
try:
f = float(value)
except ValueError:
# Raise the original exception for the int parse.
raise e # pylint: disable=raise-missing-from
if not f.is_integer():
raise ParseError(
'Couldn\'t parse non-integer string: "{0}"'.format(value)
) from e
return int(f)


def _ConvertFloat(value, field):
Expand Down

0 comments on commit f2ec951

Please sign in to comment.