Skip to content

A private setter on a nested settings object ({ get; private set; } inside a class held by an AppData property) still loads back as its default, and the next save erases it #365

Closed

Description

@matt-edmondson

What's wrong

#364 fixed #362 for properties declared directly on an AppData<T> type, but the resolver modifier only covers those types. UseNonPublicSettersOfAppDataProperties returns straight away for any contract whose type is not derived from AppData<> (AppDataStorage/AppData.cs:70-73, IsAppDataType at :86-97). So a settings object that groups its values in a nested class still hits the original System.Text.Json behaviour: the property is written on save and skipped on load, with no error.

public class WindowSettings
{
    public int Width { get; private set; } = 800;
    public void Resize(int width) => Width = width;
}

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

var s = MySettings.LoadOrCreateIn(root);
s.Window.Resize(1920);
s.Save();                 // file holds "Window": { "Width": 1920 }

var r = MySettings.LoadOrCreateIn(root);
// r.Window.Width == 800, but 1920 was expected
r.Save();                 // the file now holds "Width": 800, so the stored 1920 is gone

This is the same silent data loss #362 describes, one level down. Nothing is thrown, the .corrupt archive never fires, and the next Save()/QueueSave() writes the default over the user's value. Grouping settings into nested classes (Window, Audio, Recent) is the usual way to organise a settings file. The private-setter-plus-method shape is the encapsulated style #362 calls natural, and it shows up most often in exactly these nested value objects.

The PR said it left nested types out on purpose, to stay within what #362 asked for. It doesn't say nested types should keep losing data, so this issue is the follow-up.

Suggested fix

Apply the modifier to every object contract the AppData options build, not only AppData<T> types. These options are private to the library, and serializing an AppData graph is all they're used for, so widening the check doesn't reach any other caller's serialization. The current guards still keep it safe:

  • only properties already in the contract are touched, so [JsonIgnore] stays ignored
  • only properties with no setter in the contract get one
  • converter-handled types (RoundTripStringJsonConverterFactory, semantic strings, enums) have no object contract, so they're unaffected

If widening seems too broad, an alternative is to fail loudly for any serialized property that can't be read back.

Acceptance criteria

  • A round-trip test with a nested class holding { get; private set; } and { get; internal set; } properties keeps the stored values on reload, and saving the reloaded instance doesn't overwrite them.
  • A nested [JsonIgnore] property with a private setter stays ignored.
  • The existing NonPublicSetterTests still pass.

Activity

  1. added
    bug
    on Nov 4, 2026
  2. matt-edmondson commented on Nov 4, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions