Skip to content

Reject a loaded state with no current item but items, or any invalid state from a custom provider [patch] - #86

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/navigation-69-validate-loaded-state
Oct 14, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/navigation-69-validate-loaded-state

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #69

What was wrong

  • NavigationState<T> accepted currentIndex: -1 alongside a non-empty items.
  • Navigation<T>.LoadStateAsync passed whatever INavigationState<T> the provider returned straight to the stack. A custom provider's own state type skipped even the null-item and range checks in NavigationState's constructor.

After such a load the stack had items but no current page. The next NavigateTo treated every restored item as forward history and deleted it, and SaveStateAsync then saved the corrupt state again.

The change

  • The NavigationState<T> constructor now throws ArgumentOutOfRangeException for -1 when there are items. JsonFilePersistenceProvider already turns an ArgumentException into a null load.

  • LoadStateAsync copies the loaded items once and checks them itself, whichever provider they came from:

    • no item is null
    • -1 <= CurrentIndex < Items.Count
    • -1 only when there are no items

    If the state is invalid (including a null Items), it returns false and the stack, its undo history and its events are untouched.

Tests

The new file Navigation.Test/LoadedStateValidationTests.cs covers:

  • the constructor rejecting -1 with items, and accepting it with none
  • the issue's JSON file (currentIndex: -1 with two items), which loads as false and leaves the existing stack alone
  • a custom provider returning an index of -1 with items, past the end, or below -1, or a null item. Each load returns false with the stack unchanged
  • a custom provider with a valid state, which still loads

With the source changes reverted, 6 of the 8 new tests fail; the two that pass are the valid-state cases. With the change, all 54 tests pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_012PhWKjgHTCiWEe29MuG6G3


Generated by Claude Code

…state from a custom provider [patch]

NavigationState accepted currentIndex -1 alongside items, and
LoadStateAsync trusted whatever INavigationState a provider returned. A
stack loaded that way had items but no current page, so the next
NavigateTo treated them all as forward history and deleted them, and the
corrupt state then saved again.

NavigationState now rejects -1 when there are items, and LoadStateAsync
checks every loaded state itself (no null items, the index names an item,
-1 only when empty), returning false and leaving the stack unchanged when
it is invalid.

Fixes #69

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012PhWKjgHTCiWEe29MuG6G3
@sonarqubecloud

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A loaded state with currentIndex: -1 but non-empty items is accepted, and the next NavigateTo silently deletes the whole restored history

2 participants