Python: Fix Redis FT.CREATE PREFIX sending one prefix per character of collec…#13895
Python: Fix Redis FT.CREATE PREFIX sending one prefix per character of collec…#13895daric93 wants to merge 2 commits intomicrosoft:mainfrom
Conversation
…tion name Signed-off-by: Daria Korenieva <daric2612@gmail.com>
There was a problem hiding this comment.
Automated Code Review
Reviewers: 4 | Confidence: 94%
✓ Correctness
The change from
prefix=f"{self.collection_name}:"(a string) toprefix=[f"{self.collection_name}:"](a list) is a correct bug fix. The upstreamIndexDefinition._append_prefixmethod (redis-py) callslen(prefix)to emit the PREFIX count argument and then iteratesfor p in prefix. When a bare string is passed,len()returns the character count and iteration yields individual characters, producing a malformed Redis command. Wrapping it in a list ensures the count is 1 and the single prefix string is emitted correctly.
✓ Security Reliability
This is a correct and important bug fix. The
IndexDefinition._append_prefix()method (in redis-py) expects an iterable of prefix strings (typically a list), not a bare string. When a bare string is passed,len()returns the character count and iteration yields individual characters, producing a malformed Redis FT.CREATE command (e.g.,PREFIX 7 m y n a m e :instead ofPREFIX 1 myname:). Changing to[f"{self.collection_name}:"]fixes this. The legacy memory store atredis_memory_store.py:132has the same latent bug withprefix=f"{collection_name}:"but is outside this diff's scope. No security or reliability concerns with the change itself.
✓ Test Coverage
The change from a string to a list for the
prefixparameter inIndexDefinitionis a bug fix (redis-py expects a list for prefix), but the existing unit testtest_create_indexdoes not verify the arguments passed toIndexDefinitionorcreate_index. Sincecreate_indexis fully mocked, the test only checks that no exception is raised — it never asserts thatprefixis a list. This means the fix has no regression coverage; reverting it would not cause any test to fail.
✓ Design Approach
The change correctly fixes a bug where a bare string was passed to
IndexDefinition(prefix=...). The redis-pyIndexDefinition._append_prefixmethod callslen(prefix)(expecting a list length) and then iterates overprefixelement-by-element. Passing a string like"test:"caused the count to be the string length (e.g. 5) and iterated character-by-character, producing a malformed Redis FT.CREATE command. Wrapping the prefix in a list[f"{self.collection_name}:"]is the correct fix per the library's API contract.
Suggestions
- The legacy
redis_memory_store.py(line 132) has the same bug:IndexDefinition(prefix=f"{collection_name}:", ...)should also be wrapped in a list. Consider fixing it in this PR for consistency. - The
test_create_indextest (line 294) should assert thatcreate_indexwas called with anIndexDefinitionwhoseprefixis a list (e.g.,["test_collection:"]). Currently the mock makes zero assertions on its arguments, so this fix has no regression protection. Additionally,test_create_index_manual(line 298) passesprefix="test:"(a bare string); consider updating it to a list to match the production code and document the expected format.
Automated review by daric93's agents
Signed-off-by: Daria Korenieva <daric2612@gmail.com>
@microsoft-github-policy-service agree |
Fixes #13894.
The Python Redis connector was passing a plain string to
redis-py'sIndexDefinition(prefix=...).redis-pytreats that argument as an iterable of prefixes and iterates the string character-by-character. As a result,FT.CREATEwas registering one prefix per character of the collection name instead of one prefix equal to"<collection_name>:".Observed on the wire for collection
redis_json_list_data_model:Expected:
RediSearch on Redis Stack silently accepts the malformed command, so the bug has been latent for single-collection users.
Contribution Checklist