Skip to content

Throw from Serialize when TrySerialize fails [minor] - #115

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/70-serialize-throws-on-failure
Oct 15, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/70-serialize-throws-on-failure

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #70

What was wrong

The default ISerializationProvider.Serialize(object) and Serialize(object, TextWriter) ignored the result of TrySerialize. A failed serialization came back as "". FileSystemPersistenceProvider.StoreAsync and TempPersistenceProvider.StoreAsync (and through them ConfigHome and DataHome) wrote that empty string over the last good value, and still reported success.

Change

  • Both Serialize overloads now throw InvalidOperationException when TrySerialize returns false. This is how Encode, Hash and Decrypt already behave. Serialize(object) goes through the TextWriter overload, so the check lives in one place, and SerializeAsync inherits it.
  • No change was needed in the persistence providers. Their existing catch in StoreAsync wraps the exception in PersistenceProviderException before any file is written, so the previous value is kept.
  • Serialize now throws where it used to return "", which is a public behaviour change, so the commit carries [minor].

Tests

  • Serialize_Throws_When_TrySerialize_Fails: both overloads throw for a cyclic object graph.
  • Store_Of_An_Unserializable_Value_Throws_And_Keeps_The_Previous_Value: on the FileSystem and Temp providers, store a good value, then try to store a cyclic one. StoreAsync throws PersistenceProviderException, and RetrieveAsync still returns the good value.

Both tests fail on main ("no exception was thrown") and pass with this change. The full Essentials.Tests suite passes on net10.0 (1037 tests), and Essentials.csproj builds for every target framework with no warnings.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YLmoTmY5N7amW8Z9jazuGV


Generated by Claude Code

ISerializationProvider.Serialize(object) and Serialize(object, TextWriter)
discarded TrySerialize's false, so a failed serialization returned "".
The file-backed persistence providers wrote that empty string over the
last good value and StoreAsync reported success.

Both now throw InvalidOperationException, as Encode, Hash and Decrypt
already do. StoreAsync's existing catch wraps it in
PersistenceProviderException before any file is touched, so the stored
value survives. This changes the public behaviour of Serialize, hence
the minor bump.

Fixes #70

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 31b69fe into main Oct 15, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/70-serialize-throws-on-failure branch October 15, 2026 03:32
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 failed serialization makes StoreAsync overwrite the stored value with an empty file and report success

2 participants