Skip to content

Leave records whose only ToString is compiler-generated to System.Text.Json [patch] - #136

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/135-record-tostring
Nov 11, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/135-record-tostring

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #135

Problem

OverridesToString counted the ToString() the compiler synthesizes for every record and record struct as a user override. So a value object like record Email(string Value) with a static Create was claimed by the factory and written as "Email { Value = a@b.c }". Reading that back passed the whole debug string to Create, which nested it again on every save/load cycle. JSON written before the factory was registered ({"Value":"a@b.c"}) could no longer be read.

Change

  • A ToString() marked [CompilerGenerated] no longer counts as an override, so those records go back to System.Text.Json's default object serialization.
  • Records that declare their own ToString(), or inherit a sealed one (the SemanticString<T> shape), are still claimed. In both cases the compiler synthesizes nothing.
  • The README section on ToString overrides now mentions records.

I didn't walk up to the base type's ToString as the issue suggested. A record deriving from a base with a non-sealed ToString gets its own synthesized ToString, and at runtime that override prints the debug form. Walking up would claim the type and still write Derived { ... }.

Tests

ToStringOverrideTests gains:

  • a positional record with Create
  • a record struct with Parse
  • a record that declares ToString
  • a record that inherits a sealed ToString

The four tests for the compiler-generated cases fail on main and pass with the fix. All 152 tests pass, including NetStandardAssetTests and the SemanticString<T> tests.


Generated by Claude Code

…t.Json [patch]

OverridesToString counted the ToString the compiler synthesizes for every
record and record struct as a user override, so a value object such as
`record Email(string Value)` with a static Create was claimed by the
factory and written in its debug form "Email { Value = a@b.c }". Reading
that back passed the whole debug string to Create, nesting it on every
save/load cycle, and JSON written before the factory was registered could
no longer be read.

A ToString marked [CompilerGenerated] no longer counts as an override.
Records that declare their own ToString, or inherit a sealed one, are
still claimed, since the compiler then synthesizes nothing.

Fixes #135

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3mca4cGmHKaFu4Yui
@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 record with a static Create/Parse but no ToString of its own is written as "Email { Value = a@b.c }", and reading it back nests that text in the value

2 participants