Skip to content

Commit 3a405a7

Browse files
muhammad-othmanashovlin
authored andcommitted
fix DynamoDBContext.FromDocument to handle nulls
1 parent cd6a9e3 commit 3a405a7

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

sdk/src/Services/DynamoDBv2/Custom/Conversion/DynamoDBEntryConversion.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,12 @@ public bool TryFromEntry(DynamoDBEntry entry, Type targetType, out object value)
573573
if (d != null && TryFrom(d, targetType, out value))
574574
return true;
575575

576-
//var n = entry as DynamoDBNull;
577-
//if (n != null)
578-
// return null;
579-
580576
value = null;
577+
578+
var n = entry as DynamoDBNull;
579+
if (n != null)
580+
return true;
581+
581582
return false;
582583
}
583584

sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB
370370
return conversion.ConvertFromEntry(targetType, entry);
371371
else
372372
{
373+
if (entry is DynamoDBNull)
374+
return null;
375+
373376
object output;
374377
Document document = entry as Document;
375378
if (document != null)

sdk/test/Services/DynamoDBv2/UnitTests/Custom/DynamoDBTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Amazon.DynamoDBv2.DocumentModel;
1212
using Amazon.DynamoDBv2.DataModel;
1313
using Amazon.DynamoDBv2.Model;
14+
using Amazon.DynamoDBv2;
1415
using ThirdParty.Json.LitJson;
1516

1617
using Moq;
@@ -243,6 +244,72 @@ public void TestExplicitNullPropertyOnDocument()
243244
Assert.IsNull(doc["Name"].AsPrimitive().Value);
244245
}
245246

247+
[TestMethod]
248+
[TestCategory("DynamoDBv2")]
249+
public void TestFromJsonCanHandleAllDateTypes()
250+
{
251+
var json = @"
252+
{
253+
""StringValue"": ""test string"",
254+
""BoolValue"": true,
255+
""IntValue"": 200,
256+
""DateValue"": ""2022-12-29T12:46:14.097Z"",
257+
""NullableBoolValue"": null,
258+
""NullableIntValue"": null,
259+
""NullableDateValue"": null,
260+
""SubData"": null,
261+
""SubData2"": {
262+
""StringValue"": null,
263+
""NullableBoolValue"": false,
264+
""NullableIntValue"": 500,
265+
""NullableDateValue"": ""2022-12-28T12:46:14.097Z""
266+
}
267+
}";
268+
269+
using (var dbClient = new AmazonDynamoDBClient())
270+
using (var context = new DynamoDBContext(dbClient))
271+
{
272+
var document = Document.FromJson(json);
273+
var container = context.FromDocument<DataContainer>(document);
274+
275+
Assert.IsNotNull(container);
276+
Assert.AreEqual(container.StringValue, "test string");
277+
Assert.AreEqual(container.BoolValue, true);
278+
Assert.AreEqual(container.IntValue, 200);
279+
Assert.AreEqual(container.DateValue, DateTime.Parse("2022-12-29T12:46:14.097Z"));
280+
Assert.IsNull(container.NullableBoolValue);
281+
Assert.IsNull(container.NullableIntValue);
282+
Assert.IsNull(container.NullableDateValue);
283+
Assert.IsNull(container.SubData);
284+
Assert.IsNotNull(container.SubData2);
285+
Assert.IsNull(container.SubData2.StringValue);
286+
Assert.IsNotNull(container.SubData2.NullableIntValue);
287+
}
288+
}
289+
290+
public class DataContainer
291+
{
292+
public string StringValue { get; set; }
293+
public bool BoolValue { get; set; }
294+
public int IntValue { get; set; }
295+
public DateTime DateValue { get; set; }
296+
297+
public bool? NullableBoolValue { get; set; }
298+
public int? NullableIntValue { get; set; }
299+
public DateTime? NullableDateValue { get; set; }
300+
301+
public SubContainer SubData { get; set; }
302+
public SubContainer SubData2 { get; set; }
303+
304+
public class SubContainer
305+
{
306+
public string StringValue { get; set; }
307+
public bool? NullableBoolValue { get; set; }
308+
public int? NullableIntValue { get; set; }
309+
public DateTime? NullableDateValue { get; set; }
310+
}
311+
}
312+
246313
#if ASYNC_AWAIT
247314
[TestMethod]
248315
[TestCategory("DynamoDBv2")]

0 commit comments

Comments
 (0)