Skip to content

Handle LoadOrCreate deserialization of JSON null via existing recovery path - #296

Merged
matt-edmondson merged 2 commits into
mainfrom
copilot/fix-nullreferenceexception-load-or-create
Sep 13, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
copilot/fix-nullreferenceexception-load-or-create

Conversation

Copilot AI commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

AppData<T>.LoadOrCreate(...) could throw NullReferenceException when the settings file contained literal JSON null because Deserialize<T> returned null without throwing. This change treats that case as recoverable corruption so the existing backup/new-instance flow is used consistently.

  • Deserialization null handling

    • Updated LoadOrCreate to convert a null deserialization result into JsonException, keeping control flow inside the existing corruption recovery logic.
  • Regression coverage

    • Added a focused test for a settings file containing exactly null, asserting LoadOrCreate() recovers to a valid default instance rather than crashing.
newAppData = JsonSerializer.Deserialize<T>(jsonString, AppData.JsonSerializerOptions)
	?? throw new JsonException("Deserialized settings file to null.");

Co-authored-by: matt-edmondson <19528727+matt-edmondson@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NullReferenceException in LoadOrCreate method Handle LoadOrCreate deserialization of JSON null via existing recovery path Sep 13, 2026
@matt-edmondson
matt-edmondson marked this pull request as ready for review September 13, 2026 12:05
@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.

LoadOrCreate throws NullReferenceException instead of recovering when the settings file contains JSON null

2 participants