Skip to content

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

@dgellow

Description

@dgellow

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛An unexpected issue that highlights incorrect behaviormvvm-toolkit 🧰Issues/PRs for the MVVM Toolkit

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions