Skip to content

Commit

Permalink
Fix the added support for proper initialization of extensions when de…
Browse files Browse the repository at this point in the history
…serializing data created without the extensions.
  • Loading branch information
gmcelhanon committed Nov 13, 2024
1 parent 65b8af5 commit c767732
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>();

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<string>(aggregateExtensionByName.Keys);

var extensionDictionary = new Dictionary<string, object>();
IList<string> missingExtensionEntries;

int count = reader.ReadMapHeader();
if (GeneratedArtifactStaticDependencies.EntityExtensionRegistrar.AggregateExtensionEntityNamesByType.TryGetValue(_containingType, out var aggregateExtensionByName))
{
missingExtensionEntries = new List<string>(aggregateExtensionByName.Keys);
}
else
{
missingExtensionEntries = Array.Empty<string>();
}

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);
Expand All @@ -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<object>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public IDictionary Deserialize(ref MessagePackReader reader, MessagePackSerializ
// Wrap the standard collection in a GenericPersistentSet<T> compatible with NHibernate
if (reader.TryReadNil())
{
extensionDictionary[matchingExtension] = CreatePersistentCollection(null);
extensionDictionary[matchingExtension] = CreatePersistentCollection();
continue;
}

Expand All @@ -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<object>();

Expand Down

0 comments on commit c767732

Please sign in to comment.