Custom extractors
Your own extractors, responses and error types join the document by implementing one trait. That is also what makes an undocumented type a compile error: a handler only becomes a route when every argument and its return type can describe itself.
Extractors: DocInput
struct TenantId(String);
impl DocInput for TenantId {
fn describe(cx: &mut DocContext<'_>) {
cx.response(403, "tenant is missing or suspended");
}
}TenantId still implements axum's FromRequestParts as usual; DocInput only adds what it means for the document. Here every route that takes a TenantId gets a 403.
Error types: DocError
A Result<T, E> handler documents the success response of T plus whatever E describes.
impl DocError for FindUserError {
fn responses() -> Vec<(u16, &'static str)> {
vec![(404, "user not found")]
}
fn describe(cx: &mut DocContext<'_>) {
cx.error_responses::<ErrorResponse>(&Self::responses());
}
}Responses: DocOutput
DocOutput is the return-side twin of DocInput. Json<T>, Created<T>, NoContent, (), String, &'static str, Html<T> and Result<T, E: DocError> are already covered; implement it for your own IntoResponse types.
DocContext
DocContext is the builder every describe receives.
| Method | Adds |
|---|---|
body::<T>(content_type) | a request body |
response(status, description) | a response without a body |
json_response::<T>(status, description) | a JSON response |
content_response::<T>(status, content_type) | a response in another media type |
error_responses::<T>(&[(status, description)]) | several error responses sharing one body |
path_params_from::<T>() | path parameters from a struct or tuple |
query_params_from::<T>() | query parameters from a struct |
security_scheme(name, scheme) | a security requirement and its scheme |
require_security(name) | a requirement of a scheme declared elsewhere |
input_schema::<T>(), output_schema::<T>() | a $ref to T as a request or response schema |
operation() | direct access to the openapi::Operation |
A scheme a custom input requires is required for the whole operation: it is added to every alternative of the operation's security, as Auth describes. cx.require_security(name) and cx.operation().require_security(name) do the same; the first also compiles with AXUM_AUTODOC_ENABLED=false, where operation() does not exist.
Error types for the public API live in axum_autodoc::error.