Skip to content

test: round-trip every type in a JSON column through a compiled model - #211

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
test/compiled-model-json
Oct 18, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
test/compiled-model-json

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #43

Problem

The JSON-column readers and writers point ConstructorExpression at a static Instance "so compiled models can use them". That was checked once, by hand, with dotnet ef dbcontext optimize in the 9.0.0 review, and nothing has guarded it since.

Change

New CompiledModelJsonTests in the integration project:

  • The context. It holds every discovered range, multirange and value set type inside a jsonb document, one table each: 52 types today, with a floor of 48. Types come from FuzzTypes, so a new type is covered without an edit.

  • The compiled model. It is generated in-process the way dotnet ef dbcontext optimize does it (DesignTimeServicesBuilder, then the C# compiled-model generator), compiled with Roslyn against the loaded assemblies, loaded, and passed to UseModel.

  • The comparison runs on a live server, with the runtime model as the oracle:

    • each model's JSON reader/writer type;
    • the document text each model writes for the same value;
    • the value the compiled model reads back, from its own row and from a row the runtime model wrote.

    That's 148 values, and all agree.

  • The values: a range's five shapes, a multirange's empty, five-shape and gapped sets, and a set's empty and probe sets.

  • Microsoft.EntityFrameworkCore.Design 10.0.0 is a new test-only reference; it brings the generator and Roslyn. The test's types live in a namespace of their own, because the generated code imports every namespace it names and this assembly has two AuditStamp and two BillingMonth types. The EF1001 pragma is file-level, with its reason.

What I found: #209 (opened under #58)

  • The standard compiled model carries no type mappings. It resolves them through the plugin at run time, by CLR type, exactly as the runtime model does. So it never reads ConstructorExpression.
  • Only the Native AOT form emits mappings, and with them the reader/writers (dbcontext optimize --nativeaot, which --precompile-queries needs).
  • That form can't be generated today. It throws The type mapping used is incompatible with a compiled model. The mapping type must have a 'public static readonly ValueRangeTypeMapping<DateRange, DateOnly> … Default' property.

So the public Instance design has never run. I wrote two ConstructorExpression seeds: one the generated code couldn't construct, and one pointing YearMonth at a sibling's Instance. oracles.cs seeds showed both MISSED by this test, as expected, so I didn't keep them or register the test as an oracle. They are in #209's body, for when the Native AOT form works.

Tests

  • The test passes against the suite's PostgreSQL 17: 52 types, 148 values.
  • Shown to fail:
  • What it can't catch: a plugin defect that breaks both models alike only fails here when it throws; this is a differential against the runtime model. A compiled-model-only defect in the registries would need a fresh process, because the runtime model warms the static registries first. This test doesn't run one.
  • Full integration suite 238 passed; conventions 324 passed.

🤖 Generated with Claude Code

CompiledModelJsonTests generates the compiled model `dotnet ef dbcontext optimize` writes for a
context holding every discovered range, multirange and value set type inside a jsonb document
(52 types today, floor 48), compiles it with Roslyn in-process, loads it, and holds it to the
runtime model on a live server: the reader/writer each model uses, the document text each writes,
and the value the compiled model reads from its own row and from a runtime-written one. 148 values,
all agree. Microsoft.EntityFrameworkCore.Design (test-only) brings the generator and Roslyn.

The standard compiled model carries no type mappings and resolves them through the plugin at run
time, so the JSON reader/writers' ConstructorExpression is never consulted; only the Native AOT form
is, and generating that fails because the plugin's mappings have no static Default (#209, opened
from this work). A reader/writer that throws for one type fails the test ("TimeRange: saving failed").

Closes #43

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 18, 2026
@CaffeinatedCoder CaffeinatedCoder added tests Adds or changes tests only (oracles, harnesses); no library code package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL area/json System.Text.Json and TypeConverter wire forms labels Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/json System.Text.Json and TypeConverter wire forms package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL tests Adds or changes tests only (oracles, harnesses); no library code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test JSON-column readers and writers through a compiled model

1 participant