Skip to content

Leave the writer empty when Newtonsoft TrySerialize fails [patch] - #157

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/154-newtonsoft-buffer-output
Nov 4, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/154-newtonsoft-buffer-output

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #154

What changed

NewtonsoftJsonSerializationProvider.TrySerialize streamed straight into the caller's TextWriter. When serialization failed partway (e.g. a self-referencing object), disposing the JsonTextWriter auto-closed the open objects, so the caller was left holding JSON that parses cleanly but is missing data while the method returned false.

It now serializes into a local StringWriter and copies the result to the caller's writer only on success, matching the System.Text.Json provider. The caller's writer is never wrapped, so it also can no longer be closed by the JSON writer.

Tests

  • New TrySerialize_CyclicObject_ReturnsFalseAndWritesNothing asserts both the false return and an empty writer. It fails on main (writer holds {"Name":"a"}) and passes with the fix.
  • Full Essentials.Tests suite on net10.0: 1086 passed.

Companion issue #155 (depth > 64) is left for its own PR; with this buffering in place, a depth failure there will also leave the writer empty.


Generated by Claude Code

Serialize into a local buffer and copy it to the caller's writer only on
success. Disposing the JsonTextWriter auto-closes open objects, so a
failure partway through left well-formed, truncated JSON behind.

Fixes #154

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SETCEsuMvZFcF6qXRguNds
…ffer-output

# Conflicts:
#	Essentials.SerializationProviders.NewtonsoftJson/NewtonsoftJsonSerializationProvider.cs
#	Essentials.Tests/NewtonsoftJsonSerializationProviderTests.cs

Copy link
Copy Markdown
Contributor Author

SonarCloud Code Analysis failed with 0.0% coverage on new code. The cause is missing coverage data, not untested code: SonarCloud also reports the whole project at 0.0% for this analysis, which means no coverage report reached it.

Why: in run 39351070586 the Ubuntu and Windows test jobs ran on 2026-10-20, and the macOS job was re-run on 2026-11-04. The coverage artifacts are uploaded with retention-days: 7, so the Ubuntu and Windows ones had expired by the time today's Analyze & Release job went to download them.

The three uncovered lines it names are the buffer setup and the final write in TrySerialize. Every TrySerialize test runs through them; the PR's head has already passed the full suite (1107 tests), locally and on all three CI platforms.

Fix: re-run the whole workflow, not just the failed jobs, so the tests and coverage are produced together. I'll start that once the current Analyze & Release job finishes.


Generated by Claude Code

@sonarqubecloud

sonarqubecloud Bot commented Nov 4, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 7b36a65 into main Nov 4, 2026
32 of 33 checks passed
@matt-edmondson
matt-edmondson deleted the fix/154-newtonsoft-buffer-output branch November 4, 2026 03:26
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.

NewtonsoftJsonSerializationProvider.TrySerialize returns false but leaves well-formed, truncated JSON in the caller's writer

2 participants