Repository navigation
fix: check that a wrapper set element's text form reads back as the same element - #165
Merged
Merged
Conversation
Owner
Author
|
Seed prover ( |
…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]>
CaffeinatedCoder
force-pushed
the
fix/wrapper-round-trip
branch
from
October 18, 2026 12:49
556e5fa to
2f49aa5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #53
Stacked on #164 (
fix/wrapper-json-element-tokens) → #163 → #162; the diff againstmainincludes them until they merge.Problem
A wrapper set writes an element as its
IFormattabletext and reads it back throughIParsable, 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:Int32Set<OffByOne>(ToString→V + 1,Parse→V){124}for 123Equalsfalse, no exceptionStringSet<Lenient>(ToString→CUST-{v},Parsekeeps any text){CUST-123}CUST-CUST-123, growing on every round tripStringSet<Decorated>(Parseaccepts digits only){CUST-123}FormatExceptionfrom the element, not the contractThe 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:InvalidOperationExceptionnaming the element type, its text and the contract) andParsed(read:FormatException, soTryParseanswersfalse). 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 documentedAccessRight), 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.StringSet<T>,GuidSet<T>,Int16/32/64Set<T>,DecimalSet<T>,DateSet<T>,TimeSet<T>,DateTimeSet<T>,DateTimeOffsetSet<T>):ParseValueandFormatValue(for the wire form; an explicit display format is not checked) go through it, via an uncheckedParseElementso the check cannot recurse. The non-temporal arities now nameRoundTripFormat = nullas the temporal ones name theirs.ValueSetElementJsonConverter.cs: the four element converters surface the write refusal asJsonException(ValueSetElementJson.Format).requireRoundTrip(theInstantandLocalTimeNodaTime bridges too, for consistency; the calendar-bearing three already had it), the string bridge holds the rule itself, andFromPrimitivegoes through the family'sParseValue, 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 throughFormatValue: aDateTimeelement withKind.Localat a wall clock the zone skips carries the zone's offset in its"O"text, which the element's BCLParseapplies through the zone, so the literal cannot round-trip it while the store, which drops the offset, can (the 9.0.0 fixSqlLiteralTests.WrapperTimestampSet_LocalElement_KeepsAWallClockTheZoneSkipsstill holds). The migration guide names that case.DecorativeElementContractTestsrows for Wrapper set arities don't check that an element's text form round-trips #53 leavecandidates.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.patchandtimeset-wrapper-unpinned.patchare refreshed against the new shape. Clone waivers follow the renamed members.docs/value-sets.md#keep-the-text-form-of-the-underlying-type(what is checked, where, what normalizing means, what 9.x did), claimS19-text-form-round-trip-checked, the 10.0.0 Breaking row, and "Check that your element's text form round-trips" indocs/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_AreRejectedholds the bridge for the integer and string families.DecorativeElementContractTestsholds all five rows without candidates.src/from the parent commit, tests kept): the three new core tests and the lossy-stamp one fail;DecorativeElementContractTestsreportsInt32Set<OffByOne>: literal round trip changed the set → wrote {124}, read back [125],StringSet<Lenient> … read back [CUST-CUST-123]and the threeStringSet<DecoratedText>boundaries accepting; the SQL literal test fails. Restored afterwards.dotnet run tools/oracles.cs seeds) for the four touched seeds: result in a comment below.🤖 Generated with Claude Code