OxyRoute maintains a small OpenAPI 3.0-shaped JSON document in Rust while routes are registered. It is not a full OpenAPI model of every type and body schema; it is a minimal view suitable for discovery and tooling, and can be extended in future versions.
Served vs built: one flag, two ways to set it.
include_openapi(constructor) andset_openapi_served(enabled)both update the same native field (AppState::include_openapi). Use the constructor to choose the default; useset_openapi_served(False)to turn off HTTP serving later (orTrueto re-enable) without recreating the app.- Order does not change the in-memory spec: routes are merged into the OpenAPI document as you register them, no matter when you flip the serving toggle. You can call
set_openapi_servedbefore or after adding routes; only HTTP exposure ofGET/HEAD /openapi.jsonchanges. - Re-enable: an app created with
include_openapi=Falsecan still callset_openapi_served(True)to start serving the spec atGET/HEAD /openapi.json(same for turning back on afterset_openapi_served(False)). - When serving is on (
include_openapi=Trueand not since disabled): the dispatcher answersGET /openapi.jsonandHEAD /openapi.jsonin an early check, before the normal router. A user-registered route on exactly/openapi.jsonwill not run for those methods while the built-in is active. - While serving is off (
include_openapi=Falseat build time, or afterset_openapi_served(False)):- The engine does not return the spec for
GET /openapi.jsonorHEAD /openapi.json. The request is not special-cased, so it goes through normal routing. Unless you add your own handler for that path, the client usually gets 404 Not Found. If you do register a handler for/openapi.json, that handler can serve a custom response. - Route registration still merges operations into the in-memory OpenAPI document. The document is not discarded.
- The engine does not return the spec for
- Export:
openapi_json()on the PythonAppstill returns the current JSON as a string (handy in tests, admin tools, or a custom response), regardless of the serving toggle.
set_openapi_titleis applied fromApp(..., title="...")at construction.openapi_json()is described above; it is independent of whether/openapi.jsonis exposed over HTTP.
Per route, the code records path, method, a short summary / operationId derived from the handler’s __name__, and a simple 200 response placeholder.
For POST, PUT, and PATCH, you can document the JSON request body in OpenAPI in two ways (pass at most one):
body_model=...: a Pydantic v2BaseModelclass. OxyRoute callsmodel_json_schema()at registration time and stores the result underrequestBody→content→application/json→schema. Pydantic is optional at runtime: only needed if you usebody_model($defs/$refappear as Pydantic emits them).body_schema=...: a plain JSON Schema object (dict/ mapping), e.g. from hand-written spec or another library. It is JSON-serialized at registration time and used the same way in OpenAPI. No Pydantic import required.