8000 Fixing bug if channel sends empty string for a null object (#1889) · sysang/botbuilder-python@b11e029 · GitHub
[go: up one dir, main page]

Skip to content

Commit b11e029

Browse files
Fixing bug if channel sends empty string for a null object (microsoft#1889)
Co-authored-by: tracyboehrer <tracyboehrer@users.noreply.github.com>
1 parent 16f55f8 commit b11e029

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

libraries/botbuilder-core/botbuilder/core/serializer_helper.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3-
3+
from copy import copy
44
from inspect import getmembers
55
from typing import Type
66
from enum import Enum
@@ -25,6 +25,9 @@
2525

2626
def deserializer_helper(msrest_cls: Type[Model], dict_to_deserialize: dict) -> Model:
2727
deserializer = Deserializer(DEPENDICIES_DICT)
28+
_clean_data_for_serialization(
29+
deserializer.dependencies[msrest_cls.__name__], dict_to_deserialize
30+
)
2831
return deserializer(msrest_cls.__name__, dict_to_deserialize)
2932

3033

@@ -35,3 +38,21 @@ def serializer_helper(object_to_serialize: Model) -> dict:
3538
serializer = Serializer(DEPENDICIES_DICT)
3639
# pylint: disable=protected-access
3740
return serializer._serialize(object_to_serialize)
41+
42+
43+
def _clean_data_for_serialization(msrest_cls: Type[Model], dict_to_deserialize: dict):
44+
# pylint: disable=protected-access
45+
# Clean channel response of empty strings for expected objects.
46+
if not isinstance(dict_to_deserialize, dict):
47+
return
48+
serialization_model = copy(msrest_cls._attribute_map)
49+
for key, value in msrest_cls._attribute_map.items():
50+
if key != value["key"]:
51+
serialization_model[value["key"]] = value
52+
for prop, prop_value in dict_to_deserialize.items():
53+
if (
54+
prop in serialization_model
55+
and serialization_model[prop]["type"] in DEPENDICIES_DICT
56+
and not prop_value
57+
):
58+
dict_to_deserialize[prop] = None

libraries/botbuilder-core/tests/teams/test_teams_activity_handler.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,25 @@ async def test_on_teams_task_module_fetch(self):
10031003
assert bot.record[0] == "on_invoke_activity"
10041004
assert bot.record[1] == "on_teams_task_module_fetch"
10051005

1006+
async def test_on_teams_task_module_fetch_none_as_empty(self):
1007+
# Arrange
1008+
activity = Activity(
1009+
type=ActivityTypes.invoke,
1010+
9A9B name="task/fetch",
1011+
value={"data": {"key": "value"}, "context": "",},
1012+
)
1013+
1014+
turn_context = TurnContext(SimpleAdapter(), activity)
1015+
1016+
# Act
1017+
bot = TestingTeamsActivityHandler()
1018+
await bot.on_turn(turn_context)
1019+
1020+
# Assert
1021+
assert len(bot.record) == 2
1022+
assert bot.record[0] == "on_invoke_activity"
1023+
assert bot.record[1] == "on_teams_task_module_fetch"
1024+
10061025
async def test_on_teams_task_module_submit(self):
10071026
# Arrange
10081027
activity = Activity(

0 commit comments

Comments
 (0)
0