You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Get-only collection properties (List<string> Items { get; } = []) are saved but load back empty, and the next save erases them #341
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.
publicclassMySettings:AppData<MySettings>{publicList<string>Items{get;}=[];publicDictionary<string,int>Map{get;}=[];}vars=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}}varloaded=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:
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.
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.
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.
Activity
matt-edmondson commentedon Oct 13, 2026
Triage
I looked at this during an automated issue-resolution run and decided not to implement it yet. It needs a design decision first.
ReferenceHandler.Preserve, setPopulate) changes the publicAppData.JsonSerializerOptionsand the on-disk format. It also stops shared or cyclic references from round-tripping for any consumer that relies on them, and old$id/$valuesfiles still need a fallback reader.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
Load properties whose setter is private or internal [patch]
Load properties whose setter is private or internal [patch] (#364)