Why is my FastAPI sorting endpoint ignoring query parameters?

0
0
Asked By MellowPine42 On

I'm learning MLOps and building a small FastAPI app that serves patient data from a JSON file. I created endpoints and query parameters to filter and sort the records, but the response always comes back in the original default order. For example, calling `/sort?sort_by=height&order=asc` produces the same result as before, and even invalid values don't raise validation errors. I'm not sure whether the endpoint is being called incorrectly, the query parameters aren't connected to the sorting logic, or an older version of the app is still running. The relevant `main.py` and JSON data are included in the project files.

2 Answers

Answered By CedarLark7 On

Make sure you’re calling the sorting route itself, such as `/sort?sort_by=height&order=asc`, rather than sending those parameters to a different endpoint like `/view`. Also verify that the server has been restarted or is running the current code; otherwise you may be testing an older version that doesn’t include the sorting changes.

MellowPine42 -

I’m calling `/sort?sort_by=height&order=asc`, but changing the parameters still gives me the default order. Invalid values also don’t produce an error, so I’m wondering whether the code is actually using those query parameters.

Answered By QuietHarbor19 On

Ascending order is already the default, so using `order=asc` can look like it had no effect. Try `order=desc` and compare the first and last records. If descending still produces the same result, check that the endpoint passes the parsed `sort_by` and `order` values into the sorting function instead of always using its defaults. Invalid values will only raise an exception if the parameters are restricted with validation, such as an enum or a constrained type.

MellowPine42 -

I tried the descending option as well, but the response still appears to use the original order.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.