Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions AppDataStorage.Test/NonPublicSetterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,74 @@ public void Change(int volume, string theme, int ignored)
}
}

public sealed class WindowSettings
{
public int Width { get; private set; } = 800;
public string Title { get; internal set; } = "untitled";

[JsonIgnore]
public int Ignored { get; private set; } = 7;

public PositionSettings Position { get; set; } = new();

public void Change(int width, string title, int ignored)
{
Width = width;
Title = title;
Ignored = ignored;
}
}

public sealed class PositionSettings
{
public int X { get; private set; }

public void MoveTo(int x) => X = x;
}

public sealed class NestedSettings : AppData<NestedSettings>
{
public WindowSettings Window { get; set; } = new();
}

[TestMethod]
public void NestedNonPublicSettersRoundTrip()
{
AbsoluteDirectoryPath root = Root("nested_non_public_setters");
using (NestedSettings settings = NestedSettings.LoadOrCreateIn(root))
{
settings.Window.Change(1920, "main", ignored: 9);
settings.Window.Position.MoveTo(42);
settings.Save();
}

using NestedSettings reloaded = NestedSettings.LoadOrCreateIn(root);
Assert.AreEqual(1920, reloaded.Window.Width, "A private setter on a nested object should be used when loading.");
Assert.AreEqual("main", reloaded.Window.Title, "An internal setter on a nested object should be used when loading.");
Assert.AreEqual(42, reloaded.Window.Position.X, "A private setter two levels down should be used when loading.");
Assert.AreEqual(7, reloaded.Window.Ignored, "A nested [JsonIgnore] property should still be ignored.");
}

[TestMethod]
public void NextSaveKeepsTheStoredNestedValue()
{
AbsoluteDirectoryPath root = Root("nested_non_public_setters_resave");
using (NestedSettings settings = NestedSettings.LoadOrCreateIn(root))
{
settings.Window.Change(1920, "main", ignored: 0);
settings.Save();
}

using (NestedSettings reloaded = NestedSettings.LoadOrCreateIn(root))
{
reloaded.Save();
}

using NestedSettings again = NestedSettings.LoadOrCreateIn(root);
Assert.AreEqual(1920, again.Window.Width, "Saving a reloaded instance should not erase the stored nested value.");
Assert.AreEqual("main", again.Window.Title, "Saving a reloaded instance should not erase the stored nested value.");
}

[TestMethod]
public void NonPublicSettersRoundTrip()
{
Expand Down
24 changes: 8 additions & 16 deletions AppDataStorage/AppData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,23 @@
};

/// <summary>
/// Lets deserialization use the non-public setter of a public property on an <see cref="AppData{T}"/> type.
/// Lets deserialization use the non-public setter of a public property on any object in an
/// <see cref="AppData{T}"/> graph: the <see cref="AppData{T}"/> type itself and the settings objects it holds.
/// </summary>
/// <remarks>
/// System.Text.Json writes a public property whose setter is private, protected or internal, but skips
/// it when reading unless it has <see cref="JsonIncludeAttribute"/>. Nothing fails, so the stored value
/// silently loads back as its default and the next save erases it. <c>{ get; private set; }</c> is the
/// natural shape for an encapsulated setting, so the setter is used instead.
/// natural shape for an encapsulated setting, at the top level or in a nested group such as
/// <c>Window</c> or <c>Audio</c>, so the setter is used instead. These options only ever serialize an
/// <see cref="AppData{T}"/> graph, so this reaches no other caller's serialization. Only properties
/// already in the contract are touched, so <see cref="JsonIgnoreAttribute"/> still applies, and types
/// written by a converter have no object contract, so they are unaffected.
/// </remarks>
/// <param name="typeInfo">The contract being built.</param>
internal static void UseNonPublicSettersOfAppDataProperties(JsonTypeInfo typeInfo)
{
if (typeInfo.Kind != JsonTypeInfoKind.Object || !IsAppDataType(typeInfo.Type))
if (typeInfo.Kind != JsonTypeInfoKind.Object)
{
return;
}
Expand Down Expand Up @@ -109,19 +114,6 @@
}
}

private static bool IsAppDataType(Type type)
{
for (Type? current = type.BaseType; current is not null; current = current.BaseType)
{
if (current.IsGenericType && current.GetGenericTypeDefinition() == typeof(AppData<>))
{
return true;
}
}

return false;
}

/// <summary>
/// Thread-local factory for creating file system instances to ensure test isolation.
/// </summary>
Expand Down Expand Up @@ -584,10 +576,10 @@
/// </summary>
#if NET9_0_OR_GREATER
[JsonIgnore]
public static Lock Lock { get; } = new();

Check warning on line 579 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 579 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 579 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 579 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.
#else
[JsonIgnore]
public static object Lock { get; } = new();

Check warning on line 582 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 582 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 582 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 582 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 582 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 582 in AppDataStorage/AppData.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

A static field in a generic type is not shared among instances of different close constructed types.
#endif

internal bool IsSaveQueued()
Expand Down
Loading