aws / aws/aws-sdk-java-v2

DynamoDbTable.updateItem clears data from table, then only sets non-null field values

Open
#4,711 3 comments 0 reactions 1 assignee Claimed by @debora-ito View on GitHub
bug dynamodb-enhanced p3
Dominant language
Java
Stars
2.6k
Forks
1k
Avg merge
2d 9h
Merged PRs (30d)
51

Description

### Describe the bug

When executing `DynamoDbTable.updateItem` for a few fields, the execution clear the existing data and only sets new field values from input object.
My understanding, for an existing record, when a Java object populated with primary key and fields to be changed, this operation should affect only the fields to be changed for the primary key record.
Curious to know, how is this `updateItem` supposed to work?

### Expected Behavior

- The `updateItem` should only change the fields which are present in the input
- This begs a question what should happen to the default values which are assigned as `null`. There is a field level annotation for ignoring null (I tried, it didn't work for me either). Also, need a class level annotation for ignoring **all null** fields.

### Current Behavior

Overwriting existing contents of a record for `updateItem` operation.

I am using `spring-cloud-aws-starter-dynamodb` using `DynamoDbTemplate` [AWS Spring DynamoDb](https://docs.awspring.io/spring-cloud-aws/docs/3.0.3/reference/html/index.html#dynamodboperations), however, it fails when using below code snippet

```java

/*
Existing record:
Car(vin=1234567890, color=white, year=2020, model=Ford Mustang)
*/

final Car car = Car
.builder()
.vin("1234567890")
.color("black") // existing record has white color
.build();

Car updatedCar = dynamoDbEnhancedClient.table("Car", TableSchema.fromBean(Car.class))
.updateItem(car);

assertEquals("black", updatedCar.getColor()); // PASS
assertEquals("2020", updatedCar.getYear()); // FAIL
assertEquals("Ford Mustang", updatedCar.getModel()); // FAIL
```

### Reproduction Steps

1. Create a `Car` table with fields vin (primary key), year, model, color
2. Create Java class named as Car
3. Add class level annotation `@DynamoDbBean`
4. Add `@DynamoDbPartitionKey` annotation to a getter method of vin
5. Add a new record in Car table as mentioned in **Current Behavior**
6. Create an instance of `dynamoDbEnhancedClient`
7. Execute code snippet as mentioned in **Current Behavior**

### Possible Solution

- updateItem should only affect the values in the input
- This begs a question what should happen to the default values which are assigned as `null`. There is a field level annotation for ignoring null (it didn't work for me either). Also, need a class level annotation for ignoring **all null** fields.

### Additional Information/Context

- Springboot 3.1.5
- Java 21
- AWS SDK 2.20.153
- spring-cloud-aws-starter-dynamodb 3.0.2

### AWS Java SDK version used

2.20.153

### JDK version used

21

### Operating System and version

Windows 11 Pro x64

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.