Skip to content

Commit

Permalink
Raise ParseError when JSON string is expected and not given (#7935)
Browse files Browse the repository at this point in the history
Raise ParseError when JSON string is expected and not given
  • Loading branch information
ikonst committed Jun 26, 2021
1 parent 611a08e commit 991bcad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
26 changes: 25 additions & 1 deletion python/google/protobuf/internal/json_format_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,30 @@ def testInvalidStruct(self):
'Failed to parse value field: Struct must be in a dict which is 1234',
json_format.Parse, text, message)

def testTimestampInvalidStringValue(self):
message = json_format_proto3_pb2.TestTimestamp()
text = '{"value": {"foo": 123}}'
self.assertRaisesRegexp(
json_format.ParseError,
r"Timestamp JSON value not a string: {u?'foo': 123}",
json_format.Parse, text, message)

def testDurationInvalidStringValue(self):
message = json_format_proto3_pb2.TestDuration()
text = '{"value": {"foo": 123}}'
self.assertRaisesRegexp(
json_format.ParseError,
r"Duration JSON value not a string: {u?'foo': 123}",
json_format.Parse, text, message)

def testFieldMaskInvalidStringValue(self):
message = json_format_proto3_pb2.TestFieldMask()
text = '{"value": {"foo": 123}}'
self.assertRaisesRegexp(
json_format.ParseError,
r"FieldMask JSON value not a string: {u?'foo': 123}",
json_format.Parse, text, message)

def testInvalidAny(self):
message = any_pb2.Any()
text = '{"@type": "type.googleapis.com/google.protobuf.Int32Value"}'
Expand Down Expand Up @@ -1217,7 +1241,7 @@ def testParseDictAnyDescriptorPoolMissingType(self):
def testParseDictUnknownValueType(self):
class UnknownClass(object):

def __str__(self):
def __repr__(self):
return 'v'
message = json_format_proto3_pb2.TestValue()
self.assertRaisesRegexp(
Expand Down
9 changes: 9 additions & 0 deletions python/google/protobuf/internal/well_known_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def FromJsonString(self, value):
Raises:
ValueError: On parsing problems.
"""
if not isinstance(value, six.string_types):
raise ValueError(
'Timestamp JSON value not a string: {!r}'.format(value))
timezone_offset = value.find('Z')
if timezone_offset == -1:
timezone_offset = value.find('+')
Expand Down Expand Up @@ -303,6 +306,9 @@ def FromJsonString(self, value):
Raises:
ValueError: On parsing problems.
"""
if not isinstance(value, six.string_types):
raise ValueError(
'Duration JSON value not a string: {!r}'.format(value))
if len(value) < 1 or value[-1] != 's':
raise ValueError(
'Duration must end with letter "s": {0}.'.format(value))
Expand Down Expand Up @@ -428,6 +434,9 @@ def ToJsonString(self):

def FromJsonString(self, value):
"""Converts string to FieldMask according to proto3 JSON spec."""
if not isinstance(value, six.string_types):
raise ValueError(
'FieldMask JSON value not a string: {!r}'.format(value))
self.Clear()
if value:
for path in value.split(','):
Expand Down

0 comments on commit 991bcad

Please sign in to comment.