Skip to content

Commit b312ffd

Browse files
dacullopJakenVeina
authored andcommitted
Validate legacy TransformSafeAsync factories before forwarding
The Optional overload taking TransformAsyncOptions forwarded its factory inside a lambda, so the inner overload's guard could never observe a null factory. Adding an eager guard makes the failure surface at call time. The source and errorHandler arguments pass through by reference and are already guarded by the inner overload, so they need no duplicate checks here.
1 parent 047f5f8 commit b312ffd

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2011-2026 Roland Pheasant. All rights reserved.
2+
// Roland Pheasant licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for full license information.
4+
5+
using System;
6+
using System.Threading.Tasks;
7+
8+
using DynamicData.Kernel;
9+
using DynamicData.Tests.Domain;
10+
11+
using FluentAssertions;
12+
13+
using Xunit;
14+
15+
namespace DynamicData.Tests.Cache;
16+
17+
public partial class TransformSafeAsyncFixture
18+
{
19+
/// <summary>Verifies that the options overload rejects a null factory without subscribing to the source.</summary>
20+
[Theory]
21+
[InlineData(false)]
22+
[InlineData(true)]
23+
public void OptionsOverload_NullFactory_ThrowsBeforeSubscription(bool populateSource)
24+
{
25+
// Arrange
26+
using var source = new SourceCache<Person, string>(static person => person.Key);
27+
28+
if (populateSource)
29+
{
30+
source.AddOrUpdate(new Person("Person", 20));
31+
}
32+
33+
Func<Person, Optional<Person>, string, Task<Person>>? transformFactory = null;
34+
35+
// Act
36+
Action action = () => source.Connect().TransformSafeAsync(transformFactory!, static _ => { }, TransformAsyncOptions.Default);
37+
38+
// Assert
39+
action.Should().Throw<ArgumentNullException>(because: "invalid factories must be rejected before processing any items")
40+
.WithParameterName(nameof(transformFactory));
41+
}
42+
}

‎src/DynamicData.Tests/Cache/TransformSafeAsyncFixture.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reactive;
@@ -13,7 +13,7 @@
1313

1414
namespace DynamicData.Tests.Cache;
1515

16-
public class TransformSafeAsyncFixture
16+
public partial class TransformSafeAsyncFixture
1717
{
1818
[Fact]
1919
public void ReTransformAll()

‎src/DynamicData/Cache/ObservableCacheEx.TransformSafeAsync.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ public static IObservable<IChangeSet<TDestination, TKey>> TransformSafeAsync<TDe
134134
where TSource : notnull
135135
where TKey : notnull
136136
{
137+
transformFactory.ThrowArgumentNullExceptionIfNull(nameof(transformFactory));
138+
137139
return source.TransformSafeAsync((current, previous, key, cancel) => transformFactory(current, previous, key), errorHandler, options);
138140
}
139141

0 commit comments

Comments
 (0)
⌂ 换网址