How hoaxnerd/fastrest works
1. What this repo is
FastREST is a Python library for building async REST APIs on top of FastAPI + Pydantic, with a DRF-style developer model. It is not an application; it provides reusable framework components that an app imports and wires into a FastAPI instance.
2. Core request path
The main runtime flow is: FastAPI request → router → viewset/generic view → serializer/model adapter → response. Routing, validation, and response shaping are handled by FastREST rather than being left to ad hoc endpoint code.
3. Primary API surface
The high-level entry points are:
- routers.py for registering resources and generating routes
- viewsets.py and generics.py for CRUD endpoints
- serializers.py and fields.py for schema/validation
- decorators.py for custom actions
- response.py, request.py, exceptions.py for HTTP/runtime plumbing
4. Model-driven CRUD generation
A major subsystem is router.serve(Model), which auto-derives a serializer, viewset, and route set from a model. This is the “zero-config” path that turns ORM models into list/create/retrieve/update/delete endpoints plus optional search, ordering, pagination, and permissions.
5. ORM abstraction boundary
The compat/orm/ package is the main persistence boundary. It contains adapters for SQLAlchemy, Tortoise, SQLModel, and Beanie, all behind a common adapter interface. This lets the framework keep one CRUD/API layer while swapping persistence implementations underneath.
6. Serializer and validation layer
serializers.py and fields.py provide a DRF-like schema system with Pydantic-backed validation. Serializers define the data contract, perform field/object validation, and produce request/response shapes used by both HTTP endpoints and generated OpenAPI schemas.
7. Cross-cutting request policies
Authentication, permissions, throttling, filtering, pagination, and content negotiation are separate subsystems rather than endpoint-specific code:
- authentication.py for token/basic/session auth
- permissions.py for composable allow/deny rules
- throttling.py for rate limits
- filters.py and pagination.py for list query behavior
- negotiation.py for renderer selection from Accept
8. HTTP and API composition
routers.py, views.py, viewsets.py, generics.py, and response.py define how framework objects become FastAPI routes and HTTP responses. The library also generates method-specific OpenAPI metadata, typed path parameters, and status codes that match CRUD semantics.
9. Agent-facing integration layer
FastREST goes beyond standard REST with two generated artifacts:
- mcp.py mounts a Model Context Protocol server so AI agents can call viewset actions as tools
- skills.py generates per-resource and global SKILL.md documents
- manifest.py emits a machine-readable API manifest
These are derived from the same router/viewset/serializer metadata as the HTTP API, so they stay in sync.
10. Settings and feature toggles
settings.py centralizes app-level configuration, with precedence from viewset overrides to app settings to framework defaults. This controls default permissions, auth, pagination, MCP, skill generation, and other behavior without changing endpoint code.
11. Support utilities and diagnostics
Smaller modules like status.py, manifest.py, openapi.py, relations.py, mixins.py, authentication.py, and skills.py fill in framework behavior around HTTP status constants, schema generation, relation handling, and reusable mixin logic.
12. Testing strategy
The tests/ directory is broad and feature-oriented: there are contract tests for each ORM adapter, plus coverage for auth, permissions, throttling, filtering, pagination, negotiation, MCP, manifest generation, router behavior, serializers, and end-to-end integration. This indicates the repo’s architecture is centered on consistent behavior across adapters and layers.
13. Packaging and runtime constraints
The project is packaged as a typed Python library (py.typed) with optional extras for ORM backends and MCP support. Its architecture depends materially on FastAPI, Pydantic v2, and Python 3.10+; everything else is layered on top of that core stack.
14. How to read the architecture
If you are diagramming it, the key boundaries are:
- FastAPI app boundary at the top
- Routing/viewset/serializer layer in the middle
- ORM adapter boundary below that
- Agent/documentation generators as parallel outputs from the same metadata
- Cross-cutting policies applied around the request lifecycle
Main entry points: routers.py Routers · serializers.py Serializers · settings.py Settings · viewsets.py Viewsets
Components 27
Core API
Primary request-handling and composition layer for building REST endpoints.
- Routers · Registers resources and assembles route sets, including model-serving shortcuts.
src/fastrest/routers.py - Viewsets · DRF-style resource controllers that implement CRUD and custom actions.
src/fastrest/viewsets.py - Generics · Reusable generic view behavior that powers standard list/create/retrieve/update flows.
src/fastrest/generics.py - Views · FastAPI-facing view composition and dispatch helpers.
src/fastrest/views.py - Serializers · Pydantic-backed data contracts, validation, and output shaping.
src/fastrest/serializers.py - Fields · Serializer field definitions and validation primitives.
src/fastrest/fields.py - Decorators · Custom action annotations and endpoint metadata helpers.
src/fastrest/decorators.py - Request · Framework request helpers and parsed request context.
src/fastrest/request.py - Response · Framework response construction and HTTP response normalization.
src/fastrest/response.py - Exceptions · Framework error types and HTTP exception translation.
src/fastrest/exceptions.py - Settings · Central configuration and precedence rules for framework behavior.
src/fastrest/settings.py
Policies
Cross-cutting request behavior applied across the API lifecycle.
- Auth · Token, basic, and session authentication hooks.
src/fastrest/authentication.py - Permissions · Composable allow/deny rules for endpoint access.
src/fastrest/permissions.py - Throttling · Request-rate limits and quota enforcement.
src/fastrest/throttling.py - Filters · List-query filtering behavior and predicate application.
src/fastrest/filters.py - Pagination · Page sizing and list navigation behavior.
src/fastrest/pagination.py - Negotiation · Renderer selection and Accept header handling.
src/fastrest/negotiation.py
ORM Adapters
Persistence abstraction layer shared by generated CRUD flows.
- ORM base · Common persistence adapter contract used by all ORM backends.
src/fastrest/compat/orm/base.py - SQLAlchemy · SQLAlchemy persistence adapter implementation.
src/fastrest/compat/orm/sqlalchemy.py - Tortoise · Tortoise ORM persistence adapter implementation.
src/fastrest/compat/orm/tortoise.py - SQLModel · SQLModel persistence adapter implementation.
src/fastrest/compat/orm/sqlmodel.py - Beanie · Beanie persistence adapter implementation.
src/fastrest/compat/orm/beanie.py
Agent Outputs
Machine-readable and agent-facing artifacts derived from API metadata.
- Manifest · Machine-readable API description generated from router metadata.
src/fastrest/manifest.py - MCP · Model Context Protocol server exposing resource actions as tools.
src/fastrest/mcp.py - Skills · Generates per-resource and global SKILL.md documentation.
src/fastrest/skills.py - OpenAPI · OpenAPI metadata generation from framework models and routes.
src/fastrest/openapi.py
Other components
- FastAPI app · External application boundary that mounts FastREST routes.
Connections 39
- FastAPI app → Routers: mounts · An application registers FastREST routers to expose resources.
- Routers → Viewsets: builds · Routers instantiate viewsets for resources and actions.
- Routers → Generics: derives CRUD · Model-serving shortcuts create generic CRUD flows.
- Viewsets → Generics: extends · Viewsets reuse generic list/detail mutation behavior.
- Viewsets → Serializers: uses · Resource actions serialize input and output through schemas.
- Generics → Serializers: validates · Generic CRUD flows validate requests and shape responses.
- Serializers → Fields: composes · Serializers are built from field definitions and validators.
- Viewsets → Decorators: annotates · Decorators describe custom resource actions and methods.
- Views → Request: reads · View dispatch consumes framework request context.
- Views → Response: returns · Views emit normalized framework responses.
- Routers → Auth: applies · Routing configuration wires in authentication behavior.
- Routers → Permissions: applies · Routing configuration wires in access rules.
- Routers → Throttling: applies · Routing configuration wires in rate limits.
- Routers → Filters: applies · List endpoints use filtering behavior from router metadata.
- Routers → Pagination: applies · List endpoints use pagination behavior from router metadata.
- Routers → Negotiation: applies · Routers attach content negotiation for response rendering.
- Serializers → OpenAPI: describes · Schema definitions feed OpenAPI generation.
- Routers → Manifest: emits · Router metadata is exported as a machine-readable manifest.
- Routers → MCP: mounts · Router/viewset metadata is exposed through MCP tools.
- Routers → Skills: generates · Resource metadata is rendered into SKILL.md documents.
- Serializers → Manifest: feeds · Serializer contracts contribute to the manifest.
- Viewsets → Manifest: feeds · Viewset actions and routes contribute to the manifest.
- Serializers → MCP: drives · Tool schemas are derived from serializer and action metadata.
- Serializers → Skills: drives · Documentation content is derived from serializer and resource metadata.
- Settings → Routers: configures · Settings control router behavior and defaults.
- Settings → Permissions: defaults · Settings provide default access control policies.
- Settings → Auth: defaults · Settings provide default authentication configuration.
- Settings → Pagination: defaults · Settings provide default pagination configuration.
- Settings → MCP: toggles · Settings enable or disable MCP generation and mounting.
- Settings → Skills: toggles · Settings enable or disable skill generation.
- Routers → ORM base: adapts via · Model-serving flows use the persistence adapter contract.
- ORM base → SQLAlchemy: implements · SQLAlchemy backend implements the common adapter interface.
- ORM base → Tortoise: implements · Tortoise backend implements the common adapter interface.
- ORM base → SQLModel: implements · SQLModel backend implements the common adapter interface.
- ORM base → Beanie: implements · Beanie backend implements the common adapter interface.
- SQLAlchemy → Serializers: feeds models · Adapter metadata informs generated serializers and CRUD endpoints.
- Tortoise → Serializers: feeds models · Adapter metadata informs generated serializers and CRUD endpoints.
- SQLModel → Serializers: feeds models · Adapter metadata informs generated serializers and CRUD endpoints.
- Beanie → Serializers: feeds models · Adapter metadata informs generated serializers and CRUD endpoints.