Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#97 Fixed deserializing of EventBridge object body #98

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion src/AWS.Messaging/Serialization/EnvelopeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ private MessageEnvelopeConfiguration GetMessageEnvelopeConfiguration(Message sqs
&& root.TryGetProperty("region", out var _))
{
// Retrieve the inner message envelope.
envelopeConfiguration.MessageEnvelopeBody = innerEnvelope.GetString();
envelopeConfiguration.MessageEnvelopeBody = innerEnvelope.ValueKind switch
{
JsonValueKind.String => innerEnvelope.GetString(),
JsonValueKind.Object => innerEnvelope.ToString(),
_ => throw new InvalidDataException($"detail has unexpected json kind: {innerEnvelope.ValueKind}")
};
if (string.IsNullOrEmpty(envelopeConfiguration.MessageEnvelopeBody))
{
_logger.LogError("Failed to create a message envelope configuration because the EventBridge message envelope does not contain a valid 'detail' property.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,24 @@ public async Task ConvertToEnvelopeAsync_DataMessageLogging_WithError(bool dataM
Assert.Null(exception.InnerException);
}
}

[Fact]
public async Task Deserialize_Eventbridge_Sqs_Envelope()
{
const string eventbridgeSqsEnvelope = "{\"Attributes\":{\"SenderId\":\"AIDAIP3MER2HFHNCCMVD4\",\"ApproximateFirstReceiveTimestamp\":\"1708335393527\",\"ApproximateReceiveCount\":\"1\",\"SentTimestamp\":\"1708335388527\"},\"Body\":\"{\\u0022version\\u0022:\\u00220\\u0022,\\u0022id\\u0022:\\u0022de3d19d6-755a-b9b1-c625-1708a2c36b50\\u0022,\\u0022detail-type\\u0022:\\u0022ChatMessage\\u0022,\\u0022source\\u0022:\\u0022ServiceNow/Integration\\u0022,\\u0022account\\u0022:\\u0022041717511598\\u0022,\\u0022time\\u0022:\\u00222024-02-19T09:36:28Z\\u0022,\\u0022region\\u0022:\\u0022eu-central-1\\u0022,\\u0022resources\\u0022:[],\\u0022detail\\u0022:{\\u0022id\\u0022:\\u00226ef9516a-6565-4e81-8177-be89d207fab0\\u0022,\\u0022source\\u0022:\\u0022ServiceNow/Integration\\u0022,\\u0022specversion\\u0022:\\u00221.0\\u0022,\\u0022type\\u0022:\\u0022ChatMessage\\u0022,\\u0022time\\u0022:\\u00222024-02-19T09:36:28.6090473\\u002B00:00\\u0022,\\u0022data\\u0022:\\u0022{\\\\\\u0022MessageDescription\\\\\\u0022:\\\\\\u0022Hello\\\\\\u0022}\\u0022}}\",\"MD5OfBody\":\"fa0ed402839bcce842343be5a5b0e758\",\"MD5OfMessageAttributes\":null,\"MessageAttributes\":{},\"MessageId\":\"83b53c15-8838-4ea1-89ae-97990548f8e4\",\"ReceiptHandle\":\"AQEBrLb6AkV/lUdrvNnEQjxK24LJLPU9\\u002BJko30VRmXIXljTrbfzHGZP3wuGCFSnaBUvUPTHGWU6rxRXCDnifFswSzuCyYjtJV95N3UajyEjkau\\u002BsBZdpYvWE4SKJSrh69HzKCS1QZ8Izula1jDA4dSYDl578kYY67uCKCvyRPtCKq7r5mveThv\\u002BRik1K9zmMrX9urwg8OgCsWcPEOu7duKJEsCSchorNoXjCVRycK7/mqyP1ndeiWc6SBefR9Coiynp2tsA6qFmkCwZNx4hqLINclkV4KyidX5cpdORMY74eoZ/puE6xrwQTJCJL2QKmnegxf645RL0i1tS2nSCeDUVwuQeebaSHbI0dKTyhtFvK/O2V8S39/siAHKz\\u002BCCaUo0YhtJ6FG1tm0xytwo5la2ysi5BKRrlf9thZ16pNaiNmKyI=\"}";
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you please replace any sensitive info with dummy data? I'm particularly worried about "SenderId".

var logger = new Mock<ILogger<EnvelopeSerializer>>();
var messageConfiguration = new MessageConfiguration { LogMessageContent = false };
var messageSerializer = new Mock<IMessageSerializer>();
var dateTimeHandler = new Mock<IDateTimeHandler>();
var messageIdGenerator = new Mock<IMessageIdGenerator>();
var messageSourceHandler = new Mock<IMessageSourceHandler>();
var envelopeSerializer = new EnvelopeSerializer(logger.Object, messageConfiguration, messageSerializer.Object, dateTimeHandler.Object, messageIdGenerator.Object, messageSourceHandler.Object);
var message = JsonSerializer.Deserialize<Message>(eventbridgeSqsEnvelope);
Assert.NotNull(message);
messageConfiguration.SubscriberMappings.Add(new(typeof(ChatMessageHandler), typeof(ChatMessage), nameof(ChatMessage)));
var result = await envelopeSerializer.ConvertToEnvelopeAsync(message);
Assert.NotNull(result.Envelope.EventBridgeMetadata);
}
}

public class MockSerializationCallback : ISerializationCallback
Expand Down
Loading