Skip to content

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

Closed

Description

@TruePluto

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

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

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

Activity

  1. added
    bug 🐛An unexpected issue that highlights incorrect behavior
    on Feb 10, 2023
  2. Sergio0694 commented on Feb 10, 2023

    @Sergio0694
    Member

    Good find, thank you! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions