CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.07k stars 299 forks source link

ObservableValidator not generating validation code for properties declared in abstract classes #691

Closed dgellow closed 1 year ago

dgellow commented 1 year ago

Describe the bug

When using MVVM Toolkit with ObservableValidator, I noticed that the source generator does not generate the validation code for abstract classes as part of the generated method static void ValidateAllProperties(object obj).

Example

Here is a sample abstract class with validator properties:

public partial abstract class BaseMyViewModel: ObservableValidator
{
    [Required]
    private string MyProperty;
}

Here is a concrete class that inherits from the abstract class:

public class MyViewModel : BaseMyViewModel
{
}

Here is the generated source file C:\Users\sam\AppData\Local\Temp\VSGeneratedDocuments\ef8d18dd-b51e-be83-e0c9-e148364b22d7\MyNamespace.MyViewModel.g.cs:

// <auto-generated/>
#pragma warning disable
namespace CommunityToolkit.Mvvm.ComponentModel.__Internals
{
    partial class __ObservableValidatorExtensions
    {
        [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
        [global::System.Obsolete("This method is not intended to be called directly by user code")]
        public static global::System.Action<object> CreateAllPropertiesValidator(global::MyNamespace.MyViewModel _)
        {
            static void ValidateAllProperties(object obj)
            {
                var instance = (global::MyNamespace.MyViewModel)obj;
            }

            return ValidateAllProperties;
        }
    }
}

Here is what I would instead expect as the implementation of ValidateAllProperties:

static void ValidateAllProperties(object obj)
{
    var instance = (global::MyNamespace.MyViewModel)obj;
    __ObservableValidatorHelper.ValidateProperty(instance, instance.MyProperty, nameof(instance.MyProperty));
}

Steps to reproduce

  1. Create an abstract class that inherits from ObservableValidator.
  2. Add properties with validation attributes.
  3. Create a concrete class that inherits the abstract class.
  4. Build the project.
  5. Look at sources generated in namespace CommunityToolkit.Mvvm.ComponentModel.__Internals.

Expected behavior

The generated code for the concrete class should contain the validation logic for properties in the abstract class.

Visual Studio Version

2022

Nuget packages

CommunityToolkit.Mvvm v8.1.0

Help us help you

Yes, but only if others can assist.

ghost commented 1 year ago

Hello dgellow, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

kmgallahan commented 1 year ago

Wrong repo:

https://github.com/CommunityToolkit/dotnet

dgellow commented 1 year ago

Thanks Sergio, that was a quick fix :)