Repository navigation
Fix RESP3 map parsing for FT.SEARCH/AGGREGATE/INFO (#570) - #571
Merged
Merged
Conversation
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 Security Scan Results✅ No security findings were detected in this PR
Security scan by Jit
|
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.
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:Under RESP3,
FT.SEARCH/FT.AGGREGATE/FT.INFOreply with a map (total_results/results/extra_attributes) instead of the flat RESP2 array. SE.Redis collapses a map'sResp2TypetoArray(flattened, interleaved key/value pairs), so the existing parsers read the first map key ("attributes") as the document count and fail to cast it tolong. ForcingProtocol = RedisProtocol.Resp2is 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:
RedisReplyrecordsIsMap(Resp3Type == ResultType.Map) and exposesGetMapValueOrDefault(key). The existing flattening behavior is preserved —IsMapis purely additive.SearchResponse.NormalizeReplyreshapes a map reply into the legacy[count, id, (score,) fields, …]array (used by bothSearchResponseandSearchResponse<T>, including WITHSCORES).AggregationResult.NormalizeReplydoes the same forFT.AGGREGATE(generic + non-generic, incl. WITHCURSOR).RedisIndexInfoAttribute— RESP3 groups attribute flags (SORTABLE/NOSTEM/INDEXMISSING/INDEXEMPTY) inside a nestedflagsarray rather than as trailing top-level tokens; flag detection now flattens one level so both protocols are handled.RedisUriParserhonors aprotocolURI query arg (resp2/resp3or2/3) and theREDIS_OM_PROTOCOLenvironment variable. This gives users an explicit protocol selector (and a RESP2 workaround) and lets the whole test suite run under RESP3.Testing
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.redis/redis-stack-server):REDIS_OM_PROTOCOL=resp3: 637/0Notes
warningfield rather than as a single-element array, so the existingTimeoutExceptiondetection 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.
RedisReplynow recordsIsMap(Resp3Type == Map) and addsGetMapValueOrDefaultfor flattened key/value map access, without changing existing array flattening behavior.SearchResponse.NormalizeReplyandAggregationResult.NormalizeReplydetect map replies and reshapetotal_results/results/extra_attributes(and searchscorewhen present) back into the legacy flat RESP2 arrays so existing parse logic stays unchanged.RedisIndexInfoAttributeno longer treatsINDEXMISSING/INDEXEMPTYas inline switch cases; it flattens one level soNOSTEM,SORTABLE,INDEXMISSING, andINDEXEMPTYwork whether flags are top-level (RESP2) or nested underflags(RESP3).RedisUriParsersetsConfigurationOptions.Protocolfrom aprotocolURI query (resp2/resp3or2/3) orREDIS_OM_PROTOCOL, leaving SE.Redis defaults when unset.Adds
Resp3ResponseTestswith 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.