Skip to content

fix: give every range shape its own Parse and TryParse, so minimal APIs bind it - #177

Merged
CaffeinatedCoder merged 3 commits into
mainfrom
fix/variant-tryparse
Oct 18, 2026
Merged

CaffeinatedCoder merged 3 commits into
mainfrom
fix/variant-tryparse

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #116

Stacked on #175 (base fix/sub-microsecond-literal), which is stacked on #173 → #171.

Problem

A minimal-API parameter typed as one shape, such as Int32Range.Finite or DateRange.UnboundedEnd, bound null for a valid literal and the handler answered 200, even when the parameter wasn't nullable. A literal of another shape did the same. The shapes had no TryParse of their own. The binder found the union's inherited TryParse, which succeeded, and then couldn't assign the union-typed result to the parameter. HostCultureProbeTests measured it for all 11 range types × 5 shapes × {query, nullable query, route value, form field} × 4 request cultures. MVC and JSON bodies were correct.

Change

  • src/CodoMetis.ValueRanges/Internals/RangeFormat.cs: ParseVariant / TryParseVariant read a literal as one shape, returning FormatException / false for another. RangeVariantTypeConverter now uses ParseVariant, so the shape check lives in one place.
  • src/CodoMetis.ValueRanges/RangeVariantParsing.cs and src/CodoMetis.ValueRanges.NodaTime/RangeVariantParsing.cs: every shape of the 11 range types gets static new TShape Parse(string, IFormatProvider?) and static bool TryParse(string?, IFormatProvider?, out TShape?). To keep the 110 members in one file per package, the range records and their shapes are now partial, which doesn't open the closed hierarchy because the constructors stay private. The span overloads stay the union's.
  • Docs: docs/serialization.md#bind-a-range-from-configuration (claim J38 covers the new surface), a docs/migration.md v9.x section, and changelog lines under Breaking (400 instead of 200 + null) and Added (root, core, NodaTime).
  • test/oracles/candidates.json: the 11 <Type> variant minimal TryParse rows (Minimal API binds a variant-typed parameter as null for a valid literal and answers 200 #116) are removed.
  • Seeds: variant-without-tryparse.patch (new). variant-without-json-converter.patch and variant-without-type-converter.patch get the partial context line.

Tests

  • HostCultureProbeTests is the oracle. All 11 rows pass with the candidates removed, and they failed as "candidate fixed" before the removal.
  • RangeParseFormatTests.ShapeParse_ReadsOnlyItsOwnShape: right shape, wrong shape (message), garbage, null, all five shapes, and the union overload still reachable through a shape's name.
  • Proven by revert: removing Int32Range.Finite.TryParse (the committed seed) fails the Int32Range row with answered 200 having bound null for [-5,9] under every culture.
  • dotnet test: every project passes, both integration suites included (Docker, postgres:17).

#115 (range side)

#166 already covered it. DecimalRange.ParseValue goes through DecimalLiteral.Parse (exact or refused), and every range entry point reaches it: Parse, TryParse, JSON, the TypeConverter and the EF JSON document. No other decimal.Parse remains on a range path in src/, and the literal corpus probe shows no decimal rows. Nothing to change here.

Value-semantics review (whole stack)

Run over #171, #173, #175 and this PR, with nothing found to fix:

  • The inverted-bound check compares bounds the way PostgreSQL does: instants for timestamptz, wall clocks for timestamp, and equal-value decimals with different scale are not inverted.
  • No doc promised that an inverted literal (an overnight TimeRange, for example) parses to empty.
  • partial keeps the unions closed.
  • Residual risk: the corpus probe now treats the sub-microsecond family as documented, so the exact three values rest on DomainBoundaryDivergenceTests and the S08 round-trip unit tests.

🤖 Generated with Claude Code

…Is bind it

A minimal-API parameter typed as one shape (Int32Range.Finite, …) found
the union's inherited TryParse, which succeeded, and could not assign
its result to the parameter: a valid literal bound null and the handler
ran with 200, a literal of another shape too. MVC was right, through the
shape's TypeConverter.

Every shape of the 11 range types now has a static Parse returning the
shape and TryParse(string?, IFormatProvider?, out shape), both through
RangeFormat.ParseVariant/TryParseVariant, which the shapes'
TypeConverter now uses as well. The records are partial so the 55
shapes' members live in one RangeVariantParsing.cs per package.

The 11 '<Type> variant minimal TryParse ' candidates (#116) go; claim
J38 covers the new surface; the seed variant-without-tryparse.patch
removes Int32Range.Finite's TryParse and fails HostCultureProbeTests
on every source and culture. Two TimeRange seeds get the partial
context.

Closes #116

Found-By: OT
Defect-Class: JSON

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 bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) package/nodatime Affects CodoMetis.ValueRanges.NodaTime area/literals Parse, TryParse and formatting package/core Affects CodoMetis.ValueRanges severity/low Cosmetic, message or documentation-level difference stacked Base is another open PR; merge in order, CI does not run until it targets main labels Oct 18, 2026
# Conflicts:
#	docs/migration.md
#	test/oracles/candidates.json
Base automatically changed from fix/sub-microsecond-literal to main October 18, 2026 14:44
@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 935c42e into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/variant-tryparse branch October 18, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/literals Parse, TryParse and formatting bug Something isn't working found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) package/core Affects CodoMetis.ValueRanges package/nodatime Affects CodoMetis.ValueRanges.NodaTime severity/low Cosmetic, message or documentation-level difference 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.

Minimal API binds a variant-typed parameter as null for a valid literal and answers 200

1 participant