From f5637cd1f331b9201769fbdbfa8113843b7059f4 Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 11 Aug 2023 01:22:59 -0400 Subject: [PATCH] Add read-only property skipping in serializer --- .../ViewModels/LoadableViewModelBase.cs | 8 ++++++ .../Avalonia/LoadableViewModelBaseTests.cs | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/StabilityMatrix.Avalonia/ViewModels/LoadableViewModelBase.cs b/StabilityMatrix.Avalonia/ViewModels/LoadableViewModelBase.cs index cf06789d..81fc5d1a 100644 --- a/StabilityMatrix.Avalonia/ViewModels/LoadableViewModelBase.cs +++ b/StabilityMatrix.Avalonia/ViewModels/LoadableViewModelBase.cs @@ -30,6 +30,12 @@ public abstract class LoadableViewModelBase : ViewModelBase, IJsonLoadableState private static bool ShouldIgnoreProperty(PropertyInfo property) { + // Check not read-only + if (property.SetMethod is null) + { + Logger.Trace("Skipping {Property} - read-only", property.Name); + return true; + } // Check not JsonIgnore if (property.GetCustomAttributes(typeof(JsonIgnoreAttribute), true).Length > 0) { @@ -58,6 +64,7 @@ public abstract class LoadableViewModelBase : ViewModelBase, IJsonLoadableState /// For the following properties on this class, we will try to set from the JSON object: /// /// Public + /// Not read-only /// Not marked with [JsonIgnore] /// Not a type within the SerializerIgnoredTypes /// Not a name within the SerializerIgnoredNames @@ -128,6 +135,7 @@ public abstract class LoadableViewModelBase : ViewModelBase, IJsonLoadableState /// save all properties that are: /// /// Public + /// Not read-only /// Not marked with [JsonIgnore] /// Not a type within the SerializerIgnoredTypes /// Not a name within the SerializerIgnoredNames diff --git a/StabilityMatrix.Tests/Avalonia/LoadableViewModelBaseTests.cs b/StabilityMatrix.Tests/Avalonia/LoadableViewModelBaseTests.cs index 4de24799..83df2b4c 100644 --- a/StabilityMatrix.Tests/Avalonia/LoadableViewModelBaseTests.cs +++ b/StabilityMatrix.Tests/Avalonia/LoadableViewModelBaseTests.cs @@ -21,6 +21,16 @@ public class TestLoadableViewModel : LoadableViewModelBase public int Ignored { get; set; } } +public class TestLoadableViewModelReadOnly : LoadableViewModelBase +{ + public int ReadOnly { get; } + + public TestLoadableViewModelReadOnly(int readOnly) + { + ReadOnly = readOnly; + } +} + public partial class TestLoadableViewModelObservable : LoadableViewModelBase { [ObservableProperty] @@ -200,4 +210,22 @@ public class LoadableViewModelBaseTests Assert.AreEqual(123, loadedNested.Id); Assert.AreEqual(0, loadedNested.Ignored); } + + [TestMethod] + public void TestLoadStateFromJsonObject_ReadOnly() + { + var vm = new TestLoadableViewModelReadOnly(456); + + var state = vm.SaveStateToJsonObject(); + + // Check no properties were serialized + Assert.AreEqual(0, state.Count); + + // Create a new instance and load the state + var vm2 = new TestLoadableViewModelReadOnly(123); + vm2.LoadStateFromJsonObject(state); + + // Read only property should have been ignored + Assert.AreEqual(123, vm2.ReadOnly); + } }