Skip to content

[Bug]: ReferenceEquals breaks value types #931

Description

@erri120

Describe the bug 🐞

DynamicData has many instances of using ReferenceEquals which breaks observable streams silently for value types.

The issue I ran into specifically was using MergeManyChangeSets for IChangeSet<TValue, TKey> where both the value and key are value types. When an item upstream is getting removed, the item was never removed from the cache because the equality check uses ReferenceEquals:

private bool CheckEquality(TObject left, TObject right) =>
ReferenceEquals(left, right) || (equalityComparer?.Equals(left, right) ?? false);

The temporary workaround is to add a custom equality comparer, but a proper solution should use EqualityComparer<T>.Default or the custom equality comparer instead of ReferenceEquals.

Step to reproduce

var source1 = new SourceCache<int, int>(x => x);
var source2 = new SourceCache<bool, int>(_ => throw new NotSupportedException());

source1.Connect().MergeManyChangeSets(_ => source2.Connect()).Do(changeSet =>
{
    foreach (var change in changeSet)
    {
        Console.WriteLine($"{change.Reason}: {change.Current}");
    }
}).Subscribe();

source1.Edit(updater => updater.AddOrUpdate(1, key: 1));

// produces console log
source2.Edit(updater => updater.AddOrUpdate(false, key: 1));

// doesn't produce console log
source2.Edit(updater => updater.Remove(key: 1));

Expected behavior

Value types shouldn't break DynamicData silently or loudly with exceptions, as seen in #925.

DynamicData Version

9.0.4

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions