Skip to content

Commit b733e68

Browse files
committed
Enabled nullability annotations for [ObservableProperty]
1 parent 27f429f commit b733e68

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
339339
updateAndNotificationBlock));
340340
}
341341

342+
// Get the right type for the declared property (including nullability annotations)
343+
TypeSyntax propertyType = IdentifierName(typeName);
344+
345+
if (fieldSymbol.Type is { IsReferenceType: true, NullableAnnotation: NullableAnnotation.Annotated })
346+
{
347+
propertyType = NullableType(propertyType);
348+
}
349+
342350
// Construct the generated property as follows:
343351
//
344352
// <FIELD_TRIVIA>
@@ -349,7 +357,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
349357
// <VALIDATION_ATTRIBUTE2>
350358
// ...
351359
// <VALIDATION_ATTRIBUTEN>
352-
// public <FIELD_TYPE> <PROPERTY_NAME>
360+
// public <FIELD_TYPE><NULLABLE_ANNOTATION?> <PROPERTY_NAME>
353361
// {
354362
// get => <FIELD_NAME>;
355363
// set
@@ -358,7 +366,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
358366
// }
359367
// }
360368
return
361-
PropertyDeclaration(IdentifierName(typeName), Identifier(propertyName))
369+
PropertyDeclaration(propertyType, Identifier(propertyName))
362370
.AddAttributeLists(
363371
AttributeList(SingletonSeparatedList(
364372
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))

UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using Microsoft.Toolkit.Mvvm.ComponentModel;
1111
using Microsoft.VisualStudio.TestTools.UnitTesting;
1212

13+
#nullable enable
14+
1315
namespace UnitTests.Mvvm
1416
{
1517
[TestClass]
@@ -128,11 +130,11 @@ public sealed partial class DependentPropertyModel
128130
{
129131
[ObservableProperty]
130132
[AlsoNotifyFor(nameof(FullName))]
131-
private string name;
133+
private string? name;
132134

133135
[ObservableProperty]
134136
[AlsoNotifyFor(nameof(FullName))]
135-
private string surname;
137+
private string? surname;
136138

137139
public string FullName => $"{Name} {Surname}";
138140
}
@@ -143,15 +145,15 @@ public partial class MyFormViewModel : ObservableValidator
143145
[Required]
144146
[MinLength(1)]
145147
[MaxLength(100)]
146-
private string name;
148+
private string? name;
147149

148150
[ObservableProperty]
149151
[Range(0, 120)]
150152
private int age;
151153

152154
[ObservableProperty]
153155
[EmailAddress]
154-
private string email;
156+
private string? email;
155157

156158
[ObservableProperty]
157159
[TestValidation(null, typeof(SampleModel), true, 6.28, new[] { "Bob", "Ross" }, NestedArray = new object[] { 1, "Hello", new int[] { 2, 3, 4 } }, Animal = Animal.Llama)]
@@ -160,7 +162,7 @@ public partial class MyFormViewModel : ObservableValidator
160162

161163
private sealed class TestValidationAttribute : ValidationAttribute
162164
{
163-
public TestValidationAttribute(object o, Type t, bool flag, double d, string[] names)
165+
public TestValidationAttribute(object? o, Type t, bool flag, double d, string[] names)
164166
{
165167
O = o;
166168
T = t;
@@ -169,7 +171,7 @@ public TestValidationAttribute(object o, Type t, bool flag, double d, string[] n
169171
Names = names;
170172
}
171173

172-
public object O { get; }
174+
public object? O { get; }
173175

174176
public Type T { get; }
175177

0 commit comments

Comments
 (0)