-
Notifications
You must be signed in to change notification settings - Fork 997
Support AutoGeneratedTimestamp and UpdateBehavior annotation in nested objects #6109
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
Merged
L-Applin
merged 1 commit into
aws:master
from
roamariei:feature/support-autoGeneratedTimestamp-and-updateBehavior-annotations-in-nested-objects
Sep 5, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changes/next-release/feature-AmazonDynamoDBEnhancedClient-96aff9e.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "feature", | ||
| "category": "Amazon DynamoDB Enhanced Client", | ||
| "contributor": "", | ||
| "description": "Added support for @DynamoDbAutoGeneratedTimestampAttribute and @DynamoDbUpdateBehavior on attributes within nested objects. The @DynamoDbUpdateBehavior annotation will only take effect for nested attributes when using IgnoreNullsMode.SCALAR_ONLY." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...ftware/amazon/awssdk/enhanced/dynamodb/internal/extensions/utility/NestedRecordUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.enhanced.dynamodb.internal.extensions.utility; | ||
|
|
||
| import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.getNestedSchema; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.internal.operations.UpdateItemOperation.NESTED_OBJECT_UPDATE; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.regex.Pattern; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.enhanced.dynamodb.TableSchema; | ||
| import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
|
||
| @SdkInternalApi | ||
| public final class NestedRecordUtils { | ||
|
|
||
| private static final Pattern NESTED_OBJECT_PATTERN = Pattern.compile(NESTED_OBJECT_UPDATE); | ||
|
|
||
| private NestedRecordUtils() { | ||
| } | ||
|
|
||
| /** | ||
| * Resolves and returns the {@link TableSchema} for the element type of a list attribute from the provided root schema. | ||
| * <p> | ||
| * This method is useful when dealing with lists of nested objects in a DynamoDB-enhanced table schema, | ||
| * particularly in scenarios where the list is part of a flattened nested structure. | ||
| * <p> | ||
| * If the provided key contains the nested object delimiter (e.g., {@code _NESTED_ATTR_UPDATE_}), the method traverses | ||
| * the nested hierarchy based on that path to locate the correct schema for the target attribute. | ||
| * Otherwise, it directly resolves the list element type from the root schema using reflection. | ||
| * | ||
| * @param rootSchema The root {@link TableSchema} representing the top-level entity. | ||
| * @param key The key representing the list attribute, either flat or nested (using a delimiter). | ||
| * @return The {@link TableSchema} representing the list element type of the specified attribute. | ||
| * @throws IllegalArgumentException If the list element class cannot be found via reflection. | ||
| */ | ||
| public static TableSchema<?> getTableSchemaForListElement(TableSchema<?> rootSchema, String key) { | ||
| TableSchema<?> listElementSchema; | ||
| try { | ||
| if (!key.contains(NESTED_OBJECT_UPDATE)) { | ||
| listElementSchema = TableSchema.fromClass( | ||
| Class.forName(rootSchema.converterForAttribute(key).type().rawClassParameters().get(0).rawClass().getName())); | ||
| } else { | ||
| String[] parts = NESTED_OBJECT_PATTERN.split(key); | ||
| TableSchema<?> currentSchema = rootSchema; | ||
|
|
||
| for (int i = 0; i < parts.length - 1; i++) { | ||
| Optional<? extends TableSchema<?>> nestedSchema = getNestedSchema(currentSchema, parts[i]); | ||
| if (nestedSchema.isPresent()) { | ||
| currentSchema = nestedSchema.get(); | ||
| } | ||
| } | ||
| String attributeName = parts[parts.length - 1]; | ||
| listElementSchema = TableSchema.fromClass( | ||
| Class.forName(currentSchema.converterForAttribute(attributeName) | ||
| .type().rawClassParameters().get(0).rawClass().getName())); | ||
| } | ||
| } catch (ClassNotFoundException e) { | ||
| throw new IllegalArgumentException("Class not found for field name: " + key, e); | ||
| } | ||
| return listElementSchema; | ||
| } | ||
|
|
||
| /** | ||
| * Traverses the attribute keys representing flattened nested structures and resolves the corresponding | ||
| * {@link TableSchema} for each nested path. | ||
| * <p> | ||
| * The method constructs a mapping between each unique nested path (represented as dot-delimited strings) | ||
| * and the corresponding {@link TableSchema} object derived from the root schema. It supports resolving schemas | ||
| * for arbitrarily deep nesting, using the {@code _NESTED_ATTR_UPDATE_} pattern as a path delimiter. | ||
| * <p> | ||
| * This is typically used in update or transformation flows where fields from nested objects are represented | ||
| * as flattened keys in the attribute map (e.g., {@code parent_NESTED_ATTR_UPDATE_child}). | ||
| * | ||
| * @param attributesToSet A map of flattened attribute keys to values, where keys may represent paths to nested attributes. | ||
| * @param rootSchema The root {@link TableSchema} of the top-level entity. | ||
| * @return A map where the key is the nested path (e.g., {@code "parent.child"}) and the value is the {@link TableSchema} | ||
| * corresponding to that level in the object hierarchy. | ||
| */ | ||
| public static Map<String, TableSchema<?>> resolveSchemasPerPath(Map<String, AttributeValue> attributesToSet, | ||
| TableSchema<?> rootSchema) { | ||
| Map<String, TableSchema<?>> schemaMap = new HashMap<>(); | ||
| schemaMap.put("", rootSchema); | ||
|
|
||
| for (String key : attributesToSet.keySet()) { | ||
| String[] parts = NESTED_OBJECT_PATTERN.split(key); | ||
|
|
||
| StringBuilder pathBuilder = new StringBuilder(); | ||
| TableSchema<?> currentSchema = rootSchema; | ||
|
|
||
| for (int i = 0; i < parts.length - 1; i++) { | ||
| if (pathBuilder.length() > 0) { | ||
| pathBuilder.append("."); | ||
| } | ||
| pathBuilder.append(parts[i]); | ||
|
|
||
| String path = pathBuilder.toString(); | ||
|
|
||
| if (!schemaMap.containsKey(path)) { | ||
| Optional<? extends TableSchema<?>> nestedSchema = getNestedSchema(currentSchema, parts[i]); | ||
| if (nestedSchema.isPresent()) { | ||
| schemaMap.put(path, nestedSchema.get()); | ||
| currentSchema = nestedSchema.get(); | ||
| } | ||
| } else { | ||
| currentSchema = schemaMap.get(path); | ||
| } | ||
| } | ||
| } | ||
| return schemaMap; | ||
| } | ||
|
|
||
| public static String reconstructCompositeKey(String path, String attributeName) { | ||
| if (path == null || path.isEmpty()) { | ||
| return attributeName; | ||
| } | ||
| return String.join(NESTED_OBJECT_UPDATE, path.split("\\.")) | ||
| + NESTED_OBJECT_UPDATE + attributeName; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.