Skip to content

Commit

Permalink
Fixed cycle detection in AnyType and ObjectToDictionaryConverter (#7262)
Browse files Browse the repository at this point in the history
  • Loading branch information
glen-84 authored Aug 6, 2024
1 parent 90cb64b commit 4223f76
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 11 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/HotChocolate/Core/src/Types/Properties/TypeResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,7 @@ Type: `{0}`</value>
<data name="ResolverContextExtensions_IsSelected_FieldNameEmpty" xml:space="preserve">
<value>The field name mustn't be null, empty or consist only of white spaces.</value>
</data>
<data name="ObjectToDictionaryConverter_CycleInObjectGraph" xml:space="preserve">
<value>Cycle in object graph detected.</value>
</data>
</root>
14 changes: 12 additions & 2 deletions src/HotChocolate/Core/src/Types/Types/Scalars/AnyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public override IValueNode ParseValue(object? value)
{
return value is null
? NullValueNode.Default
: ParseValue(value, new HashSet<object>());
: ParseValue(value, new HashSet<object>(ReferenceEqualityComparer.Instance));
}

private IValueNode ParseValue(object? value, ISet<object> set)
Expand Down Expand Up @@ -158,6 +158,9 @@ private IValueNode ParseValue(object? value, ISet<object> set)
field.Key,
ParseValue(field.Value, set)));
}

set.Remove(value);

return new ObjectValueNode(fields);
}

Expand All @@ -168,10 +171,17 @@ private IValueNode ParseValue(object? value, ISet<object> set)
{
valueList.Add(ParseValue(element, set));
}

set.Remove(value);

return new ListValueNode(valueList);
}

return ParseValue(_objectToDictConverter.Convert(value), set);
var valueNode = ParseValue(_objectToDictConverter.Convert(value), set);

set.Remove(value);

return valueNode;
}

throw new SerializationException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Reflection;
using HotChocolate.Properties;

namespace HotChocolate.Utilities;

Expand All @@ -23,7 +24,7 @@ public object Convert(object obj)

object value = null;
void SetValue(object v) => value = v;
VisitValue(obj, SetValue, []);
VisitValue(obj, SetValue, new HashSet<object>(ReferenceEqualityComparer.Instance));
return value;
}

Expand Down Expand Up @@ -140,6 +141,13 @@ private void VisitObject(
VisitValue(value, SetField, processed);
}
}

processed.Remove(obj);
}
else
{
throw new GraphQLException(
TypeResources.ObjectToDictionaryConverter_CycleInObjectGraph);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#nullable enable

#if NETSTANDARD2_0
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace HotChocolate.Utilities;

/// <summary>
/// An <see cref="IEqualityComparer{Object}"/> that uses reference equality (<see cref="object.ReferenceEquals(object?, object?)"/>)
/// instead of value equality (<see cref="object.Equals(object?)"/>) when comparing two object instances.
/// </summary>
/// <remarks>
/// The <see cref="ReferenceEqualityComparer"/> type cannot be instantiated. Instead, use the <see cref="Instance"/> property
/// to access the singleton instance of this type.
/// </remarks>
internal sealed class ReferenceEqualityComparer : IEqualityComparer<object?>, IEqualityComparer
{
private ReferenceEqualityComparer() { }

/// <summary>
/// Gets the singleton <see cref="ReferenceEqualityComparer"/> instance.
/// </summary>
public static ReferenceEqualityComparer Instance { get; } = new ReferenceEqualityComparer();

/// <summary>
/// Determines whether two object references refer to the same object instance.
/// </summary>
/// <param name="x">The first object to compare.</param>
/// <param name="y">The second object to compare.</param>
/// <returns>
/// <see langword="true"/> if both <paramref name="x"/> and <paramref name="y"/> refer to the same object instance
/// or if both are <see langword="null"/>; otherwise, <see langword="false"/>.
/// </returns>
/// <remarks>
/// This API is a wrapper around <see cref="object.ReferenceEquals(object?, object?)"/>.
/// It is not necessarily equivalent to calling <see cref="object.Equals(object?, object?)"/>.
/// </remarks>
public new bool Equals(object? x, object? y) => ReferenceEquals(x, y);

/// <summary>
/// Returns a hash code for the specified object. The returned hash code is based on the object
/// identity, not on the contents of the object.
/// </summary>
/// <param name="obj">The object for which to retrieve the hash code.</param>
/// <returns>A hash code for the identity of <paramref name="obj"/>.</returns>
/// <remarks>
/// This API is a wrapper around <see cref="RuntimeHelpers.GetHashCode(object)"/>.
/// It is not necessarily equivalent to calling <see cref="object.GetHashCode()"/>.
/// </remarks>
public int GetHashCode(object? obj)
{
// Depending on target framework, RuntimeHelpers.GetHashCode might not be annotated
// with the proper nullability attribute. We'll suppress any warning that might
// result.
return RuntimeHelpers.GetHashCode(obj!);
}
}
#endif
Loading

0 comments on commit 4223f76

Please sign in to comment.