Skip to content

fix: a declaration's strings are written by one fixed rule, so its key is the same on every runtime - #53

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/49-canonical-escaping
Nov 11, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/49-canonical-escaping

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #49

Problem

The identity an unnamed declaration's key is hashed over, and the stored value, were written through JavaScriptEncoder.UnsafeRelaxedJsonEscaping. That encoder escapes every code point its runtime does not know as assigned, and each Unicode version assigns more: .NET 9 escaped 22 BMP characters that .NET 10 writes as themselves. One declaration therefore had two keys, depending on the runtime that scaffolded. The key sits in consumers' snapshots and is permanent from 11.0.0.

A consumer would have seen, after a runtime major: the snapshot rewritten for declarations nobody changed, another order from GetDeclarations, and a changed count from Redeclare.

Change

  • Declarations/Internal/CanonicalJson.cs (new) writes every string of a stored declaration by one rule that reads no table: ", \ and everything below U+0020 are escaped (\b \t \n \f \r, otherwise \u and four upper-case hex digits), and so are U+0085, U+2028 and U+2029. Everything else is written as itself.
  • SchemaJsonWriter (String, OptionalString, Strings) and DeclarationCodec (the kind and the name) write through it.
  • A string that is not well-formed UTF-16 is refused. Half a surrogate pair was stored as U+FFFD, so two different strings became one declaration.
  • A property name is ASCII letters, digits and _. Utf8JsonWriter has no raw form for a name, so names still go through its encoder; these are the characters no encoder escapes. This narrows the experimental contract; every name in the repository complies.
  • CLAUDE.md has the rule.

Why three characters more than JSON asks for. With U+2028 written as itself, the snapshot EF Core scaffolds does not compile (CS1010, newline in constant): EF Core writes an annotation's value into a C# string literal and does not escape U+0085, U+2028 or U+2029, which end a line in C#. The old encoder happened to escape them. SnapshotRoundTripTests found it once its declaration had such a character.

Keys of declarations made of ASCII do not move. Both golden keys in DeclarationStoreTests are unchanged.

Tests

Kernel tests without a server: 252 before, 263 after. Probe project: 125, unchanged. The two PostgreSQL integration tests did not run locally (SCHEMAKIT_TEST_PG not set); CI runs them.

  • Across runtimes. A console app compiling CanonicalJson.cs wrote every BMP character and one outside it on .NET 9.0.23, 10.0.15 and 11.0.0: the bytes are identical (SHA-256 a1669ffa0cbcaadc… on all three) and read back as the string that was written.
  • A golden key for a declaration with U+A7DB (escaped by .NET 9, not by 10), U+0378 (never assigned) and a character outside the BMP is held verbatim, and derived from the documented identity string beside it. The literal was computed independently with shasum.
  • Every character of the BMP is written as itself on each path a string takes: the three writer methods, the kind and the name.
  • The snapshot round trip now carries U+0001, U+007F, U+2028, U+0378, U+A7DB and U+1F600 through a snapshot compiled with Roslyn, on Npgsql, SQL Server and SQLite.

Each part was undone in turn and the tests run:

Undone Fails
String through the runtime's encoder Every_character_is_written_as_itself_…, A_key_does_not_depend_on_what_the_runtime_knows_of_a_character, A_string_that_is_not_well_formed_is_refused
OptionalString; Strings; the name Every_character_is_written_as_itself_…, A_string_that_is_not_well_formed_is_refused (each)
The kind Every_character_is_written_as_itself_…
U+0085, U+2028 and U+2029 written as themselves A_string_is_escaped_where_json_requires_it_and_nowhere_else, and the snapshot round trip on all three providers, which no longer compiles
Half a surrogate pair written A_string_that_is_not_well_formed_is_refused
Any property name taken A_property_name_is_made_of_ascii_letters_digits_and_underscores (6 rows)

🤖 Generated with Claude Code

…y is the same on every runtime

Closes #49.

The identity an unnamed declaration's key is hashed over, and the stored
value, went through JavaScriptEncoder.UnsafeRelaxedJsonEscaping, which
escapes every code point its runtime does not know as assigned. .NET 9
escaped 22 characters that .NET 10 writes as themselves, so one declaration
had two keys.

CanonicalJson now writes every string: `"`, `\` and what is below U+0020
are escaped, and so are U+0085, U+2028 and U+2029. Everything else is
written as itself. The last three are not JSON's: EF Core writes the value
into a string literal in the snapshot without escaping them, and they end a
line in C#, so the snapshot did not compile. The round trip through a
compiled snapshot found that.

Also:
- a string that is not well-formed UTF-16 is refused instead of being
  stored as U+FFFD
- a property name is ASCII letters, digits and `_`, since a name still goes
  through the writer's encoder

Keys of declarations made of ASCII do not move; the two golden keys hold.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Nov 11, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) severity/medium Inconsistency or failure in a narrow scenario area/migrations The differ and generator decorators, snapshot format, EnsureCreated and the UseSchemaKit wiring labels Nov 11, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 0948ac3 into main Nov 11, 2026
6 of 7 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/49-canonical-escaping branch November 11, 2026 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/migrations The differ and generator decorators, snapshot format, EnsureCreated and the UseSchemaKit wiring bug Something isn't working package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) severity/medium Inconsistency or failure in a narrow scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An unnamed declaration's key depends on the runtime's Unicode tables

1 participant