fix: preserve $meta direction in [field, direction] sort pair#4988
Open
spokodev wants to merge 1 commit into
Open
fix: preserve $meta direction in [field, direction] sort pair#4988spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
formatSort's pairToMap wrapped the direction in an array
(prepareDirection([v[1]])) unlike every sibling converter. Scalar directions
survive the array-to-string coercion, but a $meta object does not (isMeta is
false for an array), so the documented cursor.sort(['score', { $meta:
'textScore' }]) throws Invalid sort direction while the object and deep-array
forms work. Pass the raw value through, as the other converters do.
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.
Description
Sorting by
$metais silently rejected in the[field, direction]pair form ofsort. The documented, type-legal inputwhich is the canonical way to sort full-text search results by relevance, throws:
The equivalent object form
{ score: { $meta: 'textScore' } }and the deep form[['score', { $meta: 'textScore' }]]both work, so the three shapes the API accepts behave inconsistently.Root cause
In
src/sort.ts,pairToMapcallsprepareDirection([v[1]]), wrapping the direction in an array, unlike every sibling converter which passes the raw value. A scalar direction coincidentally survives the array to string coercion ([-1]->"-1"), but a$metaobject does not (isMetareturns false for an array), so it is rejected.This is a regression:
$meta-in-pair support was added in #1808, and the #2573 sort refactor reintroduced the array wrapping.Fix
Test
Added
test/unit/sort.test.ts(notest/unitfile coveredformatSortbefore). The$metapair case fails unpatched with the error above and passes after; the scalar-direction pair cases pass throughout, showing the fix is scoped. Fulltest/unitsuite stays green.