-
Notifications
You must be signed in to change notification settings - Fork 951
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! |
|
Hi @anasatirbasa , I read through the PR, I have a few concerns I wanted to raise with the entire team before providing feedback. Will update you soon. |
|
Hi @anasatirbasa, thanks for the wait. I have reviewed this PR with the team and have a few points that we would ideally like to fix. I agree that introducing this separate annotation would solve the problem in an "opt in" non invasive way. Considerations:
If both
This creates unpredictable behavior. If this is the case, can we please add some tests that cover it, and throw an exception when both annotations (new and current) are applied to the same field?
In v1,
but also, misleadingly suggests using We will likely ask you to add more test coverage similar to other extensions test coverage, but I will provide more concrete about testing gaps in the near future after another review. Thanks, |
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.
Please refer to my comments on the PR.
|
|
||
| TableMetadata meta = context.tableMetadata(); | ||
| Set<String> allowedKeys = new HashSet<>(); | ||
|
|
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.
Please add a test that checks what happens if both annotations are added to the same field. If my assumption is correct, based on the extensions' load order you can get conflicting results. If I'm correct we want to prevent these annotations from being applied to the same field by throwing an exception:
-
Add a check to make sure this annotation is not applied to a field that already has the current
AutoGeneratedUuidExtensionsince they can conflict based on load order. -
Do the inverse logic for
AutoGeneratedUuidExtension#beforeWrite()
Hello, @RanVaknin! Thank you very much for your feedback! I am in progress with the changes. |

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
@DynamoDbUpdateBehaviorfor 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 likelastUpdatedKeythat 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
@DynamoDbAutoGeneratedKeyin thesoftware.amazon.awssdk.enhanced.dynamodb.extensions.annotationspackage.AutoGeneratedKeyExtension, which ensures attributes annotated with@DynamoDbAutoGeneratedKeyare populated with a UUID when absent. This usesUUID.randomUUID()under the hood.AutoGeneratedKeyTagas the annotation tag integration point.@DynamoDbUpdateBehaviorto support both stable one-time keys - works for secondary index keys only (WRITE_IF_NOT_EXISTS) and regenerating keys (WRITE_ALWAYS).Stringis 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 installsucceedsscripts/new-changescript and following the instructions. Commit the new file created by the script in.changes/next-releasewith your changes.License