diff --git a/Application/EdFi.Ods.Common/Serialization/AggregateExtensionsMessagePackFormatter.cs b/Application/EdFi.Ods.Common/Serialization/AggregateExtensionsMessagePackFormatter.cs index 07ba69ff3..c3f3365f4 100644 --- a/Application/EdFi.Ods.Common/Serialization/AggregateExtensionsMessagePackFormatter.cs +++ b/Application/EdFi.Ods.Common/Serialization/AggregateExtensionsMessagePackFormatter.cs @@ -77,24 +77,33 @@ public IDictionary Deserialize(ref MessagePackReader reader, MessagePackSerializ return null; } - if (!GeneratedArtifactStaticDependencies.EntityExtensionRegistrar.AggregateExtensionEntityNamesByType.TryGetValue(_containingType, out var aggregateExtensionByName)) + var extensionDictionary = new Dictionary(); + + int count = reader.ReadMapHeader(); + + if (count == 0) { - throw new Exception($"Unable to find aggregate extension by type '{_containingType.Name}'"); + return extensionDictionary; } - var missingExtensionEntries = new List(aggregateExtensionByName.Keys); - - var extensionDictionary = new Dictionary(); + IList missingExtensionEntries; - int count = reader.ReadMapHeader(); + if (GeneratedArtifactStaticDependencies.EntityExtensionRegistrar.AggregateExtensionEntityNamesByType.TryGetValue(_containingType, out var aggregateExtensionByName)) + { + missingExtensionEntries = new List(aggregateExtensionByName.Keys); + } + else + { + missingExtensionEntries = Array.Empty(); + } for (int i = 0; i < count; i++) { string extensionCollectionName = reader.ReadString(); - if (!aggregateExtensionByName.TryGetValue(extensionCollectionName, out var extensionEntity)) + if (aggregateExtensionByName?.TryGetValue(extensionCollectionName, out var extensionEntity) != true) { - throw new Exception($"Unable to find aggregate extension for '{_containingType.Name}' by name '{extensionCollectionName}'."); + throw new Exception($"Unable to find the deserialized aggregate extension '{extensionCollectionName}' on '{_containingType.Name}' in the current model."); } missingExtensionEntries.Remove(extensionCollectionName); @@ -119,9 +128,9 @@ public IDictionary Deserialize(ref MessagePackReader reader, MessagePackSerializ extensionDictionary.Add(extensionCollectionName, persistentList); } + // If there are any extensions that weren't included in the deserialized data, we need to initialize them properly if (missingExtensionEntries.Count > 0) { - // Initialize entries missing from serialized data, but needed for current API context foreach (string missingExtensionEntry in missingExtensionEntries) { extensionDictionary.Add(missingExtensionEntry, new DeserializedPersistentGenericBag( diff --git a/Application/EdFi.Ods.Common/Serialization/EntityExtensionsMessagePackFormatter.cs b/Application/EdFi.Ods.Common/Serialization/EntityExtensionsMessagePackFormatter.cs index 557f4326a..280ed401e 100644 --- a/Application/EdFi.Ods.Common/Serialization/EntityExtensionsMessagePackFormatter.cs +++ b/Application/EdFi.Ods.Common/Serialization/EntityExtensionsMessagePackFormatter.cs @@ -121,7 +121,7 @@ public IDictionary Deserialize(ref MessagePackReader reader, MessagePackSerializ // Wrap the standard collection in a GenericPersistentSet compatible with NHibernate if (reader.TryReadNil()) { - extensionDictionary[matchingExtension] = CreatePersistentCollection(null); + extensionDictionary[matchingExtension] = CreatePersistentCollection(); continue; } @@ -134,7 +134,7 @@ public IDictionary Deserialize(ref MessagePackReader reader, MessagePackSerializ return extensionDictionary; } - private static object CreatePersistentCollection(object extensionObject) + private static object CreatePersistentCollection(object extensionObject = null) { var list = new List();