Skip to content

Implement DynamoDbFlatten annotation #3833

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

Open
wants to merge 3 commits into
base: development
Choose a base branch
from

Conversation

irina-herciu
Copy link
Contributor

@irina-herciu irina-herciu commented May 21, 2025

Implement DynamoDbFlatten annotation

Description

Introduce the [DynamoDbFlatten] attribute, enabling developers to flatten complex nested types into their parent objects in DynamoDB, mirroring the behavior of Java SDK’s @DynamoDbFlattenDynamoDB Object Mapping. This allows for simpler, flat table schemas while maintaining clean object models in code.

Java DynamoDbFlatten

 [DynamoDBTable("HashTable")]
 public class EmployeeNonFlat
 {
     [DynamoDBHashKey("Id")]
     public int EmployeeId { get; set; }

     [DynamoDBFlatten]
     public ContactInfo Contact { get; set; }
 }

 public class ContactInfo
 {
     public string Email { get; set; }

     [DynamoDBFlatten]
     public Address Address { get; set; }
 }

Motivation and Context

Testing

Integration test added
Unit test to be added for internal methods

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed

License

  • I confirm that this pull request can be released under the Apache 2 license

add new attribute and update write item flow
fix flattened properties issue
@@ -181,6 +181,35 @@ public DynamoDBPolymorphicTypeAttribute(string typeDiscriminator,
}
}

/// <summary>
/// Indicates that the properties of the decorated field or property type should be "flattened"
/// into the top-level attributes of the DynamoDB item. When applied, all public properties
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they always flattened to "top-level attributes" or in the case of multi-nested structure the attributes are flattened to the parent structure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attributes are flattened to the parent structure

@@ -390,29 +390,53 @@ private void PopulateInstance(ItemStorage storage, object instance, DynamoDBFlat

using (flatConfig.State.Track(document))
{
foreach (PropertyStorage propertyStorage in storageConfig.AllPropertyStorage)
foreach (PropertyStorage propertyStorage in storageConfig.Properties)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the switch from AllPropertyStorage to Properties?

/// </para>
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class DynamoDBFlattenAttribute : DynamoDBAttribute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if a POCO has this attribute and the POCO is used in a collection property?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should only applies to single nested objects – not to collections, and this behavior is consistent with Java SDK. The only collection that will support flatten are Map<String, String>, but this is a new annotation @DynamoDbFlatten that is proposed to be added in the Java SDK.

@normj
Copy link
Member

normj commented May 21, 2025

Is there an ask for this feature or is this parity feature with the Java SDK?

@irina-herciu
Copy link
Contributor Author

Is there an ask for this feature or is this parity feature with the Java SDK?

this is for feature parity with Java SDK

@irina-herciu irina-herciu marked this pull request as ready for review May 22, 2025 15:25
@AlexDaines AlexDaines requested a review from normj May 22, 2025 15:26
@peterrsongg peterrsongg requested a review from GarrettBeatty May 22, 2025 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants