diff --git a/Kyoo.Common/Utility/Merger.cs b/Kyoo.Common/Utility/Merger.cs index 3c2d6247..32441c1d 100644 --- a/Kyoo.Common/Utility/Merger.cs +++ b/Kyoo.Common/Utility/Merger.cs @@ -79,7 +79,16 @@ namespace Kyoo if (second == null) return first; foreach ((T key, T2 value) in second) - hasChanged |= first.TryAdd(key, value); + { + bool success = first.TryAdd(key, value); + hasChanged |= success; + + if (success || first[key]?.Equals(default) == false || value?.Equals(default) != false) + continue; + first[key] = value; + hasChanged = true; + } + return first; } @@ -123,7 +132,7 @@ namespace Kyoo hasChanged = false; if (second == null) return first; - hasChanged = second.Any(x => !x.Value.Equals(first[x.Key])); + hasChanged = second.Any(x => x.Value?.Equals(first[x.Key]) == false); foreach ((T key, T2 value) in first) second.TryAdd(key, value); return second; diff --git a/Kyoo.TheMovieDb/ProviderTmdb.cs b/Kyoo.TheMovieDb/ProviderTmdb.cs index 08bc36ea..b04d3f03 100644 --- a/Kyoo.TheMovieDb/ProviderTmdb.cs +++ b/Kyoo.TheMovieDb/ProviderTmdb.cs @@ -157,7 +157,7 @@ namespace Kyoo.TheMovieDb TMDbClient client = new(_apiKey.Value.ApiKey); return (await client.GetTvEpisodeAsync(id, episode.SeasonNumber.Value, episode.EpisodeNumber.Value)) - .ToEpisode(id, Provider); + ?.ToEpisode(id, Provider); } /// diff --git a/tests/Kyoo.Tests/Utility/MergerTests.cs b/tests/Kyoo.Tests/Utility/MergerTests.cs index 6aed3eb2..de9d3b04 100644 --- a/tests/Kyoo.Tests/Utility/MergerTests.cs +++ b/tests/Kyoo.Tests/Utility/MergerTests.cs @@ -455,5 +455,85 @@ namespace Kyoo.Tests.Utility Merger.Merge(first, second); // This should no call the setter of first so the test should pass. } + + [Fact] + public void MergeDictionaryNullValue() + { + Dictionary first = new() + { + [Images.Logo] = "logo", + [Images.Poster] = null + }; + Dictionary second = new() + { + [Images.Poster] = "new-poster", + [Images.Thumbnail] = "thumbnails" + }; + IDictionary ret = Merger.MergeDictionaries(first, second, out bool changed); + Assert.True(changed); + Assert.Equal(3, ret.Count); + Assert.Equal("new-poster", ret[Images.Poster]); + Assert.Equal("thumbnails", ret[Images.Thumbnail]); + Assert.Equal("logo", ret[Images.Logo]); + } + + [Fact] + public void MergeDictionaryNullValueNoChange() + { + Dictionary first = new() + { + [Images.Logo] = "logo", + [Images.Poster] = null + }; + Dictionary second = new() + { + [Images.Poster] = null, + }; + IDictionary ret = Merger.MergeDictionaries(first, second, out bool changed); + Assert.False(changed); + Assert.Equal(2, ret.Count); + Assert.Null(ret[Images.Poster]); + Assert.Equal("logo", ret[Images.Logo]); + } + + [Fact] + public void CompleteDictionaryNullValue() + { + Dictionary first = new() + { + [Images.Logo] = "logo", + [Images.Poster] = null + }; + Dictionary second = new() + { + [Images.Poster] = "new-poster", + [Images.Thumbnail] = "thumbnails" + }; + IDictionary ret = Merger.CompleteDictionaries(first, second, out bool changed); + Assert.True(changed); + Assert.Equal(3, ret.Count); + Assert.Equal("new-poster", ret[Images.Poster]); + Assert.Equal("thumbnails", ret[Images.Thumbnail]); + Assert.Equal("logo", ret[Images.Logo]); + } + + [Fact] + public void CompleteDictionaryNullValueNoChange() + { + Dictionary first = new() + { + [Images.Logo] = "logo", + [Images.Poster] = null + }; + Dictionary second = new() + { + [Images.Poster] = null, + }; + IDictionary ret = Merger.CompleteDictionaries(first, second, out bool changed); + Assert.False(changed); + Assert.Equal(2, ret.Count); + Assert.Null(ret[Images.Poster]); + Assert.Equal("logo", ret[Images.Logo]); + } } } \ No newline at end of file