Skip to content

Commit f558b40

Browse files
dscpinheiroboblodgett
authored andcommitted
fix: Update DocumentModel to handle expressions without variables
1 parent 8bf9e5d commit f558b40

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
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+
"Fix regression where an `Expression` could not be used without `ExpressionAttributeValues` (https://github.com/aws/aws-sdk-net/issues/3802)"
8+
]
9+
}
10+
]
11+
}

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -218,27 +218,29 @@ internal static void ApplyExpression(QueryRequest request, Table table,
218218
internal static Dictionary<string, AttributeValue> ConvertToAttributeValues(
219219
Dictionary<string, DynamoDBEntry> valueMap, Table table)
220220
{
221+
if (valueMap == null || valueMap.Count == 0)
222+
{
223+
return null;
224+
}
225+
221226
var convertedValues = new Dictionary<string, AttributeValue>();
222-
if (valueMap != null)
227+
foreach (var kvp in valueMap)
223228
{
224-
foreach (var kvp in valueMap)
229+
var attributeName = kvp.Key;
230+
var entry = kvp.Value;
231+
232+
if (entry == null)
233+
convertedValues[attributeName] = new AttributeValue { NULL = true };
234+
else
225235
{
226-
var attributeName = kvp.Key;
227-
var entry = kvp.Value;
228-
229-
if (entry == null)
230-
convertedValues[attributeName] = new AttributeValue { NULL = true };
231-
else
232-
{
233-
if (table.StoreAsEpoch.Contains(attributeName))
234-
entry = Document.DateTimeToEpochSeconds(entry, attributeName);
235-
236-
if (table.StoreAsEpochLong.Contains(attributeName))
237-
entry = Document.DateTimeToEpochSecondsLong(entry, attributeName);
238-
239-
var attributeConversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
240-
convertedValues[attributeName] = entry.ConvertToAttributeValue(attributeConversionConfig);
241-
}
236+
if (table.StoreAsEpoch.Contains(attributeName))
237+
entry = Document.DateTimeToEpochSeconds(entry, attributeName);
238+
239+
if (table.StoreAsEpochLong.Contains(attributeName))
240+
entry = Document.DateTimeToEpochSecondsLong(entry, attributeName);
241+
242+
var attributeConversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
243+
convertedValues[attributeName] = entry.ConvertToAttributeValue(attributeConversionConfig);
242244
}
243245
}
244246

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public void TestTableOperations()
5858

5959
// Test expressions for put
6060
TestExpressionPut(hashTable);
61+
TestExpressionPutWithoutValues(hashTable);
6162

6263
// Test expressions for delete
6364
TestExpressionsOnDelete(hashTable);
@@ -137,6 +138,7 @@ public void TestTableOperationsViaBuilder()
137138

138139
// Test expressions for put
139140
TestExpressionPut(hashTable);
141+
TestExpressionPutWithoutValues(hashTable);
140142

141143
// Test expressions for delete
142144
TestExpressionsOnDelete(hashTable);
@@ -1755,6 +1757,27 @@ private void TestExpressionPut(ITable hashTable)
17551757
hashTable.DeleteItem(doc);
17561758
}
17571759

1760+
private void TestExpressionPutWithoutValues(ITable hashTable)
1761+
{
1762+
var doc = new Document
1763+
{
1764+
["Id"] = DateTime.UtcNow.Ticks
1765+
};
1766+
1767+
var expression = new Expression
1768+
{
1769+
ExpressionStatement = "attribute_not_exists(Id)"
1770+
};
1771+
1772+
var config = new PutItemOperationConfig
1773+
{
1774+
ConditionalExpression = expression
1775+
};
1776+
1777+
Assert.IsTrue(hashTable.TryPutItem(doc, config));
1778+
hashTable.DeleteItem(doc);
1779+
}
1780+
17581781
private void TestExpressionUpdate(ITable hashTable)
17591782
{
17601783
Document doc = new Document();

0 commit comments

Comments
 (0)