Skip to content

Commit

Permalink
Merge pull request #1215 from microsoft/mk/fix-cloning-stackoverflow-…
Browse files Browse the repository at this point in the history
…exception

Track cloned objects to avoid StackOverflowExceptions during cloning
  • Loading branch information
MaggieKimani1 authored Apr 12, 2023
2 parents 020790d + 178bb46 commit 2acc209
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi.Readers</Title>
<PackageId>Microsoft.OpenApi.Readers</PackageId>
<Version>1.6.4-preview2</Version>
<Version>1.6.4-preview3</Version>
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
24 changes: 18 additions & 6 deletions src/Microsoft.OpenApi/Helpers/DictionaryCloneHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.OpenApi.Helpers
/// <summary>
/// Helper class for deep cloning dictionaries.
/// </summary>
internal class DictionaryCloneHelper
internal static class DictionaryCloneHelper
{
/// <summary>
/// Deep clone key value pairs in a dictionary.
Expand All @@ -21,14 +21,26 @@ internal class DictionaryCloneHelper
internal static Dictionary<T, U> Clone<T, U>(IDictionary<T, U> dictionary)
{
if (dictionary is null) return null;

var clonedDictionary = new Dictionary<T, U>(dictionary.Keys.Count);
var clonedObjects = new Dictionary<object, object>();

foreach (var kvp in dictionary)
foreach (var keyValuePair in dictionary)
{
// Create instance of the specified type using the constructor matching the specified parameter types.
clonedDictionary[kvp.Key] = (U)Activator.CreateInstance(kvp.Value.GetType(), kvp.Value);
}

// If the object has already been cloned, use the cloned object instead of cloning it again
if (clonedObjects.TryGetValue(keyValuePair.Value, out var clonedValue))
{
clonedDictionary[keyValuePair.Key] = (U)clonedValue;
}
else
{
// Create instance of the specified type using the constructor matching the specified parameter types.
clonedDictionary[keyValuePair.Key] = (U)Activator.CreateInstance(keyValuePair.Value.GetType(), keyValuePair.Value);

// Add the cloned object to the dictionary of cloned objects
clonedObjects.Add(keyValuePair.Value, clonedDictionary[keyValuePair.Key]);
}
}

return clonedDictionary;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi</Title>
<PackageId>Microsoft.OpenApi</PackageId>
<Version>1.6.4-preview2</Version>
<Version>1.6.4-preview3</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down

0 comments on commit 2acc209

Please sign in to comment.