Skip to content

Commit b5b0045

Browse files
committed
Fix duplicate generated files for ObservableValidator generator
1 parent d1f7ef9 commit b5b0045

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Linq;
6-
using System.Text;
76
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
87
using CommunityToolkit.Mvvm.SourceGenerators.Input.Models;
98
using Microsoft.CodeAnalysis;
109
using Microsoft.CodeAnalysis.CSharp;
1110
using Microsoft.CodeAnalysis.CSharp.Syntax;
12-
using Microsoft.CodeAnalysis.Text;
1311

1412
namespace CommunityToolkit.Mvvm.SourceGenerators;
1513

@@ -27,7 +25,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2725
context.SyntaxProvider
2826
.CreateSyntaxProvider(
2927
static (node, _) => node is ClassDeclarationSyntax,
30-
static (context, _) => (INamedTypeSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!);
28+
static (context, _) => (context.Node, Symbol: (INamedTypeSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!))
29+
.Where(static item => item.Node.IsFirstSyntaxDeclarationForSymbol(item.Symbol))
30+
.Select(static (item, _) => item.Symbol);
3131

3232
// Get the types that inherit from ObservableValidator and gather their info
3333
IncrementalValuesProvider<ValidationInfo> validationInfo =

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservableValidator.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,36 @@ public void Test_ObservableValidator_ValidateAllProperties_WithFallback()
382382
Assert.IsTrue(events.Any(e => e.PropertyName == nameof(Person.Age)));
383383
}
384384

385+
// See https://github.com/CommunityToolkit/dotnet/issues/235
386+
[TestMethod]
387+
public void Test_ObservableValidator_ValidateAllProperties_WithinPartialClassDeclaration()
388+
{
389+
PersonWithPartialDeclaration model = new();
390+
List<DataErrorsChangedEventArgs> events = new();
391+
392+
model.ErrorsChanged += (s, e) => events.Add(e);
393+
394+
model.ValidateAllProperties();
395+
396+
Assert.IsTrue(model.HasErrors);
397+
Assert.IsTrue(events.Count == 2);
398+
399+
Assert.IsTrue(events.Any(e => e.PropertyName == nameof(PersonWithPartialDeclaration.Name)));
400+
Assert.IsTrue(events.Any(e => e.PropertyName == nameof(PersonWithPartialDeclaration.Number)));
401+
402+
events.Clear();
403+
404+
model.Name = "Bob";
405+
model.Number = 42;
406+
407+
model.ValidateAllProperties();
408+
409+
Assert.IsFalse(model.HasErrors);
410+
Assert.IsTrue(events.Count == 2);
411+
Assert.IsTrue(events.Any(e => e.PropertyName == nameof(PersonWithPartialDeclaration.Name)));
412+
Assert.IsTrue(events.Any(e => e.PropertyName == nameof(PersonWithPartialDeclaration.Number)));
413+
}
414+
385415
[TestMethod]
386416
public void Test_ObservableValidator_CustomValidation()
387417
{
@@ -729,4 +759,22 @@ public class ObservableValidatorDerived : ObservableValidatorBase
729759

730760
public int SomeRandomproperty { get; set; }
731761
}
762+
763+
public partial class PersonWithPartialDeclaration : ObservableValidator
764+
{
765+
[Required]
766+
[MinLength(1)]
767+
public string? Name { get; set; }
768+
769+
public new void ValidateAllProperties()
770+
{
771+
base.ValidateAllProperties();
772+
}
773+
}
774+
775+
public partial class PersonWithPartialDeclaration
776+
{
777+
[Range(10, 1000)]
778+
public int Number { get; set; }
779+
}
732780
}

0 commit comments

Comments
 (0)