-
Notifications
You must be signed in to change notification settings - Fork 940
Added support for DynamoDbAutoGeneratedKey annotation #6373
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
base: master
Are you sure you want to change the base?
Added support for DynamoDbAutoGeneratedKey annotation #6373
Conversation
519acfe
to
fa35bfc
Compare
586dbf5
to
1c0b19f
Compare
335e532
to
f3a3ad1
Compare
d407b73
to
e2b74e3
Compare
fb82197
to
ede74c8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No tests with @DynamoDbSortKey
...ava/software/amazon/awssdk/enhanced/dynamodb/functionaltests/AutoGeneratedKeyRecordTest.java
Outdated
Show resolved
Hide resolved
...ava/software/amazon/awssdk/enhanced/dynamodb/functionaltests/AutoGeneratedKeyRecordTest.java
Outdated
Show resolved
Hide resolved
...ava/software/amazon/awssdk/enhanced/dynamodb/functionaltests/AutoGeneratedKeyRecordTest.java
Show resolved
Hide resolved
...main/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedKeyExtension.java
Outdated
Show resolved
Hide resolved
Hello @marcusvoltolim, Thank you very much for your review. I have added tests for
Regarding your comment about I have updated the tests, ticket description, and PR description to clearly reflect this. Could you please take another look when you have a chance? Thank you! |
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
for secondary index keys only. 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 - works for secondary index keys only (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