Skip to content

NavigationItem loses Metadata and CreatedAt on a JSON save/load round-trip #56

Closed

Description

@matt-edmondson

What's wrong

NavigationItem (Navigation/Models/NavigationItem.cs) exposes its state in ways System.Text.Json cannot write back into:

  • Metadata is a computed, get-only property (=> new ReadOnlyDictionary<string, object>(_metadata)) over a private field. The only way to fill it is SetMetadata. On deserialization STJ writes the Metadata object to the JSON, then on the way back in finds nothing it can set, so it drops the value without an error.
  • CreatedAt is get-only and is set to DateTime.UtcNow in 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 takes id, displayName, createdAt, and IReadOnlyDictionary<string, object>? metadata, and copies the metadata into _metadata. Its parameter names and types have to match the properties.
  • Metadata values come back as JsonElement rather than their original CLR types. Either say so in the docs, or narrow metadata to string values, so that callers are not surprised.

Acceptance criteria

Activity

  1. matt-edmondson commented on Sep 26, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    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 to string, which would break the public API. If you keep object values, document that they deserialize as JsonElement. 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

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions