Skip to content

Commit d0c75cd

Browse files
committed
Fix for Nullable partial OAPH properties
Nullable partial properties with ObservableAsProperty are not updated with null value
1 parent 5205db6 commit d0c75cd

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

src/ReactiveUI.SourceGenerator.Tests/OAPH/OAPFromObservableGeneratorTests.FromPartialProperty#TestNs.TestVM.ObservableAsPropertyFromObservable.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public partial class TestVM
2121
/// <inheritdoc cref="_testProperty"/>
2222
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2323
[global::System.Runtime.Serialization.DataMemberAttribute()]
24-
public partial double? TestProperty { get => _testProperty = _testPropertyHelper?.Value ?? _testProperty; }
24+
public partial double? TestProperty { get => _testProperty = (_testPropertyHelper == null ? _testProperty : _testPropertyHelper.Value); }
2525

2626
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
2727
protected void InitializeOAPH()

src/ReactiveUI.SourceGenerators.Execute/TestClassOAPH_VM.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public partial class TestClassOAPH_VM : ReactiveObject
2424
private string value = string.Empty;
2525
#pragma warning restore SX1309 // Field names should begin with underscore
2626

27+
[Reactive]
28+
private string? _testProperty;
29+
2730
/// <summary>
2831
/// Initializes a new instance of the <see cref="TestClassOAPH_VM"/> class.
2932
/// </summary>
@@ -34,6 +37,21 @@ public TestClassOAPH_VM()
3437

3538
_observableTestFieldHelper = this.WhenAnyValue(x => x.ReactiveTestField)
3639
.ToProperty(this, x => x.ObservableTestField);
40+
41+
_testHelper = this.WhenAnyValue(x => x.TestProperty).ToProperty(this, x => x.Test);
42+
43+
TestProperty = null;
44+
var t0 = Test;
45+
46+
TestProperty = "Test";
47+
48+
var t1 = Test;
49+
50+
TestProperty = null;
51+
var t2 = Test;
52+
53+
TestProperty = "Test2";
54+
var t3 = Test;
3755
}
3856

3957
/// <summary>
@@ -53,4 +71,13 @@ public TestClassOAPH_VM()
5371
/// </value>
5472
[Reactive]
5573
public partial bool ReactiveTestProperty { get; set; }
74+
75+
/// <summary>
76+
/// Gets the test.
77+
/// </summary>
78+
/// <value>
79+
/// The test.
80+
/// </value>
81+
[ObservableAsProperty]
82+
public partial string? Test { get; }
5683
}

src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ namespace SGReactiveUI.SourceGenerators.Test;
2121
/// <summary>
2222
/// TestClass.
2323
/// </summary>
24+
/// <seealso cref="ReactiveUI.ReactiveObject" />
25+
/// <seealso cref="ReactiveUI.IActivatableViewModel" />
26+
/// <seealso cref="System.IDisposable" />
2427
/// <seealso cref="ReactiveObject" />
2528
/// <seealso cref="IActivatableViewModel" />
2629
/// <seealso cref="IDisposable" />
@@ -218,6 +221,14 @@ public TestViewModel()
218221
/// </value>
219222
public static TestViewModel Instance { get; } = new();
220223

224+
/// <summary>
225+
/// Gets the test class oaph vm.
226+
/// </summary>
227+
/// <value>
228+
/// The test class oaph vm.
229+
/// </value>
230+
public TestClassOAPH_VM TestClassOAPH_VM { get; } = new();
231+
221232
/// <summary>
222233
/// Gets or sets the partial property test.
223234
/// </summary>

src/ReactiveUI.SourceGenerators.Roslyn/ObservableAsProperty/ObservableAsPropertyGenerator{FromObservable}.Execute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private static string GetPropertySyntax(ObservableMethodInfo propertyInfo)
307307
{
308308
var propertyAttributes = string.Join("\n ", AttributeDefinitions.ExcludeFromCodeCoverage.Concat(propertyInfo.ForwardedPropertyAttributes));
309309
var getterFieldIdentifierName = propertyInfo.GetGeneratedFieldName();
310-
var getterArrowExpression = propertyInfo.IsNullableType
310+
var getterArrowExpression = propertyInfo.IsNullableType || propertyInfo.IsFromPartialProperty
311311
? $"{getterFieldIdentifierName} = ({getterFieldIdentifierName}Helper == null ? {getterFieldIdentifierName} : {getterFieldIdentifierName}Helper.Value)"
312312
: $"{getterFieldIdentifierName} = {getterFieldIdentifierName}Helper?.Value ?? {getterFieldIdentifierName}";
313313

0 commit comments

Comments
 (0)