Fix LFClassic api 500 error when querying for all entries#2242
Fix LFClassic api 500 error when querying for all entries#2242imnasnainaec wants to merge 1 commit intodevelopfrom
Conversation
📝 WalkthroughWalkthroughModified MongoDB query pagination behavior in the Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@backend/LfClassicData/LfClassicMiniLcmApi.cs`:
- Around line 213-214: Add input validation in the Query method to reject any
options.Count that is negative and not equal to QueryOptions.QueryAll;
specifically check options.Count and if (options.Count < 0 && options.Count !=
QueryOptions.QueryAll) return a client error (e.g., BadRequest / throw
ArgumentException) rather than letting the value reach the .Limit(...) call,
ensuring only the sentinel -1 is allowed and all other negative counts are
rejected.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: c1800774-7d2d-401e-8fb6-f2267b6e3d2d
📒 Files selected for processing (1)
backend/LfClassicData/LfClassicMiniLcmApi.cs
| .Skip(options.Offset) | ||
| .Limit(options.Count) | ||
| .Limit(options.Count == QueryOptions.QueryAll ? 0 : options.Count) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== QueryOptions definitions / count validation ==="
fd -i 'QueryOptions' backend -x sed -n '1,220p' {}
echo
echo "=== LFClassic count binding and limit call sites ==="
rg -n -C3 --glob 'backend/**/*.cs' 'QueryAll|\.Limit\s*\(|\bCount\b|\bcount\b|ArgumentOutOfRangeException'Repository: sillsdev/languageforge-lexbox
Length of output: 50386
🏁 Script executed:
# Check if QueryOptions.Count has validation attributes
rg -A 5 'record QueryOptions|int Count' backend/FwLite/MiniLcm/IMiniLcmReadApi.cs
# Check if LfClassicRoutes.Count has validation
rg -B 2 -A 5 'public int\? Count' backend/LfClassicData/LfClassicRoutes.cs
# Check for any validation on negative counts in the Query method or upstream
rg -B 3 -A 8 'public.*Query.*QueryOptions' backend/LfClassicData/LfClassicMiniLcmApi.cs | head -40Repository: sillsdev/languageforge-lexbox
Length of output: 533
🏁 Script executed:
# Check for any ArgumentException or validation in Query method implementation
rg -B 5 -A 25 'private.*IAsyncEnumerable.*Query.*QueryOptions' backend/LfClassicData/LfClassicMiniLcmApi.cs
# Check if there's any validation in the route handler or controller
rg -B 3 -A 10 'Count.*QueryAll|ArgumentOutOfRangeException.*Count|ArgumentException.*Count' backend/LfClassicData/Repository: sillsdev/languageforge-lexbox
Length of output: 3551
Add validation to reject non-sentinel negative counts.
This fixes the QueryOptions.QueryAll case (count=-1), but options.Count < -1 still reaches .Limit(...). There is no upstream validation on the Count parameter in LfClassicRoutes or within the Query method itself. If a client sends count=-2, it will pass the conditional check and cause MongoDB to reject the aggregation pipeline with a 500 error, reproducing the original issue.
Add a guard to reject every negative value except QueryAll:
Suggested fix
+ var limit = options.Count switch
+ {
+ QueryOptions.QueryAll => 0,
+ < 0 => throw new ArgumentOutOfRangeException(nameof(options.Count), "Count must be >= 0 or QueryAll."),
+ _ => options.Count
+ };
+
pipeline = AppendHeadWord(sortWs, pipeline)
.Sort(Builders<HeadwordEntry>.Sort
.Ascending(entry => entry.headword)
.Ascending(entry => entry.MorphologyType)
.Ascending(entry => entry.Guid))
.Skip(options.Offset)
- .Limit(options.Count == QueryOptions.QueryAll ? 0 : options.Count)
+ .Limit(limit)
.Project(entry => entry as Entities.Entry);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@backend/LfClassicData/LfClassicMiniLcmApi.cs` around lines 213 - 214, Add
input validation in the Query method to reject any options.Count that is
negative and not equal to QueryOptions.QueryAll; specifically check
options.Count and if (options.Count < 0 && options.Count !=
QueryOptions.QueryAll) return a client error (e.g., BadRequest / throw
ArgumentException) rather than letting the value reach the .Limit(...) call,
ensuring only the sentinel -1 is allowed and all other negative counts are
rejected.
There was a problem hiding this comment.
@myieye Would you prefer if I also add this check for invalid negative counts?
There was a problem hiding this comment.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
count=-1is how to indicate to the API to get all entries, but that is accomplished with MongoDB via.Limit(0), not.Limit(-1).