Skip to content

Commit

Permalink
Event - serialize non-serializable fields as str (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerShor authored May 31, 2023
1 parent f8021b0 commit aec8417
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion nuclio_sdk/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def to_json(self):
if isinstance(obj["body"], bytes):
obj["body"] = base64.b64encode(obj["body"]).decode("ascii")

return json.dumps(obj)
return json.dumps(obj, default=str)

def get_header(self, header_key):
for key, value in self.headers.items():
Expand Down
9 changes: 8 additions & 1 deletion nuclio_sdk/test/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
import json

import nuclio_sdk.test
Expand Down Expand Up @@ -49,6 +49,13 @@ def test_event_to_json_string_body(self):
serialized_event = self._deserialize_event(event)
self.assertEqual(request_body, serialized_event.body)

def test_print_event(self):
"""
Test that printing an event doesn't raise an exception
"""
event = nuclio_sdk.Event(body=datetime.datetime(2022, 1, 1))
print(event)

def _deserialize_event(self, event):
raise NotImplementedError

Expand Down

0 comments on commit aec8417

Please sign in to comment.