For a paginated response from Dynamo, I am attempting to preserve the ExclusiveStartKey value. In the code sample, if I use the response.LastEvaluatedKey value directly, subsequent requests work fine.
However, if I serialize the response.LastEvaluatedKey and then serialize it back to be used as the ExclusiveStartKey value, subsequent requests fail with the following error message:
The provided starting key is invalid: One or more parameter values were invalid: Null attribute value types must have the value of true
The deserialized dictionary appears to have the same values as the original dictionary...is there anything to check to see what is different between the two?
QueryResponse response = null;
do
{
string gsiPartitionKey = "gsi-pk-value-1";
var queryRequest = new QueryRequest()
{
TableName = "my-table",
IndexName = "my-index",
KeyConditionExpression = "IndexPk = :s_gsiPartitionKey",
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{
":s_gsiPartitionKey", new AttributeValue { S = gsiPartitionKey}
}
},
Limit = 1
};
if (response != null)
{
//OPTION 1 - OK - Using LastEvaluatedKey directly works fine
//queryRequest.ExclusiveStartKey = response.LastEvaluatedKey;
//OPTION 2 - BAD - Serializing and deserializing fails
var serialized = JsonConvert.SerializeObject(response.LastEvaluatedKey);
var deserialized = JsonConvert.DeserializeObject<Dictionary<string, AttributeValue>>(serialized);
queryRequest.ExclusiveStartKey = deserialized;
}
response = await DynamoDbClient.QueryAsync(queryRequest);
} while (response.LastEvaluatedKey.Count != 0);
I hit this issue today, and thought I'd update this. Inside
AttributeValueclass, there is a non public member_nullof typebool?that is being initialised incorrectly when de-serialising from JSON. It's being set asfalsewhen it should be set asnull.Using reflection, after deserialization, I set the value to
nullfor each key in the dictionary and AWS now returns data as expected.To access the private member I used this function:
And the method call was
SetPrivatePropertyValue<bool?>(attribute, "_null", null);Good luck!