Skip to content

Get-only collection properties (List<string> Items { get; } = []) are saved but load back empty, and the next save erases them #341

Open

Description

@matt-edmondson

What's wrong

A settings class whose collections are get-only — the shape the ktsu SDK requires, since CA2227 ("collection properties should be read only") is a build error — writes those collections to disk but never reads them back.

public class MySettings : AppData<MySettings>
{
	public List<string> Items { get; } = [];
	public Dictionary<string, int> Map { get; } = [];
}

var s = MySettings.LoadOrCreate();
s.Items.Add("one");
s.Map["k"] = 7;
s.Save();
// file: {"$id":"1","Items":{"$id":"2","$values":["one"]},"Map":{"$id":"3","k":7}}

var loaded = MySettings.LoadOrCreate();   // fresh process / cache
// loaded.Items.Count == 0, loaded.Map.Count == 0   (expected 1 and 1)

No exception is thrown, so nothing is archived. The next Save() / QueueSave() writes the empty collections over the file, and the user's data is gone for good.

Reproduced with a temporary MSTest in AppDataStorage.Test at 3b1e8f5 (Assert.AreEqual(1, loaded.Items.Count) fails at the load step).

Why

AppData.JsonSerializerOptions (AppDataStorage/AppData.cs:36-46) uses default object-creation handling, so System.Text.Json skips any property that has no setter. Writing works because serialization only needs the getter.

Turning on PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate alone does not work: with the current ReferenceHandler = ReferenceHandler.Preserve it throws InvalidOperationException: JsonObjectCreationHandling.Populate is incompatible with reference handling. With Preserve removed and Populate set, the same round trip returns Items=1, Map=1.

Suggested fix

One of:

  1. Drop ReferenceHandler.Preserve and set PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate. Existing files carry $id/$values metadata, so loading has to keep accepting them: for example, read with a Preserve-compatible fallback when the first attempt hits metadata, or strip the metadata before the read.
  2. Keep Preserve, and detect get-only collection members on AppData<T> types. Fail loudly (or warn) instead of silently dropping them, and document that settings collections need setters (with a CA2227 suppression).

Acceptance: a round-trip test with get-only List<T> and Dictionary<K,V> members passes, and so does a test that loads a file written by the current version.

Activity

  1. added
    bug
    on Oct 7, 2026
  2. matt-edmondson commented on Oct 13, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    I looked at this during an automated issue-resolution run and decided not to implement it yet. It needs a design decision first.

    • Option 1 (drop ReferenceHandler.Preserve, set Populate) changes the public AppData.JsonSerializerOptions and the on-disk format. It also stops shared or cyclic references from round-tripping for any consumer that relies on them, and old $id/$values files still need a fallback reader.
    • Option 2 (keep Preserve, fail loudly on get-only collections) keeps the format but turns silent data loss into a startup error, which is a breaking change for every settings class with this shape. Many ktsu apps have that shape, because CA2227 pushes them toward it.

    Next step: pick option 1 or option 2, and say whether supporting cyclic references matters. The fix is straightforward after that.


    Generated by Claude Code

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions