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

Observable property attribute with negative enum values stops generating code #681

Closed
1 of 4 tasks
n-coelho-cerinnov opened this issue May 4, 2023 · 0 comments · Fixed by #682
Closed
1 of 4 tasks
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit

Comments

@n-coelho-cerinnov
Copy link

n-coelho-cerinnov commented May 4, 2023

Describe the bug

This is a weird bug that I came across by accident.
If an observable property annotated with the ObservablePropertyAttribute has another attribute for the generated property and that attribute has an enum with a negative value, the code generation stops working. I'm assuming there might be a crash with the code generator when it encounters this case and no code is generated further.
This bug can be easily reproduced with the code below.

Regression

No response

Steps to reproduce

This bug occurred when I tested in .NET 7 in a WPF project with Visual Studio 17.5.5 and the latest CommunityToolkit.Mvvm 8.2.0. It probably would happen with other versions.
Using the following code the bug can be reproduced:

using CommunityToolkit.Mvvm.ComponentModel;
using System.ComponentModel;

namespace MvvmBug;
public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    private bool _test1;

    [ObservableProperty]
    [property: DefaultValue(PositiveEnum.Something)]
    private PositiveEnum _test2;

    [ObservableProperty]
    [property: DefaultValue(NegativeEnum.Problem)]
    private NegativeEnum _test3;

    [ObservableProperty]
    private int _test4;

    public MainViewModel()
    {
        Test1 = true;
        Test2 = PositiveEnum.Else;
    }
}

public enum PositiveEnum
{
    Something = 0,
    Else = 1
}

public enum NegativeEnum
{
    Problem = -1,
    OK = 0
}

Changing the default value for _test3 to NegativeEnum.OK will allow the code to compile without errors, changing back to NegativeEnum.Problem and the code generator stops working.
Also note that if using the NegativeEnum.Problem, removing the 'property:' before the DefaultValueAttribute doesn't seem to have the problem. It only occurs for attributes passed to the property generated.

Expected behavior

An enum with a negative value in a property attribute shouldn't stop the code generator.

Screenshots

No response

IDE and version

VS 2022

IDE version

17.5.5

Nuget packages

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

Nuget package version(s)

8.2.0

Additional context

No response

Help us help you

No, just wanted to report this

@n-coelho-cerinnov n-coelho-cerinnov added the bug 🐛 An unexpected issue that highlights incorrect behavior label May 4, 2023
@Sergio0694 Sergio0694 added the mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit label May 4, 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 mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants