Events: put_events() now throws a warning when Detail field is not a dict (#6875)

This commit is contained in:
Bert Blommers 2023-10-02 07:50:47 +00:00 committed by GitHub
parent 9767f0226d
commit f5a69d531d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -1327,7 +1327,11 @@ class EventsBackend(BaseBackend):
) )
else: else:
try: try:
json.loads(event["Detail"]) detail = json.loads(event["Detail"])
if not isinstance(detail, dict):
warnings.warn(
f"EventDetail should be of type dict - types such as {type(detail)} are ignored by AWS"
)
except ValueError: # json.JSONDecodeError exists since Python 3.5 except ValueError: # json.JSONDecodeError exists since Python 3.5
entries.append( entries.append(
{ {

View File

@ -1,13 +1,14 @@
import json import json
import random import random
import unittest import unittest
import warnings
from datetime import datetime, timezone from datetime import datetime, timezone
import boto3 import boto3
import pytest import pytest
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
from moto import mock_logs from moto import mock_logs, settings
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
from moto.core.utils import iso_8601_datetime_without_milliseconds from moto.core.utils import iso_8601_datetime_without_milliseconds
from moto.events import mock_events from moto.events import mock_events
@ -682,6 +683,12 @@ def test_put_events():
assert response["FailedEntryCount"] == 0 assert response["FailedEntryCount"] == 0
assert len(response["Entries"]) == 1 assert len(response["Entries"]) == 1
if settings.TEST_DECORATOR_MODE:
event["Detail"] = json.dumps([{"Key": "k", "Value": "v"}])
with warnings.catch_warnings(record=True) as w:
client.put_events(Entries=[event])
assert "EventDetail should be of type dict" in str(w[0].message)
@mock_events @mock_events
def test_put_events_error_too_many_entries(): def test_put_events_error_too_many_entries():