StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 1 · Path Operations & Routing

Enum Constrained Categories

easy100%1 of 1 solved

Problem statement

The reports endpoint takes category as plain text, so a typo like /reports/sale gets through to the handler. Constrain category to an Enum, so only the real report categories reach the handler and FastAPI rejects everything else.

Requirements

The app, the GET / home route, the ReportCategory enum in app/schemas.py and the report data REPORTS in app/store.py are already provided, and both are imported in app/main.py. The route GET /reports/{category} is declared too. Your task has one part, in app/main.py: finish the get_report handler.

An Enum lists the only values a parameter may take. When a path parameter has an Enum type, FastAPI accepts only those values, gives the handler the matching member, and shows the values in the API docs.

  • get_report takes category typed as the ReportCategory enum and returns that category's report from REPORTS. /reports/sales returns {"category": "sales", "metric": "revenue", "total": 42500}
  • Any other value, such as sale, Sales or revenue, is rejected by FastAPI with a 422 validation error that lists the allowed values, and get_report does not run
  • The API docs, at /docs and /openapi.json, show the three allowed values for category: sales, inventory and users
Sample Requests & Responses

GET /reports/sales

Response (200 OK):

{"category": "sales", "metric": "revenue", "total": 42500}

GET /reports/inventory

Response (200 OK):

{"category": "inventory", "metric": "units_in_stock", "total": 1280}

GET /reports/users

Response (200 OK):

{"category": "users", "metric": "active_users", "total": 512}

GET /reports/revenue

Response (422 Unprocessable Entity):

{"detail": [{"type": "enum", "loc": ["path", "category"], "msg": "Input should be 'sales', 'inventory' or 'users'", "input": "revenue", "ctx": {"expected": "'sales', 'inventory' or 'users'"}}]}

GET /openapi.json

Response (200 OK), the part that describes category:

{"type": "string", "enum": ["sales", "inventory", "users"], "title": "ReportCategory"}

More in Path Operations & Routing

See the module
Typed Path ParametersThis problem covers reading the values that sit inside a web address, so a caller who puts something unexpected there is turned away before any of your code runs.89%8 of 9 solvedeasyRoute Order And Fixed PathsThis problem covers what happens when two addresses could both match the same request, so a page with a fixed name keeps working after a more general pattern is added beside it.100%1 of 1 solvedeasyStorage Browser RoutesThis problem covers laying out the whole set of addresses a file browser needs, so each one reaches the handler it was meant for and none of them quietly swallows another.67%2 of 3 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.