Skip to content
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

DynamoDBDocumentClient does not unmarshall attributes on ConditionalCheckFailedException #6723

Closed
3 of 4 tasks
brianle1301 opened this issue Dec 11, 2024 · 2 comments
Closed
3 of 4 tasks
Assignees
Labels
bug This issue is a bug. p2 This is a standard priority issue

Comments

@brianle1301
Copy link

brianle1301 commented Dec 11, 2024

Checkboxes for prior research

Describe the bug

When using the document client and setting ReturnValuesOnConditionCheckFailure: 'ALL_OLD', item attributes returned with ConditionalCheckFailedException are not unmarshalled

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/[email protected]

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v22.3.0

Reproduction Steps

Run the snippet below against a record that violates the condition expression, using your own values and attribute names if needed.

const client = DynamoDBDocumentClient.from(new DynamoDBClient());
try {
    const result = client.send(
        new UpdateCommand({
            TableName: 'MyTable',
            Key: { pk: 'pk', sk: 'sk' },
            UpdateExpression: 'SET my_attribute = :attr',
            ConditionExpression: 'attribute_exists(pk) AND owner_id = :ownerId',
            ExpressionAttributeValues: { ':attr': 'test', ':ownerId': 'ownerId' },
            ReturnValues: 'ALL_NEW',
            ReturnValuesOnConditionCheckFailure: 'ALL_OLD',
        }),
    );
} catch (error) {
    if (error instanceof ConditionalCheckFailedException) {
        console.log(error.Item); // error.Item is unmarshalled
    }
}

Observed Behavior

The record item returned along with the error is not unmarshalled

Expected Behavior

The record item returned along with the error should be unmarshalled

Possible Solution

Catch the error, unmarshall the item and rethrow

Additional Information/Context

No response

@brianle1301 brianle1301 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 11, 2024
@zshzbh zshzbh self-assigned this Dec 13, 2024
@RanVaknin RanVaknin assigned RanVaknin and unassigned zshzbh Dec 17, 2024
@RanVaknin
Copy link
Contributor

Hi @brianle1301,

The SDK's behavior here is working as intended. The ReturnValuesOnConditionCheckFailure feature was specifically designed to return the raw item state at the time of failure - there's no automatic unmarshalling happening here, nor should there be.

Looking at the official announcement blog post (https://aws.amazon.com/blogs/database/handle-conditional-write-errors-in-high-concurrency-scenarios-with-amazon-dynamodb/), you can see that in the examples provided by the DDB team return the raw DynamoDB format. Check out their Python example:

Current Value: {'bacon': {'N': '4'}, 'lettuce': {'N': '0'}, ...}

The whole point of this feature is to avoid an extra GetItem call when a condition check fails - it's giving you the raw state so you can figure out why the condition failed. If you need the data unmarshalled, you'll have to handle that yourself by using the unmarshall functionality from import { unmarshall } from "@aws-sdk/util-dynamodb";

Hope this helps clear things up!

Cheers,
Ran~

@RanVaknin RanVaknin removed the needs-triage This issue or PR still needs to be triaged. label Dec 18, 2024
@RanVaknin RanVaknin closed this as not planned Won't fix, can't repro, duplicate, stale Dec 18, 2024
@zshzbh zshzbh added the p2 This is a standard priority issue label Dec 18, 2024
@brianle1301
Copy link
Author

@RanVaknin Hello,
Thanks for your reply.

I don't think there's a need for an extra GetItem call to get the item unmarshalled? I'd suppose the document client can catch the error, introspect it and if the Item key exists, unmarshall it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

3 participants