Skip to content

Commit 432ae51

Browse files
authored
Fix NPE when SQS message has no message attributes (#213)
1 parent e424cfc commit 432ae51

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "AWS.Messaging.Lambda",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Fix null pointer exception when message contains no SQS message attributes"
8+
]
9+
}
10+
]
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Runtime.CompilerServices;
5+
6+
[assembly: InternalsVisibleTo("AWS.Messaging.UnitTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100db5f59f098d27276c7833875a6263a3cc74ab17ba9a9df0b52aedbe7252745db7274d5271fd79c1f08f668ecfa8eaab5626fa76adc811d3c8fc55859b0d09d3bc0a84eecd0ba891f2b8a2fc55141cdcc37c2053d53491e650a479967c3622762977900eddbf1252ed08a2413f00a28f3a0752a81203f03ccb7f684db373518b4")]
7+
[assembly: InternalsVisibleTo("AWS.Messaging.IntegrationTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100db5f59f098d27276c7833875a6263a3cc74ab17ba9a9df0b52aedbe7252745db7274d5271fd79c1f08f668ecfa8eaab5626fa76adc811d3c8fc55859b0d09d3bc0a84eecd0ba891f2b8a2fc55141cdcc37c2053d53491e650a479967c3622762977900eddbf1252ed08a2413f00a28f3a0752a81203f03ccb7f684db373518b4")]
8+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]

src/AWS.Messaging.Lambda/Services/DefaultLambdaMessageProcessor.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public ValueTask ReportMessageFailureAsync(MessageEnvelope message, Cancellation
337337
return ValueTask.CompletedTask;
338338
}
339339

340-
private Message ConvertToStandardSQSMessage(SQSEvent.SQSMessage sqsEventMessage)
340+
internal static Message ConvertToStandardSQSMessage(SQSEvent.SQSMessage sqsEventMessage)
341341
{
342342
var sqsMessage = new Message
343343
{
@@ -349,16 +349,19 @@ private Message ConvertToStandardSQSMessage(SQSEvent.SQSMessage sqsEventMessage)
349349
ReceiptHandle = sqsEventMessage.ReceiptHandle,
350350
};
351351

352-
foreach (var attr in sqsEventMessage.MessageAttributes)
352+
if (sqsEventMessage.MessageAttributes != null)
353353
{
354-
sqsMessage.MessageAttributes.Add(attr.Key, new MessageAttributeValue
354+
foreach (var attr in sqsEventMessage.MessageAttributes)
355355
{
356-
BinaryListValues = attr.Value.BinaryListValues,
357-
BinaryValue = attr.Value.BinaryValue,
358-
DataType = attr.Value.DataType,
359-
StringListValues = attr.Value.StringListValues,
360-
StringValue = attr.Value.StringValue,
361-
});
356+
sqsMessage.MessageAttributes.Add(attr.Key, new MessageAttributeValue
357+
{
358+
BinaryListValues = attr.Value.BinaryListValues,
359+
BinaryValue = attr.Value.BinaryValue,
360+
DataType = attr.Value.DataType,
361+
StringListValues = attr.Value.StringListValues,
362+
StringValue = attr.Value.StringValue,
363+
});
364+
}
362365
}
363366

364367
return sqsMessage;

test/AWS.Messaging.UnitTests/LambdaTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
using AWS.Messaging.Lambda;
1515
using AWS.Messaging.Serialization;
1616
using AWS.Messaging.UnitTests.Models;
17+
using AWS.Messaging.Lambda.Services;
1718
using Microsoft.Extensions.DependencyInjection;
1819
using Moq;
1920
using Xunit;
2021
using static Amazon.Lambda.SQSEvents.SQSEvent;
22+
using Amazon;
2123

2224
namespace AWS.Messaging.UnitTests;
2325

@@ -312,6 +314,65 @@ await Execute(new SimulatedMessage[] { message4B, message1A, message2A, message5
312314
Assert.True(message5BPos < message6BPos);
313315
}
314316

317+
[Fact]
318+
public void ConvertToSDKMessage()
319+
{
320+
var lambdaMessage = new SQSEvent.SQSMessage
321+
{
322+
Attributes = new Dictionary<string, string> { { "key", "value" } },
323+
Body = "body",
324+
Md5OfMessageAttributes = "md5Attributes",
325+
MessageId = "messageId",
326+
ReceiptHandle = "handle"
327+
};
328+
329+
var sdkMessage = DefaultLambdaMessageProcessor.ConvertToStandardSQSMessage(lambdaMessage);
330+
331+
Assert.Equal(lambdaMessage.Body, sdkMessage.Body);
332+
Assert.Equal(lambdaMessage.Md5OfMessageAttributes, sdkMessage.MD5OfMessageAttributes);
333+
Assert.Equal(lambdaMessage.MessageId, sdkMessage.MessageId);
334+
Assert.Equal(lambdaMessage.ReceiptHandle, sdkMessage.ReceiptHandle);
335+
Assert.True(object.ReferenceEquals(lambdaMessage.Attributes, sdkMessage.Attributes));
336+
337+
if (AWSConfigs.InitializeCollections)
338+
{
339+
Assert.Empty(sdkMessage.MessageAttributes);
340+
}
341+
else
342+
{
343+
Assert.Null(sdkMessage.MessageAttributes);
344+
}
345+
346+
lambdaMessage.MessageAttributes = new Dictionary<string, MessageAttribute>
347+
{
348+
{"keyString", new MessageAttribute
349+
{
350+
DataType = "String",
351+
StringValue = "TheString"
352+
}
353+
},
354+
{"keyBinary", new MessageAttribute
355+
{
356+
DataType = "Binary",
357+
BinaryValue = new System.IO.MemoryStream(new byte[]{1,2,3})
358+
}
359+
}
360+
};
361+
362+
sdkMessage = DefaultLambdaMessageProcessor.ConvertToStandardSQSMessage(lambdaMessage);
363+
364+
365+
Assert.Equal(lambdaMessage.Body, sdkMessage.Body);
366+
Assert.Equal(lambdaMessage.Md5OfMessageAttributes, sdkMessage.MD5OfMessageAttributes);
367+
Assert.Equal(lambdaMessage.MessageId, sdkMessage.MessageId);
368+
Assert.Equal(lambdaMessage.ReceiptHandle, sdkMessage.ReceiptHandle);
369+
Assert.True(object.ReferenceEquals(lambdaMessage.Attributes, sdkMessage.Attributes));
370+
371+
Assert.Equal(2, sdkMessage.MessageAttributes.Count);
372+
Assert.Equal("Binary", sdkMessage.MessageAttributes["keyBinary"].DataType);
373+
Assert.Equal(3, sdkMessage.MessageAttributes["keyBinary"].BinaryValue.Length);
374+
}
375+
315376
private async Task<string> Execute(SimulatedMessage[] messages, int maxNumberOfConcurrentMessages = 1, bool deleteMessagesWhenCompleted = false, bool addInvalidMessageFormatRecord = false, bool isFifoQueue = false)
316377
{
317378
var provider = CreateServiceProvider(maxNumberOfConcurrentMessages: maxNumberOfConcurrentMessages, deleteMessagesWhenCompleted: deleteMessagesWhenCompleted, isFifoQueue: isFifoQueue);

0 commit comments

Comments
 (0)