Skip to content

fix: refuse an inverted range literal, as PostgreSQL's range_in does - #171

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/inverted-range-literal
Oct 18, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/inverted-range-literal

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #114

Problem

A range literal whose lower bound is greater than its upper bound parsed as empty on every entry point, where PostgreSQL's range_in rejects the text (range lower bound must be less than or equal to range upper bound). RangeFormat.Parse handed the two parsed bounds to CreateFinite, which applies the factory's rule for computed bounds. So Int32Range.Parse("[5,1)") was empty, TryParse returned true, JSON and the TypeConverter read empty, a minimal API answered 200 with an empty range, and RangeSet.Parse("{[5,1),[7,9]}") was {[7,9]}: the inverted element vanished. A swapped period in a query string or a JSON body silently matched nothing.

Change

  • src/CodoMetis.ValueRanges/Internals/RangeFormat.cs: a finite literal goes through Finite, which compares the bounds as written (before a discrete type closes them) and throws FormatException when lower > upper. Every entry point reads the literal through RangeFormat.Parse, so TryParse returns false, JSON throws JsonException, the TypeConverter throws, and RangeSet.Parse throws. Equal bounds that aren't both inclusive ([5,5), (5,5)) and closing bounds ((1,2) over integers) stay empty, as on the server. CreateFinite keeps its documented rule (inverted → Empty), as the issue asks.
  • Docs: docs/serialization.md#parse-a-literal states the rule (new claim J35 quotes it), docs/ranges.md points from the factory rule to it, docs/migration.md has the v9.x entry, and the root changelog has the Breaking line.
  • src/CodoMetis.ValueRanges/CHANGELOG.md: its 10.0.0 section said "No change in this package" although the five value-set fixes merged today (fix: read GuidSet elements with PostgreSQL's uuid_in grammar #162–fix: read decimal text exactly or refuse it #166) are core changes. It now carries those five lines and this one, copied from the root.
  • test/oracles/candidates.json: the 20 {type} inverted rows (An inverted range literal parses as empty on every entry point; PostgreSQL rejects it #114) are removed. They failed as "candidate fixed" with the fix in place.
  • test/oracles/seeds/inverted-literal-reads-empty.patch: the fix reverted, naming LiteralCorpusProbeTests and LiteralGrammarParityTests.

Tests

  • RangeParseFormatTests: inverted literals through Parse (string and span), TryParse (string and span), JSON (union and Finite), the TypeConverter and RangeSet.TryParse; temporal ([infinity,2026-01-01), (2026-01-01,-infinity]) and continuous inverted literals; equal and closing bounds stay empty; CreateFinite(5, 1) stays Empty.
  • LiteralGrammarParityTests (integration): [5,1), (5,1], [2,1), the equal-bound forms, inverted daterange literals and {[5,1),[7,9]} are asked of PostgreSQL 17.
  • LiteralCorpusProbeTests (integration): the inverted family passes for all 10 range and 10 multirange types.
  • Proven by revert: with the check removed, 8 RangeParseFormatTests cases fail, LiteralGrammarParityTests reports int4range [5,1): library empty, PostgreSQL refused and int4multirange {[5,1),[7,9]}: library {[7,9]}, and the corpus probe reports library empty, PostgreSQL refuses for every type. That revert is the committed seed.
  • dotnet test: every project passes, both integration suites included (Docker, postgres:17).

🤖 Generated with Claude Code

A finite literal whose lower bound is greater than its upper bound
went through CreateFinite, whose rule for computed bounds makes it
empty. So [5,1) parsed as empty on every entry point (Parse, TryParse,
JSON, TypeConverter, the EF JSON document reader) and an inverted
element vanished from a multirange literal, where PostgreSQL refuses the
text. RangeFormat now compares the bounds as written, before a discrete
type closes them, and throws FormatException. Equal bounds that are not
both inclusive stay empty, as on the server, and CreateFinite keeps its
rule.

Removes the 20 '{type} inverted ' candidates (#114), adds claim J35
and the seed inverted-literal-reads-empty.patch, inverted literals in
LiteralGrammarParityTests, and fills the core package changelog's
10.0.0 section, which still said "No change".

Closes #114

Found-By: OT
Defect-Class: LIT

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…teral

# Conflicts:
#	src/CodoMetis.ValueRanges/CHANGELOG.md
@CaffeinatedCoder
CaffeinatedCoder merged commit 5542a03 into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/inverted-range-literal branch October 18, 2026 14:35
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 area/literals Parse, TryParse and formatting 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 severity/medium Inconsistency or failure in a narrow 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.

An inverted range literal parses as empty on every entry point; PostgreSQL rejects it

1 participant