Added support for DynamoDbAutoGeneratedKey annotation #6373
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Added the facility of using an annotation that will auto-generate a key for an attribute in a class,
similar to the legacy V1
@DynamoDBAutoGeneratedKey
, now ported and adapted for V2 with the Enhanced Client.Important Restriction
This annotation is only valid for primary keys (PK/SK) or secondary index keys (GSI/LSI PK/SK).
If applied to a non-key attribute, the extension will throw an
IllegalArgumentException
.If the attribute is not provided by the client, the SDK will automatically generate a value (UUID by default)
during serialization. If the attribute already has a value, it will be preserved depending on the update policy applied.
The implementation reuses the concept from V1 but leverages the V2 Enhanced Client extension model to achieve parity.
A key aspect of this feature is its combination with
@DynamoDbUpdateBehavior
. This determines whether a generated key is one-time only or is regenerated on every update:WRITE_ALWAYS
) → The attribute will be regenerated on every write if missing, even during updates. Useful for attributes likelastUpdatedKey
that are meant to refresh often.WRITE_IF_NOT_EXISTS
→ The attribute will only be generated once (on the first insert) and preserved across updates. This is the recommended option for stable identifiers likecreatedKey
.Motivation and Context
#5497
This functionality was present in V1 under
@DynamoDBAutoGeneratedKey
. Many users requested its reintroduction in V2.By introducing this annotation and the supporting extension, we align the Enhanced Client API with developer expectations and provide a familiar migration path.
Modifications
@DynamoDbAutoGeneratedKey
in thesoftware.amazon.awssdk.enhanced.dynamodb.extensions.annotations
package.AutoGeneratedKeyExtension
, which ensures attributes annotated with@DynamoDbAutoGeneratedKey
are populated with a UUID when absent. This usesUUID.randomUUID()
under the hood.AutoGeneratedKeyTag
as the annotation tag integration point.@DynamoDbUpdateBehavior
to support both stable one-time keys (WRITE_IF_NOT_EXISTS
) and regenerating keys (WRITE_ALWAYS
).String
is supported).AutoGeneratedKeyExtensionTest
) and functional integration tests (AutoGeneratedKeyRecordTest
) to verify correct behavior with real DynamoDB operations.and integration with
VersionedRecordExtension
.Testing
The changes have already been tested by running the existing tests and also added new unit/integration tests
for the new flow, ensuring parity with V1 behavior while also validating V2-specific integration points.
Test Coverage Checklist
Types of changes
Checklist
mvn install
succeedsscripts/new-change
script and following the instructions. Commit the new file created by the script in.changes/next-release
with your changes.License