Skip to content

Fix RESP3 map parsing for FT.SEARCH/AGGREGATE/INFO (#570) - #571

Merged
slorello89 merged 1 commit into
mainfrom
fix-resp3-search-response
Jun 18, 2026
Merged

slorello89 merged 1 commit into
mainfrom
fix-resp3-search-response

Conversation

@slorello89

@slorello89 slorello89 commented Jun 16, 2026 •

Copy link
Copy Markdown
Member

Problem

Fixes #570. After StackExchange.Redis auto-negotiates RESP3 (the default against RESP3-capable servers in newer SE.Redis versions), RedisCollection<T>.ToListAsync() and other searches throw:

System.InvalidCastException: Could not cast to long
   at Redis.OM.RedisReply.op_Implicit(RedisReply v)
   at Redis.OM.Searching.SearchResponse`1..ctor(RedisReply val)
   ...

Under RESP3, FT.SEARCH/FT.AGGREGATE/FT.INFO reply with a map (total_results/results/extra_attributes) instead of the flat RESP2 array. SE.Redis collapses a map's Resp2Type to Array (flattened, interleaved key/value pairs), so the existing parsers read the first map key ("attributes") as the document count and fail to cast it to long. Forcing Protocol = RedisProtocol.Resp2 is the current workaround.

Fix

Detect the RESP3 map shape at the parser layer and reshape it back into the legacy RESP2 layout, so the existing parse logic runs unchanged:

  • RedisReply records IsMap (Resp3Type == ResultType.Map) and exposes GetMapValueOrDefault(key). The existing flattening behavior is preserved — IsMap is purely additive.
  • SearchResponse.NormalizeReply reshapes a map reply into the legacy [count, id, (score,) fields, …] array (used by both SearchResponse and SearchResponse<T>, including WITHSCORES).
  • AggregationResult.NormalizeReply does the same for FT.AGGREGATE (generic + non-generic, incl. WITHCURSOR).
  • RedisIndexInfoAttribute — RESP3 groups attribute flags (SORTABLE/NOSTEM/INDEXMISSING/INDEXEMPTY) inside a nested flags array rather than as trailing top-level tokens; flag detection now flattens one level so both protocols are handled.
  • RedisUriParser honors a protocol URI query arg (resp2/resp3 or 2/3) and the REDIS_OM_PROTOCOL environment variable. This gives users an explicit protocol selector (and a RESP2 workaround) and lets the whole test suite run under RESP3.

Testing

  • Adds Resp3ResponseTests — deterministic unit tests that feed synthetic RESP3 map replies through the search and aggregation parsers. These guard the fix in CI, which runs RESP2 by default.
  • Ran the full integration suite under both protocols (net6.0, parallelization disabled, against redis/redis-stack-server):
    • RESP2 baseline: 637/0
    • REDIS_OM_PROTOCOL=resp3: 637/0

Notes

  • RESP3 reports query timeouts in the map's warning field rather than as a single-element array, so the existing TimeoutException detection path doesn't trigger under RESP3. Left out of scope for this fix; can follow up if desired.

🤖 Generated with Claude Code


Note

Medium Risk
Changes core RediSearch response parsing on a widely used path, but RESP2 behavior is preserved and the fix is covered by new unit tests plus full integration runs under both protocols.

Overview
Fixes #570 by teaching RediSearch response parsers to handle RESP3 map-shaped replies when StackExchange.Redis negotiates RESP3 automatically.

RedisReply now records IsMap (Resp3Type == Map) and adds GetMapValueOrDefault for flattened key/value map access, without changing existing array flattening behavior.

SearchResponse.NormalizeReply and AggregationResult.NormalizeReply detect map replies and reshape total_results / results / extra_attributes (and search score when present) back into the legacy flat RESP2 arrays so existing parse logic stays unchanged.

RedisIndexInfoAttribute no longer treats INDEXMISSING / INDEXEMPTY as inline switch cases; it flattens one level so NOSTEM, SORTABLE, INDEXMISSING, and INDEXEMPTY work whether flags are top-level (RESP2) or nested under flags (RESP3).

RedisUriParser sets ConfigurationOptions.Protocol from a protocol URI query (resp2/resp3 or 2/3) or REDIS_OM_PROTOCOL, leaving SE.Redis defaults when unset.

Adds Resp3ResponseTests with synthetic RESP3 map replies for search (with/without scores) and aggregation.

Reviewed by Cursor Bugbot for commit 19f5706. Bugbot is set up for automated code reviews on this repo. Configure here.

Newer StackExchange.Redis versions auto-negotiate RESP3, which changes
FT.SEARCH/FT.AGGREGATE/FT.INFO replies from the flat RESP2 array into a
map. SE.Redis collapses a map's Resp2Type to Array (flattened key/value
pairs), so the existing parsers read the first map key as the document
count, throwing "InvalidCastException: Could not cast to long".

Detect the RESP3 map shape at the parser layer and reshape it back into
the legacy RESP2 layout before the existing logic runs:

- RedisReply records IsMap (Resp3Type == Map) and exposes
  GetMapValueOrDefault; the existing flattening is preserved.
- SearchResponse.NormalizeReply and AggregationResult.NormalizeReply
  reshape map replies into the legacy [count, id, (score,) fields, ...]
  array (search) and [count, fields, ...] array (aggregation).
- RedisIndexInfoAttribute flattens RESP3's nested "flags" array so
  SORTABLE/NOSTEM/INDEXMISSING/INDEXEMPTY are detected under both
  protocols.
- RedisUriParser honors a "protocol" URI query arg and the
  REDIS_OM_PROTOCOL env var (resp2/resp3), giving users a workaround and
  enabling the full suite to run under RESP3.

Adds deterministic unit tests that feed synthetic RESP3 maps through the
search and aggregation parsers (CI runs RESP2 by default).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jit-ci

jit-ci Bot commented Jun 16, 2026

Copy link
Copy Markdown

🛡️ Jit Security Scan Results

CRITICAL HIGH MEDIUM

✅ No security findings were detected in this PR


Security scan by Jit

@slorello89
slorello89 merged commit 15a5e05 into main Jun 18, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InvalidCastException: Could not cast to long in SearchResponse when using StackExchange.Redis > 2.12.14 (RESP3 related?)

1 participant