The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S49 · V3·48

Typed at the Door

Volume 3, chapter 48. TiffinBox's Server held the sockets, the routes, every order, the queues and the rules for changing them in 681 lines; its order API took line-based text that accepted orders for kitchens k07 and k999999; one request read the clock three times, so an order priced as breakfast at 10:59:59.999 was logged as received at 11:00. This stage splits out an order service (Orders) and its controller (OrdersApi), reads the clock once per request into a RequestContext, takes typed JSON bodies (PlaceOrder, KitchenEvent) bound field by field with every problem at once, and reports errors as RFC 9457 problem details to clients that send JSON or ask for them, plain text to the rest. The text bodies keep working, marked with Deprecation and Sunset headers from the contract, and the access log counts their use: 54 of the browser suite's 57 POSTs, all from TiffinBox's own code, until those clients moved.

6 parts · ~6 min to read
Updated Suggest a correction

Stage 49: typed at the door

The brief

What you'll build.

  • Orders: the order service: orders, prices, queues, idempotency keys and the kitchens that exist; place(PlaceOrder, customer, key, at) and apply(id, event, key, at) return outcomes (Created, Replayed; Moved, Refused, AlreadyApplied, NotFound), never responses.
  • OrdersApi: the controller for POST /orders, GET /orders/{id}, POST /orders/{id}/events and GET /kitchens/{id}/next: headers, body, service, response.
  • RequestContext(id, at): built once per request in Server.serve, passed to every handler (the route table's Handler takes it), used by the order's time, its price, the log's received-at and the access log.
  • JsonIn: a strict JSON reader (duplicate keys, trailing text, lone surrogates, control characters and deep nesting refused, with offsets).
  • Bodies: PlaceOrder and KitchenEvent bound from JSON with every field's problem; the old text formats as before, plus a kitchen check (k1 to k12); Deprecation, Sunset and Link headers on responses to text bodies; GET /api/deprecations.
  • Problem: one error shape, sent as application/problem+json to clients that send JSON or ask for it, as plain text otherwise.
  • The contract: PlaceOrder, LineRequest, KitchenEvent, Problem, FieldError and $deprecations in contract/api.json; web-src/contract.ts regenerated.
  • Clients: the kitchen views' sender converts taps to KitchenEvent JSON (queued taps too) and reads a problem's detail; the browser suite's seeding, Main simulate, the canary and the release check send JSON. The HTML checkout calls Orders directly.
  • Access log: "deprecated":true on requests that used a text body; the release check fails if it sends one.
  • Tests: OrdersTest (3, no HTTP), ContractTest.requestBodiesAndProblemsMatchTheContract, deprecationHeadersCarryTheContractsDates, ServerTest.aRequestReadsTheClockOnce, theJsonReaderRefusesWhatParsersDisagreeAbout; web tests for the sender's conversion and postCommand's problem detail.
  • Measurements in exp/49: Instants.java (instants-before.out, instants-after.out), bodies.py (bodies-before.out, bodies-text-after.out, bodies-json-after.out), deprecated.py (deprecated.out), problem.out.

Why a real company needs this. The boundary is where a service decides what it will believe. A loose format lets bad data in quietly; a missing request time lets records disagree; an error format change breaks clients that show errors to people; a deprecation that nobody counts never ends.

Why now. Chapter 48 is Spring Web: the DispatcherServlet, binding and validation, ProblemDetail, filters and interceptors, content negotiation. Stage 35 left Server large; stage 38 promised a request-context instant; stage 46 (chapter 45's stage) left untyped request bodies and deprecation headers for this stage.

What you'll have at the end.

  • a service with the order rules and no HTTP, a controller with HTTP and no rules;
  • one instant per request;
  • typed request bodies with every error at once, and problem details for clients that read them;
  • a deprecation with dates, headers and a count, and no internal users left;
  • ADR-0051: TiffinBox's web layer.

Theory to own

  1. Controller and service. What does each know, and which can be tested without a socket? (The chapter, "Controllers and request mapping"; Companion, Theory 1.)
  2. Parse, don't validate. Why bind to a type that can only hold valid values? (Companion, Gaps 1 and Theory 2; the chapter, "Validation in MVC".)
  3. Time as an input. Why one reading per request? (Companion, Gaps 2 and Theory 3; stage 38.)
  4. Negotiation and deprecation. How can an error format or a request format change without breaking anyone? (The chapter, "Message converters, serialisation and content negotiation"; Companion, Gaps 3, 4 and Theory 4, 5.)

Build it

Step 1: what the boundary lets in

Write exp/49/bodies.py (nine bodies to POST /orders, each on a fresh key) and exp/49/Instants.java (a clock that moves 1 ms per reading, starting just before 11:00 in Kolkata; two orders, an event and a kitchen page; the log's received-at read back). Run both on stage 48's jar.

Checkpoint. exp/49/bodies-before.out, instants-before.out.

Step 2: the service and its controller

Move orders, prices, queues, keys and recovery from Server into Orders, with outcome records instead of responses. Write OrdersApi, point the route table at it, and make the HTML checkout call Orders.place with a typed command. Write OrdersTest.

Checkpoint. golden master: 47 exchanges, identical; server tests pass; OrdersTest places orders with no server.

Step 3: one instant

Add RequestContext, create it in serve after reading the request's head, and pass it through the route table, Pages and OrdersApi to Orders. Use it for the access and error log lines. Write aRequestReadsTheClockOnce.

Checkpoint. exp/49/instants-after.out.

Step 4: typed bodies and problems

Write JsonIn, Bodies and Problem. JSON bodies bind to PlaceOrder and KitchenEvent with every problem at once; text bodies parse as before plus the kitchen check, and their responses get Deprecation, Sunset and Link. Add the types and $deprecations to the contract, regenerate contract.ts, and add the contract tests. Add deprecation and sunset to the golden master's headers and record it.

Checkpoint. exp/49/bodies-text-after.out, bodies-json-after.out; contract_diff.py reports five types added, compatible.

Step 5: who still sends text?

Add TB_SUITE_LOGS to the browser suite's start_server, run the suite, and count with exp/49/deprecated.py. Move every sender you find: the kitchen sender (kitchenEvent, postCommand(…, { json: true }) reading a problem's detail), the suite's Api, Main simulate (merging a dish drawn twice into one line), the canary and the release check. Run the suite again. Add the release check's own guard.

Checkpoint. exp/49/deprecated.out.

Senior and architect lens

1. The boundary decides. What binding checks, everything after it can rely on; what it lets through, everything after it must remember to check. Put checks where the type is made.

2. ADR-0051: TiffinBox's web layer. Write it with these points:

  • Layers: Server (sockets, routing, security headers, streams, static files), controllers (OrdersApi: HTTP to commands and back), services (Orders: rules, no HTTP). Pages calls services, not controllers.
  • Request context: id and instant, once per request, passed to every handler; no handler reads the clock.
  • Bodies: JSON, bound strictly (no unknown fields, duplicate keys or quoted numbers), every problem at once, references checked at the boundary (kitchens, dishes).
  • Errors: Problem; problem details for clients that send JSON or ask; plain text otherwise, until text errors have their own deprecation.
  • Deprecation: in the contract ($deprecations), headers generated from it, uses counted in the access log; removal when the count is zero for 30 days and not before the sunset (2027-05-20). The golden master's text exchanges and the stage scripts go with it.

3. Count, then remove. A deprecation ends when its count reaches zero, not on its date.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
kitchens are a constant (k1 to k12) a new kitchen needs a release stage 56 (projects: kitchens as data; stage 50 measured the storage first), re-pointed there to Volume 4 (databases: kitchens as a table)
anyone may place an order as any customer X-Customer-Id is believed stage 51 (security: authentication and authorisation on orders)
the deprecation count isn't per client we can't tell whose code to change stage 51 (security: a client identity on every request)
Server still holds streams and static files 520 lines stage 52 (async: the event stream as its own component)

Evidence

  • exp/49/instants-before.out, instants-after.out, bodies-before.out, bodies-text-after.out, bodies-json-after.out, deprecated.out, problem.out.
  • Orders, OrdersApi, RequestContext, JsonIn, Bodies, Problem; the contract's request types and $deprecations.
  • The kitchen sender, the suite, simulate, the canary and the release check send JSON; the release check refuses its own deprecated bodies.
  • Core 52, server 66 and tbx 21 tests; web unit tests 47; golden master: 47 exchanges, identical; the release check passes.
  • ADR-0051 is committed.

Carry forward

  • New endpoints get a controller and a service, a typed body and Problem errors.
  • Handlers take the request's instant; they don't read the clock.
  • Every format change is expand and contract, with a count.