Skip to content

Commit 633e457

Browse files
authored
fix: Handle null AttributeValues when serializing DynamoDBEvent to JSON (#1689)
1 parent 9569c1a commit 633e457

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

Libraries/src/Amazon.Lambda.DynamoDBEvents/Amazon.Lambda.DynamoDBEvents.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<TargetFrameworks>netcoreapp3.1;net8.0</TargetFrameworks>
77
<Description>Amazon Lambda .NET Core support - DynamoDBEvents package.</Description>
88
<AssemblyTitle>Amazon.Lambda.DynamoDBEvents</AssemblyTitle>
9-
<VersionPrefix>3.1.0</VersionPrefix>
9+
<VersionPrefix>3.1.1</VersionPrefix>
1010
<AssemblyName>Amazon.Lambda.DynamoDBEvents</AssemblyName>
1111
<PackageId>Amazon.Lambda.DynamoDBEvents</PackageId>
1212
<PackageTags>AWS;Amazon;Lambda</PackageTags>

Libraries/src/Amazon.Lambda.DynamoDBEvents/ExtensionMethods.cs

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ private static void WriteJsonValue(Utf8JsonWriter writer, AttributeValue attribu
152152
}
153153
writer.WriteEndArray();
154154
}
155+
else
156+
{
157+
writer.WriteNullValue();
158+
}
155159
}
156160
}
157161
}

Libraries/test/EventsTests.Shared/DynamoDBEventJsonTests.cs

+53
Original file line numberDiff line numberDiff line change
@@ -441,5 +441,58 @@ public void BinarySet_ToDocument()
441441
Assert.Equal("hello world", Encoding.UTF8.GetString(list[0].AsByteArray()));
442442
Assert.Equal("hello world!", Encoding.UTF8.GetString(list[1].AsByteArray()));
443443
}
444+
445+
[Fact]
446+
public void NullAttributes_ToJson()
447+
{
448+
var evnt = PrepareEvent(new Dictionary<string, AttributeValue>()
449+
{
450+
{ "Null", new AttributeValue {NULL = null } },
451+
{ "Empty", new AttributeValue() }
452+
});
453+
454+
var json = evnt.Records[0].Dynamodb.NewImage.ToJson();
455+
456+
Assert.Equal("{\"Null\":null,\"Empty\":null}", json);
457+
}
458+
459+
[Fact]
460+
public void NullAttributes_ToDocument()
461+
{
462+
var evnt = PrepareEvent(new Dictionary<string, AttributeValue>()
463+
{
464+
{ "Null", new AttributeValue {NULL = null } },
465+
{ "Empty", new AttributeValue() }
466+
});
467+
468+
// Convert the event from the Lambda package to the SDK type
469+
var json = evnt.Records[0].Dynamodb.NewImage.ToJson();
470+
var document = Document.FromJson(json);
471+
472+
Assert.Equal(DynamoDBNull.Null, document["Null"].AsDynamoDBNull());
473+
Assert.Equal(DynamoDBNull.Null, document["Empty"].AsDynamoDBNull());
474+
}
475+
476+
[Fact]
477+
public void NoAttributes_ToJson()
478+
{
479+
var evnt = PrepareEvent(new Dictionary<string, AttributeValue>());
480+
481+
var json = evnt.Records[0].Dynamodb.NewImage.ToJson();
482+
483+
Assert.Equal("{}", json);
484+
}
485+
486+
[Fact]
487+
public void NoAttributes_ToDocument()
488+
{
489+
var evnt = PrepareEvent(new Dictionary<string, AttributeValue>());
490+
491+
// Convert the event from the Lambda package to the SDK type
492+
var json = evnt.Records[0].Dynamodb.NewImage.ToJson();
493+
var document = Document.FromJson(json);
494+
495+
Assert.Equal(0, document.Count);
496+
}
444497
}
445498
}

0 commit comments

Comments
 (0)