Skip to content

DynamoDBAutoGeneratedTimestamp with DynamoDbUpdateBehavior(WRITE_IF_NOT_EXISTS) does not work on PutItem #6009

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

Open
1 task
ericloe-cash opened this issue Apr 7, 2025 · 0 comments
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@ericloe-cash
Copy link

Describe the bug

The v1 mapper would evaluate the in memory value of the field on the item before applying the auto generated time stamp.

So for example if you are using putItem with versioned updates to a previous item this would not modify the created at timestamp.

The enhanced client will alway overwrite the DynamoDBAutoGeneratedTimestamp regardless of whether it has a value in memory.

Regression Issue

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

Expected Behavior

var customer = Customer(id = 1, name = "John Smith")

// Put a new item
customerTable.putItem(customer);
customer = customerTable.getItem(1)
// version = 1, created_at = t0, updated_at = t0)
customer = customer.copy(email = "[email protected]")

// Put an updated item
customerTable.putItem(customer);
customer = customerTable.getItem(1)
// version = 2, created_at = t0, updated_at = t1)

Current Behavior

var customer = Customer(id = 1, name = "John Smith")

// Put a new item
customerTable.putItem(customer);
customer = customerTable.getItem(1)
// version = 1, created_at = t0, updated_at = t0)
customer = customer.copy(email = "[email protected]")

// Put an updated item
customerTable.putItem(customer);
customer = customerTable.getItem(1)
// version = 2, created_at = t1, updated_at = t1) <-- created_at is updated to the latest timestamp unconditionally.

Reproduction Steps

@DynamoDbBean
public class Customer {

    private String id;
    private String name;
    private String email;

    private Long version;
    private Instant created_at;
    private Instant updated_at;

    @DynamoDbPartitionKey
    public String getId() { return this.id; }

    public void setId(String id) { this.id = id; }

    @DynamoDbVersionAttribute
    public Long getVersion() { return this.version; }

    public void setVersion(Long version) { this.version = version; }

    @DynamoDbUpdateBehavior(value = UpdateBehavior.WRITE_IF_NOT_EXISTS)
    @DynamoDbAutoGeneratedTimestampAttribute
    public Instant getCreatedAt() { return this.create_at; }

    public void setCreatedAt(Instant time) { this.created_at = time }
    
    @DynamoDbUpdateBehavior(value = UpdateBehavior.WRITE_ALWAYS)
    @DynamoDbAutoGeneratedTimestampAttribute
    public Instant getUpdatedAt() { return this.updated_at; }

    public void setUpdatedAt(Instant time) { this.updated_at = time }

    public String getCustName() { return this.name; }

    public void setCustName(String name) { this.name = name; }

    public String getEmail() { return this.email; }

    public void setEmail(String email) { this.email = email; }

    public Instant getRegistrationDate() { return this.regDate; }

    public void setRegistrationDate(Instant registrationDate) { this.regDate = registrationDate; }
}

Possible Solution

Evaluate the update strategy on the put item call as well

Additional Information/Context

No response

AWS Java SDK version used

2.25.11

JDK version used

openjdk 17.0.12 2024-07-16

Operating System and version

MacOs 15.3.1

@ericloe-cash ericloe-cash added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 7, 2025
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. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

1 participant