Skip to content

fix: Handle null AttributeValues when serializing DynamoDBEvent to JSON #1689

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

Merged
merged 1 commit into from
Mar 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFrameworks>netcoreapp3.1;net8.0</TargetFrameworks>
<Description>Amazon Lambda .NET Core support - DynamoDBEvents package.</Description>
<AssemblyTitle>Amazon.Lambda.DynamoDBEvents</AssemblyTitle>
<VersionPrefix>3.1.0</VersionPrefix>
<VersionPrefix>3.1.1</VersionPrefix>
<AssemblyName>Amazon.Lambda.DynamoDBEvents</AssemblyName>
<PackageId>Amazon.Lambda.DynamoDBEvents</PackageId>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ private static void WriteJsonValue(Utf8JsonWriter writer, AttributeValue attribu
}
writer.WriteEndArray();
}
else
{
writer.WriteNullValue();
}
}
}
}
53 changes: 53 additions & 0 deletions Libraries/test/EventsTests.Shared/DynamoDBEventJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,58 @@ public void BinarySet_ToDocument()
Assert.Equal("hello world", Encoding.UTF8.GetString(list[0].AsByteArray()));
Assert.Equal("hello world!", Encoding.UTF8.GetString(list[1].AsByteArray()));
}

[Fact]
public void NullAttributes_ToJson()
{
var evnt = PrepareEvent(new Dictionary<string, AttributeValue>()
{
{ "Null", new AttributeValue {NULL = null } },
{ "Empty", new AttributeValue() }
});

var json = evnt.Records[0].Dynamodb.NewImage.ToJson();

Assert.Equal("{\"Null\":null,\"Empty\":null}", json);
}

[Fact]
public void NullAttributes_ToDocument()
{
var evnt = PrepareEvent(new Dictionary<string, AttributeValue>()
{
{ "Null", new AttributeValue {NULL = null } },
{ "Empty", new AttributeValue() }
});

// Convert the event from the Lambda package to the SDK type
var json = evnt.Records[0].Dynamodb.NewImage.ToJson();
var document = Document.FromJson(json);

Assert.Equal(DynamoDBNull.Null, document["Null"].AsDynamoDBNull());
Assert.Equal(DynamoDBNull.Null, document["Empty"].AsDynamoDBNull());
}

[Fact]
public void NoAttributes_ToJson()
{
var evnt = PrepareEvent(new Dictionary<string, AttributeValue>());

var json = evnt.Records[0].Dynamodb.NewImage.ToJson();

Assert.Equal("{}", json);
}

[Fact]
public void NoAttributes_ToDocument()
{
var evnt = PrepareEvent(new Dictionary<string, AttributeValue>());

// Convert the event from the Lambda package to the SDK type
var json = evnt.Records[0].Dynamodb.NewImage.ToJson();
var document = Document.FromJson(json);

Assert.Equal(0, document.Count);
}
}
}