Skip to content

fix: check that a wrapper set element's text form reads back as the same element - #165

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/wrapper-round-trip
Oct 18, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/wrapper-round-trip

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #53

Stacked on #164 (fix/wrapper-json-element-tokens) → #163 → #162; the diff against main includes them until they merge.

Problem

A wrapper set writes an element as its IFormattable text and reads it back through IParsable, and nothing checked that the two are inverses. The numeric and temporal arities refused only decoration their primitive grammar could not parse, so a wrong value that still parsed went through, at every persistence boundary:

Element Wrote Read back
Int32Set<OffByOne> (ToString → V + 1, Parse → V) {124} for 123 124, Equals false, no exception
StringSet<Lenient> (ToString → CUST-{v}, Parse keeps any text) {CUST-123} CUST-CUST-123, growing on every round trip
StringSet<Decorated> (Parse accepts digits only) {CUST-123} FormatException from the element, not the contract

The user decision on the issue: the check applies to every core wrapper arity, on write and on read.

Change

  • src/CodoMetis.ValueRanges/Internals/WrapperElement.cs (new): the check, once. Formatted (write: InvalidOperationException naming the element type, its text and the contract) and Parsed (read: FormatException, so TryParse answers false). It compares the element, Parse(Format(x)) == x, not the text: a text-level check would refuse an element that normalizes on parse (trims, lowercases — the documented AccessRight), while a lenient parse of a decorated text still fails because the element it yields formats as something else. That is the one deviation from the issue's wording of the read rule; the reason is in the class remarks and the migration guide.
  • Every core arity (StringSet<T>, GuidSet<T>, Int16/32/64Set<T>, DecimalSet<T>, DateSet<T>, TimeSet<T>, DateTimeSet<T>, DateTimeOffsetSet<T>): ParseValue and FormatValue (for the wire form; an explicit display format is not checked) go through it, via an unchecked ParseElement so the check cannot recurse. The non-temporal arities now name RoundTripFormat = null as the temporal ones name theirs.
  • ValueSetElementJsonConverter.cs: the four element converters surface the write refusal as JsonException (ValueSetElementJson.Format).
  • EF Core: every core family's bridge sets requireRoundTrip (the Instant and LocalTime NodaTime bridges too, for consistency; the calendar-bearing three already had it), the string bridge holds the rule itself, and FromPrimitive goes through the family's ParseValue, so a column holding text the element reads as another value throws on materialization. A read-leg refusal inside the round trip is re-raised as the contract (ArgumentException). The bridge keeps formatting the element itself rather than through FormatValue: a DateTime element with Kind.Local at a wall clock the zone skips carries the zone's offset in its "O" text, which the element's BCL Parse applies through the zone, so the literal cannot round-trip it while the store, which drops the offset, can (the 9.0.0 fix SqlLiteralTests.WrapperTimestampSet_LocalElement_KeepsAWallClockTheZoneSkips still holds). The migration guide names that case.
  • Oracles: the five DecorativeElementContractTests rows for Wrapper set arities don't check that an element's text form round-trips #53 leave candidates.json; two new seeds (string-wrapper-text-form-not-round-tripped.patch, integer-wrapper-text-form-not-round-tripped.patch) remove the check for one arity each; bridge-swallows-unparsable-element.patch and timeset-wrapper-unpinned.patch are refreshed against the new shape. Clone waivers follow the renamed members.
  • Docs: docs/value-sets.md#keep-the-text-form-of-the-underlying-type (what is checked, where, what normalizing means, what 9.x did), claim S19-text-form-round-trip-checked, the 10.0.0 Breaking row, and "Check that your element's text form round-trips" in docs/migration.md.

Tests

  • SetElementBridgeTests: WrapperIntSet_ElementWhoseTextIsAnotherNumber_IsRefusedOnWriteAndOnRead, WrapperStringSet_ElementKeepingItsDecoration_IsRefusedOnWriteAndOnRead, WrapperStringSet_ElementNormalizingOnParse_StillRoundTrips, and the lossy-stamp test now asserts the refusal on write too. SqlLiteralTests.WrapperSets_ElementWhoseTextDoesNotReadBackAsItself_AreRejected holds the bridge for the integer and string families.
  • DecorativeElementContractTests holds all five rows without candidates.
  • Fix reverted (src/ from the parent commit, tests kept): the three new core tests and the lossy-stamp one fail; DecorativeElementContractTests reports Int32Set<OffByOne>: literal round trip changed the set → wrote {124}, read back [125], StringSet<Lenient> … read back [CUST-CUST-123] and the three StringSet<DecoratedText> boundaries accepting; the SQL literal test fails. Restored afterwards.
  • Full runs: core 984, conventions 324, EF translation 246, NodaTime 196, EF NodaTime 81, AspNetCore 84, integration (whole project, postgres:17) 209 — green. Clone sweep: 65 waived, 0 unwaived, 0 stale. The seed prover (dotnet run tools/oracles.cs seeds) for the four touched seeds: result in a comment below.

🤖 Generated with Claude Code

@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 18, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred breaking Changes an answer or a signature — needs a major found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) area/value-sets Value set families and wrapper arities package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/core Affects CodoMetis.ValueRanges package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime severity/high Wrong results, crash or failed writes in a realistic scenario stacked Base is another open PR; merge in order, CI does not run until it targets main labels Oct 18, 2026
@CaffeinatedCoder

Copy link
Copy Markdown
Owner Author

Seed prover (dotnet run tools/oracles.cs seeds) on this branch, for the four seeds the PR adds or refreshes:

CAUGHT  integer-wrapper-text-form-not-round-tripped.patch → DecorativeElementContractTests: 1 of 4 tests fail, e.g. IntegerArity_ReadsBackTheSetItWrote_WhenTheElementTextFormDoesNotRoundTrip
CAUGHT  string-wrapper-text-form-not-round-tripped.patch → DecorativeElementContractTests: 2 of 4 tests fail, e.g. StringArity_RefusesADecoratedElement_AtEveryWriteBoundary
CAUGHT  bridge-swallows-unparsable-element.patch → DecorativeElementContractTests: 1 of 4 tests fail, e.g. IntegerArity_RefusesADecoratedElement_AtEveryWriteBoundary
CAUGHT  timeset-wrapper-unpinned.patch → WrapperGrammarContractTests: 1 of 1 tests fail
CAUGHT  timeset-wrapper-unpinned.patch → WrapperArityIntegrationTests: 6 of 9 tests fail

…ame element

A wrapper set writes an element as its IFormattable text and reads it
back through IParsable, and nothing checked that the two are inverses.
The numeric and temporal arities refused only decoration their primitive
grammar could not parse, so Int32Set<T> over an element whose ToString
gives V + 1 wrote {124} for 123 and read it back as 124, and StringSet<T>
over an element that decorates on write and keeps any text on read wrote
{CUST-123} and read back CUST-CUST-123, growing on every round trip.
Neither threw. The same text reached JSON, the SQL literal and the bound
parameter.

WrapperElement holds the check once, on the element: Parse(Format(x))
must equal x. Every core arity runs it in FormatValue (the literal, JSON,
InvalidOperationException naming the contract) and in ParseValue (Parse,
TryParse, JSON, the TypeConverter, FormatException), through an unchecked
ParseElement so the check cannot recurse. The element rather than the
text is compared so that an element that normalizes on parse — trims or
lowercases, as the documented AccessRight does — still passes, while a
lenient parse of a decorated text fails because it formats as something
else. The JSON element converters surface the write refusal as
JsonException.

The EF Core bridge checks the same contract through the store primitive:
every core family sets requireRoundTrip (the Instant and LocalTime
NodaTime families too, for consistency), the string bridge holds the rule
itself, and FromPrimitive goes through the family's ParseValue, so a
column holding text the element reads as another value throws on
materialization. The bridge keeps formatting the element itself rather
than through FormatValue: a DateTime element with Kind Local at a wall
clock the zone skips carries the zone's offset in its "O" text, which
the element's BCL Parse applies through the zone, so the literal cannot
round-trip it while the store, which drops the offset, can (9.0.0).

Closes #53

Found-By: OT
Defect-Class: STORE

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Base automatically changed from fix/wrapper-json-element-tokens to main October 18, 2026 13:19
@CaffeinatedCoder CaffeinatedCoder removed the stacked Base is another open PR; merge in order, CI does not run until it targets main label Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit ffe28a7 into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/wrapper-round-trip branch October 18, 2026 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/value-sets Value set families and wrapper arities breaking Changes an answer or a signature — needs a major bug Something isn't working found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) package/core Affects CodoMetis.ValueRanges package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime severity/high Wrong results, crash or failed writes in a realistic scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrapper set arities don't check that an element's text form round-trips

1 participant