Repository navigation
Closed
Description
What's wrong
NavigationItem (Navigation/Models/NavigationItem.cs) exposes its state in ways System.Text.Json cannot write back into:
Metadatais a computed, get-only property (=> new ReadOnlyDictionary<string, object>(_metadata)) over a private field. The only way to fill it isSetMetadata. On deserialization STJ writes theMetadataobject to the JSON, then on the way back in finds nothing it can set, so it drops the value without an error.CreatedAtis get-only and is set toDateTime.UtcNowin the constructor, so a loaded item takes the load time rather than the time it was first created.
This is separate from #53, which is about NavigationState<T> failing to deserialize at all. Once #53 is fixed, loads succeed but hand back items with this data missing.
Repro
var json = """{"Id":"x","DisplayName":"X","CreatedAt":"2020-01-01T00:00:00Z","Metadata":{"k":"v"}}""";
var item = JsonSerializer.Deserialize<NavigationItem>(json)!;
// item.Metadata.Count == 0 (expected 1)
// item.CreatedAt == DateTime.UtcNow at load time (expected 2020-01-01)The same happens to every item in a NavigationState<NavigationItem> loaded through JsonFilePersistenceProvider.
Why it matters
Metadata is the documented way to attach per-page context, such as scroll position, a selected record id, or query parameters. An app that saves on exit and restores on start gets every page back with that context wiped, and nothing reports it. Anything that sorts or expires history by CreatedAt also breaks after a reload.
Suggested fix
- Add a
[JsonConstructor]constructor that takesid,displayName,createdAt, andIReadOnlyDictionary<string, object>? metadata, and copies the metadata into_metadata. Its parameter names and types have to match the properties. - Metadata values come back as
JsonElementrather than their original CLR types. Either say so in the docs, or narrow metadata tostringvalues, so that callers are not surprised.
Acceptance criteria
- A test serializes a
NavigationItemthat has metadata and a knownCreatedAt, deserializes it, and asserts that both values survive. - After LoadStateAsync with JsonFilePersistenceProvider always throws InvalidOperationException, so saved navigation can never be restored #53 is fixed, a
JsonFilePersistenceProviderround-trip test also asserts that item metadata is kept.
Metadata
Metadata
Assignees
Labels
No labels
Activity
matt-edmondson commentedon Sep 26, 2026
Triage
CreatedAtare dropped without any error on every JSON round-trip. That breaks the documented save-on-exit/restore-on-start flow. It is not High only because no round-trip can succeed today until LoadStateAsync with JsonFilePersistenceProvider always throws InvalidOperationException, so saved navigation can never be restored #53 is fixed; once LoadStateAsync with JsonFilePersistenceProvider always throws InvalidOperationException, so saved navigation can never be restored #53 lands, this becomes the next visible failure.Navigation/Models/NavigationItem.csserialization shape, andJsonFilePersistenceProviderround-trips. Maintainer: @matt-edmondson.LoadStateAsyncalways throws), Saving then loading restores the wrong position when the current page also appears earlier in history #54 (a load restores the wrong position when the current page repeats in history) and Undo after GoBack, Clear or LoadStateAsync drops the newest page and jumps to an unrelated state #55 (undo afterLoadStateAsyncjumps to an unrelated state). Each is a separate defect.Next steps: Fix #53 first, or in the same PR, because the second acceptance criterion depends on it. For the
[JsonConstructor], keep it additive. A new constructor overload does not break existing callers, so no[major]is needed unless you choose to narrow metadata tostring, which would break the public API. If you keepobjectvalues, document that they deserialize asJsonElement. Consider one round-trip test fixture shared across #53–#56, so that all four persistence bugs are guarded by one suite.Generated by Claude Code