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

[property: DefaultValue(0.0)] changed to [DefaultValue(0)] from field to property #601

Closed TruePluto closed 1 year ago

TruePluto commented 1 year ago

Describe the bug

when using [property: DefaultValue(0.0)]

    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(Summary))]
    [property: JsonProperty]
    [property: DefaultValue(0.0)]
    private double _x2 = 0.0;

the sourcegenerator build code to [DefaultValue(0)] from field to property

/// <inheritdoc cref="_x2"/>
        [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        [global::Newtonsoft.Json.JsonPropertyAttribute()]
        [global::System.ComponentModel.DefaultValueAttribute(0)]
        public double X2
        {
            get => _x2;
            set
            {
                if (!global::System.Collections.Generic.EqualityComparer<double>.Default.Equals(_x2, value))
                {
                    OnX2Changing(value);
                    OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.X2);
                    _x2 = value;
                    OnX2Changed(value);
                    OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.X2);
                    OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Summary);
                }
            }
        }

and that cause newtonsoft.json fail with DefaultValueHandling = DefaultValueHandling.Ignore,

Regression

No response

Steps to reproduce

I have a viewmodel such as

public partial class BaseVM : ObservableRecipient
{
    [DefaultValue(7)]
    [JsonProperty()]
    public double PropDoubleZeroO { get; set; } = 7;

    [DefaultValue(7.0)]
    [JsonProperty()]
    public double PropDoubleZeroA { get; set; } = 7.0;

    [DefaultValue(7)]
    [JsonProperty()]
    public double PropDoubleZeroB { get; set; } = 7.0;

    [DefaultValue(7.0)]
    [JsonProperty()]
    public double PropDoubleZeroC { get; set; } = 7;

    //[property: JsonProperty]
    //[property: DefaultValue((double)15)]
    //private double _z2 = 165;
}

using the code following

BaseVM vm0= new BaseVM();
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
{
    Formatting = Formatting.Indented,
    DefaultValueHandling = DefaultValueHandling.Ignore,
    TypeNameHandling = TypeNameHandling.Objects,
    TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
    MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead,
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
};
var TempJson = JsonConvert.SerializeObject(vm0,jsonSerializerSettings);

Console.WriteLine("--------BEGIN----------");
Console.WriteLine(TempJson);
Console.WriteLine("---------END-----------");

the result is

--------BEGIN----------
{
  "$id": "1",
  "$type": "DvjsonTest.BaseVM, JsonDefaultValueConsole",
  "PropDoubleZeroO": 7.0,
  "PropDoubleZeroB": 7.0
}
---------END-----------

Meaning [DefaultValue(7)] is not equal to [DefaultValue(7.0)]

but when I suing ObservableObject with property such as

    [ObservableProperty]
    [property: JsonProperty]
    [property: DefaultValue(0.0)]
    private double _x2 = 0.0;

the sourcegenerator generated the code :

/// <inheritdoc cref="_x2"/>
 ...
        [global::Newtonsoft.Json.JsonPropertyAttribute()]
        [global::System.ComponentModel.DefaultValueAttribute(0)]
        public double X2
        {
            get => _x2;
            set
            {
...
                }
            }
        }

Expected behavior

[global::System.ComponentModel.DefaultValueAttribute(0.0)] is expected

Screenshots

No response

IDE and version

VS 2022

IDE version

No response

Nuget packages

Nuget package version(s)

8.1.0

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

Sergio0694 commented 1 year ago

Good find, thank you! 🙂