StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 4 · Response Models and Status Codes

Serialization Tuning

easy—No data yet

Problem statement

Both device endpoints answer with nothing, and the summary shape is a copy of the full one. Tune what each endpoint puts on the wire, so the listing reports only the values a device actually set and never a null, and the summary reports three fields for every device.

Sample Data

The five devices in app/store.py are stored as DeviceRecord, which is a Device plus an api_key that no endpoint may report. Each device was built with a different set of values supplied: one supplied all of them, one supplied only id, name and zone, and the rest fall in between. Two supplied a value of null, and two supplied a value that happens to equal the default. What each device supplied is what the listing has to report.

API Endpoints to Implement

DeviceSummary

The summary shape, in app/models/summary.py. Cut it down to three fields, all of them required:

  • id, a whole number.
  • name, text.
  • status, text.

GET /devices

Answers 200 with every stored device in order, each reported through Device, which declares id, name and zone as required and status, firmware, battery_pct, last_error and tags with defaults.

  • A field a device never supplied is left out, even when the field has a default.
  • A field a device supplied is reported, including when the value it supplied equals the default, so a supplied battery_pct of 100 and a supplied empty tags list both appear.
  • A field supplied as null is left out rather than reported as null, so no answer carries a null anywhere.
  • api_key is not part of Device and never appears.

GET /devices/summary

Answers 200 with the same devices in the same order, each reported through DeviceSummary, so every entry carries id, name and status and nothing else. A device that never supplied a status still reports the default active here, because the summary reports all three fields for every device.

The published description

The two shapes must be declared rather than assembled by hand, since the service publishes them at GET /openapi.json, where each endpoint answers with a list of a named shape, the two shapes are different, the device shape names all eight fields and requires only id, name and zone, and the summary shape names its three fields and requires all three.

Sample Requests & Responses

GET /devices

Response (200 OK):

[
  {"id": 1, "name": "Gate Sensor", "zone": "north", "status": "active", "firmware": "v3.2.1", "battery_pct": 87, "tags": ["outdoor", "gate"]},
  {"id": 2, "name": "Lobby Camera", "zone": "south"},
  {"id": 3, "name": "Cold Store Probe", "zone": "north", "battery_pct": 100, "tags": []},
  {"id": 4, "name": "Dock Scanner", "zone": "east", "status": "offline", "last_error": "link timeout"},
  {"id": 5, "name": "Roof Anemometer", "zone": "west", "status": "maintenance", "firmware": "v2.7.0", "battery_pct": 41, "tags": ["outdoor"]}
]

GET /devices/summary

Response (200 OK):

[
  {"id": 1, "name": "Gate Sensor", "status": "active"},
  {"id": 2, "name": "Lobby Camera", "status": "active"},
  {"id": 3, "name": "Cold Store Probe", "status": "active"},
  {"id": 4, "name": "Dock Scanner", "status": "offline"},
  {"id": 5, "name": "Roof Anemometer", "status": "maintenance"}
]

More in Response Models and Status Codes

See the module
Filtered User ResponseThis problem covers deciding which parts of a stored user are safe to hand back, so passwords and internal notes stay inside the service while callers still get the details they need.—No data yeteasyCorrect Creation CodesThis problem covers answering each request with the code its outcome deserves, so a caller can tell a newly created thing from a plain success and a removal from a thing that was never there.—No data yeteasyAccounts API Output ContractThis problem covers settling what an accounts API puts on the wire, so a list stays lean, a single account comes back complete, every outcome carries the right code, and the values meant to stay inside never leave.—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.