diff --git a/generator/.DevConfigs/FD891AA9-310E-4F80-B97A-00C8B7B97BA3.json b/generator/.DevConfigs/FD891AA9-310E-4F80-B97A-00C8B7B97BA3.json new file mode 100644 index 000000000000..aceb0da8010b --- /dev/null +++ b/generator/.DevConfigs/FD891AA9-310E-4F80-B97A-00C8B7B97BA3.json @@ -0,0 +1,11 @@ +{ + "services": [ + { + "serviceName": "DynamoDBv2", + "type": "patch", + "changeLogMessages": [ + "Fixed trim warnings in Object Persistence high level library introduced by PR adding polymorphic support." + ] + } + ] +} \ No newline at end of file diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs index 78f3b4667224..806354cc9a68 100644 --- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs @@ -538,13 +538,13 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB Document document = entry as Document; if (document != null) { - if (TryFromMap(targetType, document, flatConfig, propertyStorage.DerivedTypeKeysDictionary, out output)) + if (TryFromMap(targetType, document, flatConfig, propertyStorage, out output)) return output; var typeAttributeName = flatConfig.DerivedTypeAttributeName;//"$type"; var derivedType = document.ContainsKey(typeAttributeName) ? document[typeAttributeName].AsString() : null; - if (derivedType != null && propertyStorage.DerivedTypeKeysDictionary.TryGetValue(derivedType, out var value)) + if (derivedType != null && propertyStorage.TryGetDerivedType(derivedType, out var value)) { targetType = value; } @@ -554,7 +554,7 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB DynamoDBList list = entry as DynamoDBList; if (list != null && - TryFromList(targetType, list, flatConfig, propertyStorage.DerivedTypeKeysDictionary, out output)) + TryFromList(targetType, list, flatConfig, propertyStorage, out output)) { return output; } @@ -565,17 +565,17 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB } } private bool TryFromList([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig, - Dictionary derivedTypeKeysDictionary, out object output) + SimplePropertyStorage parentPropertyStorage, out object output) { return targetType.IsArray ? - TryFromListToArray(targetType, list, flatConfig, derivedTypeKeysDictionary, out output) : //targetType is Array - TryFromListToIList(targetType, list, flatConfig, derivedTypeKeysDictionary, out output) ; //targetType is IList or has Add method. + TryFromListToArray(targetType, list, flatConfig, parentPropertyStorage, out output) : //targetType is Array + TryFromListToIList(targetType, list, flatConfig, parentPropertyStorage, out output) ; //targetType is IList or has Add method. } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062", Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")] - private bool TryFromListToIList([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig,Dictionary derivedTypeKeysDictionary, out object output) + private bool TryFromListToIList([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig, SimplePropertyStorage parentPropertyStorage, out object output) { if ((!Utils.ImplementsInterface(targetType, typeof(ICollection<>)) && !Utils.ImplementsInterface(targetType, typeof(IList))) || @@ -589,7 +589,7 @@ private bool TryFromListToIList([DynamicallyAccessedMembers(InternalConstants.Da var collection = Utils.Instantiate(targetType); IList ilist = collection as IList; bool useIListInterface = ilist != null; - var propertyStorage = new SimplePropertyStorage(elementType, derivedTypeKeysDictionary); + var propertyStorage = new SimplePropertyStorage(elementType, parentPropertyStorage); MethodInfo collectionAdd = null; if (!useIListInterface) @@ -611,7 +611,7 @@ private bool TryFromListToIList([DynamicallyAccessedMembers(InternalConstants.Da return true; } - private bool TryFromListToArray([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig, Dictionary derivedTypeKeysDictionary, out object output) + private bool TryFromListToArray([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig, SimplePropertyStorage parentPropertyStorage, out object output) { if (!Utils.CanInstantiateArray(targetType)) { @@ -621,7 +621,7 @@ private bool TryFromListToArray([DynamicallyAccessedMembers(InternalConstants.Da var elementType = Utils.GetElementType(targetType); var array = (Array)Utils.InstantiateArray(targetType,list.Entries.Count); - var propertyStorage = new SimplePropertyStorage(elementType, derivedTypeKeysDictionary); + var propertyStorage = new SimplePropertyStorage(elementType, parentPropertyStorage); for (int i = 0; i < list.Entries.Count; i++) { @@ -636,7 +636,7 @@ private bool TryFromListToArray([DynamicallyAccessedMembers(InternalConstants.Da [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067", Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")] - private bool TryFromMap([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, Document map, DynamoDBFlatConfig flatConfig, Dictionary derivedTypeKeysDictionary, out object output) + private bool TryFromMap([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, Document map, DynamoDBFlatConfig flatConfig, SimplePropertyStorage parentPropertyStorage, out object output) { output = null; @@ -649,7 +649,7 @@ private bool TryFromMap([DynamicallyAccessedMembers(InternalConstants.DataModelM var dictionary = Utils.Instantiate(targetType); var idictionary = dictionary as IDictionary; - var propertyStorage = new SimplePropertyStorage(valueType, derivedTypeKeysDictionary); + var propertyStorage = new SimplePropertyStorage(valueType, parentPropertyStorage); foreach (var kvp in map) { @@ -668,6 +668,9 @@ internal DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, ob { return ToDynamoDBEntry(propertyStorage, value, flatConfig, canReturnScalarInsteadOfList: false); } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2072", + Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")] private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, object value, DynamoDBFlatConfig flatConfig, bool canReturnScalarInsteadOfList) { if (value == null) @@ -685,9 +688,9 @@ private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, obj Type type; string typeDiscriminator = null; - if (propertyStorage.DerivedTypesDictionary.ContainsKey(value.GetType())) + if (propertyStorage.TryGetDerivedTypeDiscriminator(value.GetType(), out var td)) { - typeDiscriminator = propertyStorage.DerivedTypesDictionary[value.GetType()]; + typeDiscriminator = td; type = value.GetType(); } else @@ -707,11 +710,11 @@ private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, obj else { Document map; - if (TryToMap(value, type, flatConfig, propertyStorage.DerivedTypesDictionary, out map)) + if (TryToMap(value, type, flatConfig, propertyStorage, out map)) return map; DynamoDBList list; - if (TryToList(value, type, flatConfig, propertyStorage.DerivedTypesDictionary, out list)) + if (TryToList(value, type, flatConfig, propertyStorage, out list)) return list; return SerializeToDocument(value, type, flatConfig, typeDiscriminator); @@ -721,7 +724,7 @@ private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, obj [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067", Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")] private bool TryToMap(object value, [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type type, DynamoDBFlatConfig flatConfig, - Dictionary derivedTypesDictionary, out Document output) + SimplePropertyStorage parentPropertyStorage, out Document output) { output = null; @@ -734,7 +737,7 @@ private bool TryToMap(object value, [DynamicallyAccessedMembers(InternalConstant return false; output = new Document(); - SimplePropertyStorage propertyStorage = new SimplePropertyStorage(valueType,derivedTypesDictionary); + SimplePropertyStorage propertyStorage = new SimplePropertyStorage(valueType, parentPropertyStorage); foreach (object keyValue in idictionary.Keys) { @@ -755,7 +758,7 @@ private bool TryToMap(object value, [DynamicallyAccessedMembers(InternalConstant } private bool TryToList(object value, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] Type type, DynamoDBFlatConfig flatConfig, - Dictionary derivedTypesDictionary, out DynamoDBList output) + SimplePropertyStorage parentPropertyStoragey, out DynamoDBList output) { if (!Utils.ImplementsInterface(type, typeof(ICollection<>))) { @@ -774,7 +777,7 @@ private bool TryToList(object value, [DynamicallyAccessedMembers(DynamicallyAcce Type elementType = Utils.GetElementType(type); - SimplePropertyStorage propertyStorage = new SimplePropertyStorage(elementType,derivedTypesDictionary); + SimplePropertyStorage propertyStorage = new SimplePropertyStorage(elementType, parentPropertyStoragey); output = new DynamoDBList(); foreach (var item in enumerable) { diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/InternalModel.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/InternalModel.cs index 0cfc7965f16c..eb1085ff9570 100644 --- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/InternalModel.cs +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/InternalModel.cs @@ -57,17 +57,39 @@ internal class SimplePropertyStorage // Converter, if one is present public IPropertyConverter Converter { get; protected set; } - public void AddDerivedType(string typeDiscriminator, Type type) + public void AddDerivedType(string typeDiscriminator, [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type type) { - DerivedTypesDictionary[type] = typeDiscriminator; - DerivedTypeKeysDictionary[typeDiscriminator] = type; + _derivedTypesDictionary[type] = typeDiscriminator; + _derivedTypeKeysDictionary[typeDiscriminator] = type; } // derived type information used for polymorphic serialization - public Dictionary DerivedTypesDictionary { get; private set; } + private Dictionary _derivedTypesDictionary; // derived type information used for polymorphic deserialization - public Dictionary DerivedTypeKeysDictionary { get; private set; } + private Dictionary _derivedTypeKeysDictionary; + + public bool TryGetDerivedTypeDiscriminator([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type type, out string typeDiscriminator) + { + if (_derivedTypesDictionary.TryGetValue(type, out typeDiscriminator)) + { + return true; + } + + return false; + } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067", + Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")] + public bool TryGetDerivedType(string typeDiscriminator, [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] out Type deriviedType) + { + if (_derivedTypeKeysDictionary.TryGetValue(typeDiscriminator, out deriviedType)) + { + return true; + } + + return false; + } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2072", Justification = "The user's type has been annotated with DynamicallyAccessedMemberTypes.All with the public API into the library. At this point the type will not be trimmed.")] @@ -81,18 +103,15 @@ internal SimplePropertyStorage(MemberInfo member) internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType) { MemberType = memberType; - DerivedTypesDictionary = new Dictionary(); - DerivedTypeKeysDictionary = new Dictionary(); + _derivedTypesDictionary = new Dictionary(); + _derivedTypeKeysDictionary = new Dictionary(); } - internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType, Dictionary derivedTypesDictionary) - { - MemberType = memberType; - DerivedTypesDictionary = derivedTypesDictionary; - } - internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType, Dictionary derivedTypeKeysDictionary) + + internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType, SimplePropertyStorage parentProeprtyStorage) { MemberType = memberType; - DerivedTypeKeysDictionary = derivedTypeKeysDictionary; + _derivedTypesDictionary = parentProeprtyStorage._derivedTypesDictionary; + _derivedTypeKeysDictionary = parentProeprtyStorage._derivedTypeKeysDictionary; } public override string ToString()