Skip to main content

Module openapi

Module openapi 

Source
Expand description

OpenAPI 3.1 doc generation. Behind the openapi feature.

Walk a built Router and emit a serde_json::Value shaped like an OpenAPI 3.1 document. The generator pulls structural data directly from the route tree — every (mount_path, RouteDef) pair the #[controller] and app_routes! macros recorded — so the spec reflects the code, not a hand-maintained YAML file.

use actus::prelude::*;
use actus::openapi;

let router = init().await?;
let spec = openapi::generate(
    &router,
    &openapi::Options::new("My API", "1.0.0").description("…"),
    // Document only `/api/...` — hide internal mounts.
    |mount| mount.starts_with("api/"),
);
println!("{}", openapi::to_string_pretty(&spec));

§Scope

  • Mapping is structural, not semantic. Verbs, path params, query params (typed, with defaults, optional Vec<String>), JSON / Bytes request bodies, and the handler’s /// doc as summary + description. No response-body schema is inferred — handlers can return anything, and the framework’s Reply shape doesn’t carry that information. Operations get a default response with a generic description; if you need richer responses, post-process the generated Value.
  • Trailing rest parameters ({...name}) don’t have a clean OpenAPI form — the spec’s path templating is a single segment per {name}. The generator strips the ... and adds x-actus-rest-param: true plus a description noting “captures the trailing path (slashes included)” on the parameter, so clients and tooling can recognise it if they want to.
  • DEFAULT_VERBS routes (no verb prefix in routes! — accepts GET and POST) emit two operations on the path, one per verb.
  • Route selection. The filter predicate runs on the mount path (the controller’s prefix, no leading slash, no trailing slash). A route is included iff its controller’s mount passes the predicate. The flexible form is a closure; the most common shape is |mount| mount.starts_with("api/").

Structs§

Options
Top-level options for the generated spec. The OpenAPI info object plus an optional servers list.
ServerInfo
One entry in the OpenAPI servers array — a base URL the API is reachable at, plus an optional human description.

Functions§

generate
Walk router and emit an OpenAPI 3.1 Value. filter is consulted with the mount path of each controller (no leading or trailing slash); only routes from controllers whose mount passes are included.
to_string_pretty
Pretty-printed JSON of a generated spec. Convenience for serving the document at e.g. /openapi.json.