From a3c891bf2067d2fc9a9e85ba9153a5c8bd73e383 Mon Sep 17 00:00:00 2001 From: Maximilian Stern Date: Tue, 9 Apr 2024 08:36:08 +0200 Subject: [PATCH] fix(DynamoDBv2): check for properties marked `required` when loading objects via DynamoDBContext --- .../DynamoDBv2/Custom/DataModel/ContextInternal.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs index d83c9e707fe9..c8c1a945b1e5 100644 --- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs @@ -19,6 +19,7 @@ using System.Linq; using System.IO; using System.Reflection; +using System.Runtime.CompilerServices; using Amazon.DynamoDBv2.DocumentModel; using Amazon.DynamoDBv2.Model; @@ -388,6 +389,11 @@ private void PopulateInstance(ItemStorage storage, object instance, DynamoDBFlat if (propertyStorage.IsVersion) storage.CurrentVersion = entry as Primitive; } + #if NET7_0_OR_GREATER + else if (Attribute.IsDefined(propertyStorage.Member, typeof(RequiredMemberAttribute))) { + throw new InvalidOperationException("Unable to retrieve value from " + attributeName); + } + #endif } } } @@ -785,6 +791,12 @@ private Document SerializeToDocument(object value, Type type, DynamoDBFlatConfig // Get/Set object properties private static bool TrySetValue(object instance, MemberInfo member, object value) { + #if NET7_0_OR_GREATER + if (Attribute.IsDefined(member, typeof(RequiredMemberAttribute)) && value == null) { + return false; + } + #endif + FieldInfo fieldInfo = member as FieldInfo; PropertyInfo propertyInfo = member as PropertyInfo;