Skip to content
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

[ObservableValidator] Crash on NativeAOT published app when using custom validator #810

Open
1 of 4 tasks
FirehawkV21 opened this issue Dec 15, 2023 · 0 comments
Open
1 of 4 tasks
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior

Comments

@FirehawkV21
Copy link

FirehawkV21 commented Dec 15, 2023

Describe the bug

If an app is published with NativeAOT active and it has custom validators, the app would crash when trying to validate the property. Debugging the NativeAOT app returns the error "The type <ViewModel> does not contain a public property named <Property>."

Regression

No response

Steps to reproduce

1. Create a new UI app that can be built with NativeAOT (for example, Avalonia).
2. Enable `PublishAot`

<PublishAot>true</PublishAot>

3. Create a UI that has a text box.
4. In the view model:

public sealed partial class MainWindowViewModel : ObservableValidator {

    [ObservableProperty]
    [IsValidPath(true)]
    [NotifyDataErrorInfo]
    string _sourceLocation;
}

6. Create a simple validator, like this:

public sealed class IsValidPathAttribute : ValidationAttribute
{
    private readonly bool _isRequired;

    public IsValidPathAttribute(bool required)
    {
        _isRequired = required;
    }

    public override bool IsValid(object value)
    {
        string potentialPath = value?.ToString();

        if (!string.IsNullOrEmpty(potentialPath) || !string.IsNullOrWhiteSpace(potentialPath))
        {
            foreach (char invalidChar in Path.GetInvalidPathChars())
            {
                if (potentialPath.Contains(invalidChar))
                {
                    ErrorMessage = "The path to the file shouldn't have invalid characters.";
                    return false;
                }
            }

            if (!Directory.Exists(potentialPath) || !(Path.IsPathRooted(potentialPath) && !Path.GetPathRoot(potentialPath).Equals(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)))
            {
                ErrorMessage = "The path doesn't exist or isn't valid.";
                return false;
            }

            return true;
        }

        if (_isRequired)
        {
            ErrorMessage = "This is required.";
        }

        return !_isRequired;
    }
}

7. Bind the property to the text box and run.
8. Run the following command in the CLI (while pointing to the project's .csproj file):

dotnet publish -a x64 --self-contained

8 Run the published executable.

Expected behavior

The app should run normally and do the proper validation.

Screenshots

No response

IDE and version

VS 2022

IDE version

17.8.3

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

Yes, but only if others can assist

@FirehawkV21 FirehawkV21 added the bug 🐛 An unexpected issue that highlights incorrect behavior label Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior
Projects
None yet
Development

No branches or pull requests

1 participant