-
Notifications
You must be signed in to change notification settings - Fork 370
Closed
Labels
bug 🐛An unexpected issue that highlights incorrect behaviorAn unexpected issue that highlights incorrect behaviormvvm-toolkit 🧰Issues/PRs for the MVVM ToolkitIssues/PRs for the MVVM Toolkit
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bug 🐛An unexpected issue that highlights incorrect behaviorAn unexpected issue that highlights incorrect behaviormvvm-toolkit 🧰Issues/PRs for the MVVM ToolkitIssues/PRs for the MVVM Toolkit