StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 7 · Metadata & Documentation

Schema Examples

easy—No data yet

Problem statement

Build a bakery pre-order counter that takes and lists pre-orders, and publishes ready-made example orders for callers to copy.

Sample Data

The counter in app/store.py starts with two pre-orders, and the next pre-order created takes the id 3.

API Endpoints to Implement

The two handlers live in app/api/routes/preorders.py, the draft they take, PreorderDraft, in app/models/preorder.py, and their helpers, list_preorders and add_preorder, in app/services/preorders.py.

GET /preorders

Answers 200 with every pre-order in the order the counter holds them.

POST /preorders

  • Takes the body as a PreorderDraft and answers 201 with the stored pre-order: the drafted values plus the id it was given. A stored pre-order joins the counter, so a fresh listing reports it after the two the counter started with.
  • item is text, 2 to 40 characters, required.
  • quantity is a whole number, 1 to 24, required.
  • pickup_hour is a whole number, 7 to 18, required.
  • note is text of at most 80 characters, optional, default null.

The published examples

PreorderDraft publishes examples, a list of one entry, the whole draft worked out:

[{"item": "Walnut Rye Loaf", "quantity": 3, "pickup_hour": 9, "note": "Sliced, please"}]

Each of its four values publishes an examples list of its own, holding one entry:

  • item names Sourdough Loaf
  • quantity names 2
  • pickup_hour names 8
  • note names Sliced, please

These five entries belong to the draft itself, not to the operation that takes it, and the draft still publishes the limits above alongside them. Both the whole-draft entry and a draft built from the four per-value entries are stored when sent.

POST /preorders publishes examples of its own on the JSON body it takes, three entries in this order, each holding a summary and a value:

{
  "single_loaf": {"summary": "One loaf for the morning counter", "value": {"item": "Sourdough Loaf", "quantity": 1, "pickup_hour": 8}},
  "party_order": {"summary": "A large order with a note", "value": {"item": "Cinnamon Buns", "quantity": 18, "pickup_hour": 15, "note": "Boxed in threes"}},
  "past_the_limit": {"summary": "A quantity the counter refuses", "value": {"item": "Cinnamon Buns", "quantity": 30, "pickup_hour": 15}}
}

single_loaf and party_order are stored when sent. past_the_limit is refused, like any other draft over the limit.

Rejected requests

A draft missing one of its three required values, or carrying a value of the wrong kind or outside its stated range, is answered with 422 and one detail entry per broken rule, carrying loc of ["body", "field name"], the input that was received, and the type and msg reported for the rule.

The published document

The service publishes its description at GET /openapi.json. Every example must be declared on the draft or on the operation itself, not written into that document by hand.

There, POST /preorders takes its body as a reference to the shared PreorderDraft shape under components/schemas, with nothing else beside the reference.

Sample Requests & Responses

GET /preorders

Response (200 OK):

[
  {"id": 1, "item": "Baguette", "quantity": 4, "pickup_hour": 7, "note": null},
  {"id": 2, "item": "Almond Croissant", "quantity": 6, "pickup_hour": 10, "note": "Two boxes"}
]

POST /preorders

Request:

{
  "item": "Sourdough Loaf",
  "quantity": 1,
  "pickup_hour": 8
}

Response (201 Created):

{
  "id": 3,
  "item": "Sourdough Loaf",
  "quantity": 1,
  "pickup_hour": 8,
  "note": null
}

POST /preorders

Request:

{
  "item": "Cinnamon Buns",
  "quantity": 30,
  "pickup_hour": 15
}

Response (422 Unprocessable Entity):

{
  "detail": [
    {
      "type": "less_than_equal",
      "loc": ["body", "quantity"],
      "msg": "Input should be less than or equal to 24",
      "input": 30,
      "ctx": {"le": 24}
    }
  ]
}

More in Metadata & Documentation

See the module
Described Path OperationsThis problem covers making a station directory explain itself to the people who call it, so that every operation it publishes arrives with a title, an explanation, the group it belongs to, a named shape for its answer, and a warning on the one route that is on its way out.—No data yeteasyBranded API MetadataThis problem covers giving a service its own published identity, the contact and license behind it, described groupings for its operations, and its own addresses for the pages that describe it.—No data yeteasyPartner-Ready Public DocsThis problem covers publishing a service's whole public description as a contract: the name, contact and license behind it, described groupings for its operations, a title, an explanation and a lifecycle marker on each operation, worked payloads for the values it takes, its own addresses for the pages that describe it, and a fixed expectation the description is checked against.—No data yeteasy

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.