Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from google.api_core.exceptions import Aborted
from google.cloud._helpers import _date_from_iso8601_date
from google.protobuf.internal.enum_type_wrapper import EnumTypeWrapper
from google.protobuf.message import Message
from google.protobuf.message import DecodeError, Message
from google.protobuf.struct_pb2 import ListValue, Value
from google.rpc.error_details_pb2 import RetryInfo

Expand Down Expand Up @@ -603,8 +603,14 @@ def _parse_proto(value_pb, column_info, field_name):
default_proto_message = column_info.get(field_name)
if isinstance(default_proto_message, Message):
proto_message = type(default_proto_message)()
proto_message.ParseFromString(bytes_value)
return proto_message
try:
proto_message.ParseFromString(bytes_value)
return proto_message
except (DecodeError, RecursionError):
log.warning(
"Warning: Field could not be parsed as Proto due to excessive nesting/corruption. Returning raw bytes."
)
return bytes_value
Comment on lines +610 to +613
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The variable log is not defined in this module; based on the project's conventions, it should be _LOGGER. Additionally, the "Warning: " prefix is redundant when using _LOGGER.warning().

_LOGGER.warning(
    "Field could not be parsed as Proto due to excessive nesting/corruption. Returning raw bytes."
)
return bytes_value

return bytes_value


Expand Down
51 changes: 51 additions & 0 deletions packages/google-cloud-spanner/tests/unit/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,57 @@ def test_w_proto_message(self):
self._callFUT(value_pb, field_type, field_name, column_info), VALUE
)

def test_w_proto_message_decode_error(self):
import base64
from unittest import mock

from google.protobuf.message import DecodeError
from google.protobuf.struct_pb2 import Value

from google.cloud.spanner_v1 import Type, TypeCode

from .testdata import singer_pb2

VALUE = singer_pb2.SingerInfo()
field_type = Type(code=TypeCode.PROTO)
field_name = "proto_message_column"
raw_bytes = VALUE.SerializeToString()
value_pb = Value(string_value=base64.b64encode(raw_bytes))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In Python 3, base64.b64encode returns a bytes object, but the string_value field of a Protobuf Value message expects a str. This will cause a TypeError during test execution. It should be decoded to a UTF-8 string.

value_pb = Value(string_value=base64.b64encode(raw_bytes).decode("utf-8"))

column_info = {"proto_message_column": singer_pb2.SingerInfo()}

# Mock ParseFromString to raise DecodeError
with mock.patch(
"google.protobuf.message.Message.ParseFromString",
side_effect=DecodeError("Mock Decode Error"),
):
result = self._callFUT(value_pb, field_type, field_name, column_info)
# Should return raw bytes
self.assertEqual(result, raw_bytes)

def test_w_proto_message_recursion_error(self):
import base64
from unittest import mock

from google.protobuf.struct_pb2 import Value

from google.cloud.spanner_v1 import Type, TypeCode

from .testdata import singer_pb2

VALUE = singer_pb2.SingerInfo()
field_type = Type(code=TypeCode.PROTO)
field_name = "proto_message_column"
raw_bytes = VALUE.SerializeToString()
value_pb = Value(string_value=base64.b64encode(raw_bytes))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In Python 3, base64.b64encode returns a bytes object, but the string_value field of a Protobuf Value message expects a str. This will cause a TypeError during test execution. It should be decoded to a UTF-8 string.

value_pb = Value(string_value=base64.b64encode(raw_bytes).decode("utf-8"))

column_info = {"proto_message_column": singer_pb2.SingerInfo()}

with mock.patch(
"google.protobuf.message.Message.ParseFromString",
side_effect=RecursionError("Mock Recursion Error"),
):
result = self._callFUT(value_pb, field_type, field_name, column_info)
self.assertEqual(result, raw_bytes)

def test_w_proto_enum(self):
from google.protobuf.struct_pb2 import Value

Expand Down
Loading