Skip to content

Search endpoint returns one fewer result than expected (off-by-one) #4

@anxkhn

Description

@anxkhn

Context

The GET /search endpoint has an off-by-one error in its pagination slicing. When the number of matching results equals or exceeds the limit, it returns one fewer result than it should.

Steps to reproduce

  1. Create 5 profiles with usernames starting with "a" (alice, alex, anna, amy, adam).
  2. Search with GET /search?q=a&limit=3.
  3. The total field correctly shows 5, but results contains only 2 items instead of 3.

You can also reproduce with a test:

def test_search_pagination():
    # (add 5 profiles with "a" in username)
    response = client.get("/search?q=a&limit=3")
    assert len(response.json()["results"]) == 3  # FAILS: returns 2

Expected behavior

With limit=3, the endpoint should return up to 3 matching results.

Actual behavior

With limit=3, the endpoint returns only 2 results because the slice uses offset + limit - 1 instead of offset + limit.

Files

  • app/main.py -- the search_profiles function, the return statement with the slice

Acceptance criteria

  • GET /search?q=...&limit=N returns up to N results (not N-1)
  • Existing search tests still pass
  • The total field remains unchanged (full count, not sliced count)

Suggested approach

  1. Open app/main.py and find the search_profiles function.
  2. Find the line: results[offset : offset + limit - 1]
  3. Change it to: results[offset : offset + limit]
  4. Run pytest tests/test_search.py -v.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions