Skip to content
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

fix(DynamoDBv2): check for properties marked required when loading objects via DynamoDBContext #3277

Draft
wants to merge 1 commit into
base: main-staging
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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;

Expand Down