# Design โ€” Guarantees, Startup Pipeline, and the Contract Model This page is the architecture reference: the fail-fast startup surface, the guarantees that hold by construction, and the trade-offs baked into every decision. --- ## ๐Ÿ—๏ธ 1. The Startup Pipeline Everything happens **once, eagerly, at construction time** โ€” for the app at `OpenAPIFirstApp(...)`, for the client at `OpenAPIClient(...)`. There is no lazy loading, no deferred binding, no "it'll work on first request". ```text openapi.yaml โ”€โ”€โ–บ loader.load_openapi โ”œโ”€ parse (json/yaml by extension) โ”œโ”€ validate (strict OpenAPI 3.x validator) โ””โ”€โ–บ dict โ”€โ”€โ–ถ OpenAPISpecLoadError โ”‚ OpenAPIFirstApp(openapi_path=..., routes_module=routes) โ”œโ”€ 1. load + validate spec (loader) โ”œโ”€ 2. parse securitySchemes (security) โ”œโ”€ 3. make_security_dependencies (security) โ”œโ”€ 4. bind_routes: every operationId โ”€โ”€โ–บ routes. โ”‚ โ””โ”€ missing op / missing handler โ”€โ”€โ–บ MissingOperationHandler โ””โ”€โ–บ FastAPI app: routes registry + /openapi.json + Swagger UI ``` Client โ€” same spec, same guarantees: ```text OpenAPIClient(spec) โ”œโ”€ require servers[] โ”œโ”€ require paths โ”œโ”€ one callable per operationId (client.) โ”‚ โ””โ”€ missing / duplicate opId โ”€โ”€โ–บ OpenAPIClientError โ””โ”€โ–บ ready ``` --- ## ๐Ÿ“œ 2. Guarantees That Hold by Construction These are not conventions โ€” they are enforced at startup or client construction: 1. **Every route is spec-declared.** Routes are registered *only* from `paths`; there is no decorator-driven or implicit routing. 2. **Every operation is handled.** Each `operationId` resolves to an exact handler name in `routes_module`; a missing one aborts bootstrap. 3. **Every operation has an `operationId`.** An operation without one cannot be bound and fails fast. 4. **Client and server use the same spec.** One document drives both sides, so they cannot drift. 5. **Auth is spec-driven.** `security` dependencies come from `securitySchemes` + per-operation `security`; no manual middleware. 6. **Spec is valid before use.** Invalid, malformed, or unloadable specs are rejected at load, not at first request. ### The startup "no half-states" property Because binding, validation, and security resolution all run at construction, a booted app is a **provably-complete app**. If the contract is violated in any way, the process refuses to start โ€” the failure is loud, immediate, and tells you exactly what to fix. --- ## ๐Ÿงฉ 3. Component Responsibilities | Module | Owns | Refuses to | |--------|------|------------| | `loader` | parse + validate the spec | modify or "fix" the spec | | `app` | assemble FastAPI from spec + handlers | routing decisions, decorators | | `binder` | `operationId` โ†’ handler mapping | infer handlers from paths | | `client` | build one callable per `operationId` | guess URLs, deserialize implicitly | | `security` | schemes โ†’ FastAPI `Depends` | manual middleware | | `errors` | the error hierarchy | swallow failures | | `codegen` | build-time model/routes generation | runtime codegen | | `templates` | copyable scaffolds | production data stores | **Rule of thumb for contributors:** keep modules coercive (they raise if the contract is wrong) and narrow (one responsibility each). Pydoclint + the test suite keep that contract honest. --- ## โš–๏ธ 4. Design Trade-offs (Accepted) | Decision | Chosen because | What you give up | |----------|----------------|------------------| | Handlers are plain callables | Framework-agnostic, testable, grep-able | No decorator sugar | | Fail-fast at startup | Drift caught in CI, not at 3am | Slightly heavier boot | | Raw `httpx.Response` from client | No hidden deserialization/validation | You read `.json()` yourself | | Build-time codegen | One-way generation, no runtime generator dep | Spec must already be spec-valid | | Templates copy verbatim | Scaffold, not magic | Templates never updated in place | --- ## ๐Ÿ”— Related - [01 โ€“ Overview](01_overview.md) ยท [02 โ€“ Components](02_components.md) ยท [05 โ€“ Security](05_security.md) ยท [06 โ€“ Error Handling](06_error_handling.md) ยท [07 โ€“ Testing](07_testing.md)