-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]ObservableValidator.ValidateAllProperties() throws exception when no Validation rules exists. #4272
Comments
Hello svestin, 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 🙌 |
This comment has been minimized.
This comment has been minimized.
@svestin have you tried the newly release 7.1 package as well, does it exhibit the same issue? FYI @Sergio0694 |
@michael-hawker : Yes I see the same behavior in 7.1.0. |
Hi @svestin, I cannot seem to be able to repro this issue on my end, are you sure your repro steps are complete? I defined these two classes: public class MyBase : ObservableValidator
{
public int? MyDummyInt { get; set; } = 0;
public void ValidateAll()
{
ValidateAllProperties();
}
}
public class MyDerived2 : MyBase
{
public string Name { get; set; }
public int SomeRandomproperty { get; set; }
} Then tested them like so: MyBase viewmodel = new();
viewmodel.ValidateAll();
MyDerived2 viewmodel2 = new();
viewmodel2.ValidateAll(); Which worked just fine, it just didn't do anything. When using source generators, it makes sense that this works correctly, as you can also inspect the generated code. namespace Microsoft.Toolkit.Mvvm.ComponentModel.__Internals
{
internal static 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::UnitTests.Mvvm.Test_ObservableRecipientAttribute.MyBase _)
{
static void ValidateAllProperties(object obj)
{
var instance = (global::UnitTests.Mvvm.Test_ObservableRecipientAttribute.MyBase)obj;
}
return ValidateAllProperties;
}
}
} Which is just a no-op. When disabling this path and using the fallback code path with compiled LINQ expressions, you can still see how the implementation will just skip all properties with no validation attributes, and just do nothing: WindowsCommunityToolkit/Microsoft.Toolkit.Mvvm/ComponentModel/ObservableValidator.cs Lines 516 to 518 in fef65a5
If no results are yielded here, the generated method will just not perform any validation (the body will be empty). I'm going to remove the bug label here, feel free to add more info! |
More info: If I create a WPF-app with .Net5 (and as a result a SDK-style project file), everything work as expected. Repeated the same small example on WPF .Net Framework 4.8 (old stye project file) and now I end up with Exception thrown: 'System.ArgumentException' in System.Core.dll
Originally I reported the problem in .Net Framework 4.7.2 Looking in Nuget manager I noticed that there is a difference in presentation for 4.7.2, 4.8 compared to .Net5. If that somehow helps. Thanks for looking into my problem! |
Oooh, that helps, thanks for the additional info! 😄 WindowsCommunityToolkit/Microsoft.Toolkit.Mvvm/ComponentModel/ObservableValidator.cs Lines 515 to 525 in fef65a5
That @michael-hawker If we plan on pushing out a hotfix for 7.1, I'd be happy to also add a workaround for this scenario. The fact we're still officially supporting .NET Standard 2.0 and that this workaround would also be a small optimization for this specific scenario makes me feel like it'd be worth adding even though the issue doesn't happen on .NET 5. Thoughts? 🙂 EDIT: I'll just fix this and open a PR, and then we can just merge it and push the fix whenever we release the next version 👍 |
Possible bug in Microsoft.Toolkit.Mvvm version 7.0.2 (running on .Net Framework 4.7.2):
(note: same behaviour in 7.1.0)
Windows 10 Enterprise 1909, 18363.1734
Visual Studio 2019, 16.11.3
Maybe I have misunderstood the usage of ObservableValidator.ValidateAllProperties(), but
when I run the example below, I end up with the unexpected exception:
“System.ArgumentException: 'Non-empty collection required Parameter name: expressions' ”
and then use it like
However, if I add the RequiredAttribute on MyDummyInt it works fine.
So why do I need to Validate a class with not validation rules?
The MyBase class normally has no properties that needs validation.
I then have some classes that derives from MyBase
MyDerived1: HAS some properties with validation rules.
MyDerived2: Has NO properties with validation rules.
Then I have a List storing instances of MyDerived1 and MyDerived2.
Now I would like to validate the instances in my list.
Sadly I get the unexpected exception when I try to validate an instance of MyDerived2 (that has no validation rules).
At the moment I have to add the MyDummyInt (with the RequiredAttribute) in the base class to avoid the unexpected exception.
Maybe it is possible to check if a class is missing validation attributes? Then I could avoid calling ValidateAllProperties() if there are no validation attributes used.
Any help would be very much appreciated.
Best Regards
The text was updated successfully, but these errors were encountered: