Skip to content

Commit db831fc

Browse files
committed
Fix build error with generic ObservableValidator types
1 parent 59d9299 commit db831fc

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2828
.CreateSyntaxProvider(
2929
static (node, _) => node is ClassDeclarationSyntax,
3030
static (context, _) => (context.Node, Symbol: (INamedTypeSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!))
31-
.Where(static item => !item.Symbol.IsAbstract && item.Node.IsFirstSyntaxDeclarationForSymbol(item.Symbol))
31+
.Where(static item => item.Symbol is { IsAbstract: false, IsGenericType: false } && item.Node.IsFirstSyntaxDeclarationForSymbol(item.Symbol))
3232
.Select(static (item, _) => item.Symbol);
3333

3434
// Get the types that inherit from ObservableValidator and gather their info

CommunityToolkit.Mvvm/ComponentModel/ObservableValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,8 @@ where getMethod is not null
606606
from property in validatableProperties
607607
select Expression.Call(inst0, validateMethod, new Expression[]
608608
{
609-
Expression.Convert(Expression.Call(inst0, property.GetMethod), typeof(object)),
610-
Expression.Constant(property.Name)
609+
Expression.Convert(Expression.Call(inst0, property.GetMethod), typeof(object)),
610+
Expression.Constant(property.Name)
611611
}));
612612

613613
return Expression.Lambda<Action<object>>(body, arg0).Compile();

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservableValidator.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,32 @@ public void Test_ObservableRecipient_AbstractTypesDoNotTriggerCodeGeneration()
539539
Assert.IsNull(createAllPropertiesValidatorMethod);
540540
}
541541

542+
// See https://github.com/CommunityToolkit/dotnet/issues/246
543+
[TestMethod]
544+
public void Test_ObservableValidator_WithGenericTypeParameters()
545+
{
546+
GenericPerson<string> model = new();
547+
548+
model.Name = "Bob";
549+
550+
model.ValidateAllProperties();
551+
552+
Assert.IsTrue(model.HasErrors);
553+
554+
ValidationResult[] errors = model.GetErrors(nameof(model.Value)).ToArray();
555+
556+
Assert.IsNotNull(errors);
557+
Assert.AreEqual(errors.Length, 1);
558+
559+
CollectionAssert.AreEqual(errors[0].MemberNames.ToArray(), new[] { nameof(model.Value) });
560+
561+
model.Value = "Ross";
562+
563+
model.ValidateAllProperties();
564+
565+
Assert.IsFalse(model.HasErrors);
566+
}
567+
542568
public class Person : ObservableValidator
543569
{
544570
private string? name;
@@ -797,4 +823,19 @@ public abstract class AbstractModelWithValidatableProperty : ObservableValidator
797823
[MinLength(2)]
798824
public string? Name { get; set; }
799825
}
826+
827+
public class GenericPerson<T> : ObservableValidator
828+
{
829+
[Required]
830+
[MinLength(1)]
831+
public string? Name { get; set; }
832+
833+
[Required]
834+
public T? Value { get; set; }
835+
836+
public new void ValidateAllProperties()
837+
{
838+
base.ValidateAllProperties();
839+
}
840+
}
800841
}

0 commit comments

Comments
 (0)