Skip to content

Commit 48de1c1

Browse files
Afroz Mohammedafroz429
authored andcommitted
Fixed NullReferenceException in Query using SelectValues.Count
1 parent 84b57dd commit 48de1c1

File tree

3 files changed

+90
-32
lines changed

3 files changed

+90
-32
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"services": [
3+
{
4+
"serviceName": "DynamoDBv2",
5+
"type": "patch",
6+
"changeLogMessages": [
7+
"Fixed NullReferenceException in Query using SelectValues.Count"
8+
]
9+
}
10+
]
11+
}

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Search.cs

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,19 @@ internal List<Document> GetNextSetHelper()
331331
SourceTable.UpdateRequestUserAgentDetails(scanReq, isAsync: false);
332332

333333
var scanResult = internalClient.Scan(scanReq);
334-
foreach (var item in scanResult.Items)
334+
if (scanResult.Items != null)
335335
{
336-
Document doc = SourceTable.FromAttributeMap(item);
337-
ret.Add(doc);
338-
if (CollectResults)
336+
foreach (var item in scanResult.Items)
339337
{
340-
Matches.Add(doc);
338+
Document doc = SourceTable.FromAttributeMap(item);
339+
ret.Add(doc);
340+
if (CollectResults)
341+
{
342+
Matches.Add(doc);
343+
}
341344
}
342345
}
346+
343347
NextKey = scanResult.LastEvaluatedKey;
344348
if (NextKey == null || NextKey.Count == 0)
345349
{
@@ -381,15 +385,19 @@ internal List<Document> GetNextSetHelper()
381385
SourceTable.UpdateRequestUserAgentDetails(queryReq, isAsync: false);
382386

383387
var queryResult = internalClient.Query(queryReq);
384-
foreach (var item in queryResult.Items)
388+
if (queryResult.Items != null)
385389
{
386-
Document doc = SourceTable.FromAttributeMap(item);
387-
ret.Add(doc);
388-
if (CollectResults)
390+
foreach (var item in queryResult.Items)
389391
{
390-
Matches.Add(doc);
392+
Document doc = SourceTable.FromAttributeMap(item);
393+
ret.Add(doc);
394+
if (CollectResults)
395+
{
396+
Matches.Add(doc);
397+
}
391398
}
392399
}
400+
393401
NextKey = queryResult.LastEvaluatedKey;
394402
if (NextKey == null || NextKey.Count == 0)
395403
{
@@ -445,15 +453,19 @@ internal async Task<List<Document>> GetNextSetHelperAsync(CancellationToken canc
445453
SourceTable.UpdateRequestUserAgentDetails(scanReq, isAsync: true);
446454

447455
var scanResult = await SourceTable.DDBClient.ScanAsync(scanReq, cancellationToken).ConfigureAwait(false);
448-
foreach (var item in scanResult.Items)
456+
if (scanResult.Items != null)
449457
{
450-
Document doc = SourceTable.FromAttributeMap(item);
451-
ret.Add(doc);
452-
if (CollectResults)
458+
foreach (var item in scanResult.Items)
453459
{
454-
Matches.Add(doc);
460+
Document doc = SourceTable.FromAttributeMap(item);
461+
ret.Add(doc);
462+
if (CollectResults)
463+
{
464+
Matches.Add(doc);
465+
}
455466
}
456467
}
468+
457469
NextKey = scanResult.LastEvaluatedKey;
458470
if (NextKey == null || NextKey.Count == 0)
459471
{
@@ -487,13 +499,16 @@ internal async Task<List<Document>> GetNextSetHelperAsync(CancellationToken canc
487499
SourceTable.UpdateRequestUserAgentDetails(queryReq, isAsync: true);
488500

489501
var queryResult = await SourceTable.DDBClient.QueryAsync(queryReq, cancellationToken).ConfigureAwait(false);
490-
foreach (var item in queryResult.Items)
502+
if (queryResult.Items != null)
491503
{
492-
Document doc = SourceTable.FromAttributeMap(item);
493-
ret.Add(doc);
494-
if (CollectResults)
504+
foreach (var item in queryResult.Items)
495505
{
496-
Matches.Add(doc);
506+
Document doc = SourceTable.FromAttributeMap(item);
507+
ret.Add(doc);
508+
if (CollectResults)
509+
{
510+
Matches.Add(doc);
511+
}
497512
}
498513
}
499514
NextKey = queryResult.LastEvaluatedKey;
@@ -554,20 +569,25 @@ private static void SplitQueryFilter(Filter filter, Table targetTable, string in
554569
QueryFilter queryFilter = filter as QueryFilter;
555570
if (queryFilter == null) throw new InvalidOperationException("Filter is not of type QueryFilter");
556571

557-
keyConditions = new Dictionary<string, Condition>();
558-
filterConditions = new Dictionary<string, Condition>();
559-
560572
var conditions = filter.ToConditions(targetTable);
561-
foreach (var kvp in conditions)
562-
{
563-
string attributeName = kvp.Key;
564-
Condition condition = kvp.Value;
573+
keyConditions = null;
574+
filterConditions = null;
565575

566-
// depending on whether the attribute is key, place either in keyConditions or filterConditions
567-
if (IsKeyAttribute(targetTable, indexName, attributeName))
568-
keyConditions[attributeName] = condition;
569-
else
570-
filterConditions[attributeName] = condition;
576+
if (conditions.Count > 0)
577+
{
578+
keyConditions = new Dictionary<string, Condition>();
579+
filterConditions = new Dictionary<string, Condition>();
580+
foreach (var kvp in conditions)
581+
{
582+
string attributeName = kvp.Key;
583+
Condition condition = kvp.Value;
584+
585+
// depending on whether the attribute is key, place either in keyConditions or filterConditions
586+
if (IsKeyAttribute(targetTable, indexName, attributeName))
587+
keyConditions[attributeName] = condition;
588+
else
589+
filterConditions[attributeName] = condition;
590+
}
571591
}
572592
}
573593

sdk/test/Services/DynamoDBv2/IntegrationTests/DocumentTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public void TestTableOperations()
8080

8181
// Test that attributes stored as Datetimes can be retrieved in UTC.
8282
TestAsDateTimeUtc(numericHashRangeTable);
83+
84+
// Test Count on Query
85+
TestSelectCountOnQuery(hashTable);
8386
}
8487
}
8588

@@ -1824,6 +1827,30 @@ private void TestExpressionUpdate(ITable hashTable)
18241827
hashTable.DeleteItem(doc);
18251828
}
18261829

1830+
private void TestSelectCountOnQuery(ITable hashTable)
1831+
{
1832+
Document doc = new Document();
1833+
doc["Id"] = 1;
1834+
doc["Data"] = Guid.NewGuid().ToString();
1835+
1836+
hashTable.PutItem(doc);
1837+
1838+
var expression = new Expression
1839+
{
1840+
ExpressionStatement = "Id = :id",
1841+
ExpressionAttributeValues = { [":id"] = 1 }
1842+
};
1843+
1844+
var search = hashTable.Query(new QueryOperationConfig
1845+
{
1846+
KeyExpression = expression,
1847+
Select = SelectValues.Count
1848+
});
1849+
1850+
var docs = search.GetRemaining();
1851+
Assert.AreEqual(1, search.Count);
1852+
Assert.AreEqual(0, docs.Count);
1853+
}
18271854

18281855
private bool AreValuesEqual(Document docA, Document docB, DynamoDBEntryConversion conversion = null)
18291856
{

0 commit comments

Comments
 (0)