1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! # doxa
//!
//! Ergonomic OpenAPI documentation for axum services. Built on top of
//! [`utoipa`] and [`utoipa_axum`], this crate provides:
//!
//! - An [`ApiDocBuilder`] for assembling an OpenAPI document from a
//! [`utoipa::openapi::OpenApi`] value, finalizing it into an in-memory
//! [`ApiDoc`] whose serialized JSON is shared via a reference-counted
//! [`bytes::Bytes`] buffer.
//! - A [`mount_docs`] helper that mounts `GET /openapi.json` plus an
//! interactive documentation UI on an existing [`axum::Router`], all from
//! memory — the spec is never written to disk.
//! - An RFC 7807 [`ProblemDetails`] response body usable as the default error
//! schema across a project.
//!
//! All UI integrations are feature-gated independently. The default
//! feature set enables [`docs-scalar`](crate#features) which mounts
//! the Scalar API reference UI from a CDN-loaded HTML template,
//! rendered out of the box with the three-pane `modern` layout, dark
//! mode on, the schemas index hidden, the codegen sidebar suppressed,
//! and Scalar's paid product upsells (Agent / MCP) disabled. Every
//! one of those choices is overridable via [`ScalarConfig`] passed
//! through [`MountOpts::scalar`]. Scalar is preferred because it is
//! actively maintained, parses OpenAPI 3.2 natively, renders the
//! `x-badges` vendor extension, and surfaces required OAuth2 scopes
//! inline under each operation — covering per-operation permission
//! requirements produced by extractor-side [`DocOperationSecurity`]
//! impls.
//!
//! # Tour
//!
//! The full surface of the crate, end to end. Every macro, derive,
//! extractor, and builder method that ships in the default feature set
//! appears in the snippet below and the whole thing compiles under
//! `cargo test --doc`.
//!
//! ```no_run
//! use axum::Json;
//! use doxa::{
//! routes, ApiDocBuilder, ApiErrorBody, ApiResult, DocumentedHeader, Header,
//! MountDocsExt, MountOpts, OpenApiRouter, ScalarConfig, ScalarLayout, ScalarTheme,
//! SseEvent, SseEventMeta, SseSpecVersion, SseStream, ToSchema,
//! };
//! use doxa::{get, post, ApiError};
//! use futures_core::Stream;
//! use serde::{Deserialize, Serialize};
//! use std::convert::Infallible;
//!
//! // ----- Typed error envelope ----------------------------------------------
//! //
//! // `#[derive(ApiError)]` wires both `IntoResponse` and `IntoResponses`
//! // from per-variant `#[api(status, code)]` attributes. Multiple
//! // variants may share a status — they are grouped into one OpenAPI
//! // response with separate examples.
//! #[derive(Debug, thiserror::Error, Serialize, ToSchema, ApiError)]
//! enum WidgetError {
//! #[error("validation failed: {0}")]
//! #[api(status = 400, code = "validation_error")]
//! Validation(String),
//!
//! #[error("conflict: {0}")]
//! #[api(status = 400, code = "conflict")]
//! Conflict(String),
//!
//! #[error("not found")]
//! #[api(status = 404, code = "not_found")]
//! NotFound,
//!
//! #[error("internal error")]
//! #[api(status = 500, code = "internal")]
//! Internal,
//! }
//!
//! // ----- Typed request / response bodies -----------------------------------
//! #[derive(Debug, Serialize, ToSchema)]
//! struct Widget { id: u32, name: String }
//!
//! #[derive(Debug, Deserialize, ToSchema)]
//! struct CreateWidget { name: String }
//!
//! // ----- Typed header extractor --------------------------------------------
//! //
//! // Implementing `DocumentedHeader` on a marker type lets the same
//! // marker drive both extraction (via `Header<XApiKey>`) and OpenAPI
//! // documentation. The macro recognizes `Header<H>` in the handler
//! // signature and emits the corresponding params block automatically.
//! struct XApiKey;
//! impl DocumentedHeader for XApiKey {
//! fn name() -> &'static str { "X-Api-Key" }
//! fn description() -> &'static str { "Tenant API key" }
//! }
//!
//! // ----- SSE event stream --------------------------------------------------
//! //
//! // `#[derive(SseEvent)]` provides the per-variant event name; pair
//! // it with `serde::Serialize` and `ToSchema` so the wire format and
//! // OpenAPI schema stay aligned. `SseStream<E, S>` is the response
//! // wrapper — handlers never construct axum's `Sse` directly.
//! #[derive(Serialize, ToSchema, SseEvent)]
//! #[serde(tag = "event", content = "data", rename_all = "snake_case")]
//! enum BuildEvent {
//! Started { id: u64 },
//! Progress { done: u64, total: u64 },
//! #[sse(name = "finished")]
//! Completed,
//! }
//!
//! // ----- Handlers ----------------------------------------------------------
//! /// Create a widget. The path uses the `#[post]` shortcut, takes a
//! /// typed JSON body and a typed header, and returns an
//! /// `ApiResult<Json<T>, E>` so successes and the full error
//! /// vocabulary both flow into the OpenAPI document.
//! #[post("/widgets", tag = "Widgets")]
//! async fn create_widget(
//! Header(_key, ..): Header<XApiKey>,
//! Json(req): Json<CreateWidget>,
//! ) -> ApiResult<(axum::http::StatusCode, Json<Widget>), WidgetError> {
//! if req.name.is_empty() {
//! return Err(WidgetError::Validation("name is required".into()));
//! }
//! Ok((
//! axum::http::StatusCode::CREATED,
//! Json(Widget { id: 42, name: req.name }),
//! ))
//! }
//!
//! /// Stream build progress as Server-Sent Events. The macro
//! /// recognizes `SseStream<E, _>` and emits a `text/event-stream`
//! /// response with one `oneOf` branch per `SseEvent` variant.
//! #[get("/builds/{id}/events", tag = "Builds")]
//! async fn stream_build(
//! ) -> SseStream<BuildEvent, impl Stream<Item = Result<BuildEvent, Infallible>>> {
//! let events = futures::stream::iter(vec![
//! Ok(BuildEvent::Started { id: 1 }),
//! Ok(BuildEvent::Progress { done: 1, total: 10 }),
//! Ok(BuildEvent::Completed),
//! ]);
//! SseStream::new(events)
//! }
//!
//! // ----- Compose, finalize, mount -----------------------------------------
//! # async fn run() {
//! let (router, openapi) = OpenApiRouter::<()>::new()
//! .routes(routes!(create_widget))
//! .routes(routes!(stream_build))
//! .split_for_parts();
//!
//! let api_doc = ApiDocBuilder::new()
//! .title("Widgets API")
//! .version("1.0.0")
//! .description("Tour service")
//! .bearer_security("bearerAuth")
//! .tag_group("Core", ["Widgets"])
//! .tag_group("Streaming", ["Builds"])
//! // Use OpenAPI 3.2 `itemSchema` for SSE responses (the default).
//! .sse_openapi_version(SseSpecVersion::V3_2)
//! .merge(openapi)
//! .build();
//!
//! // Customize the Scalar UI: classic single-column layout with a
//! // light theme, dark mode off. `MountOpts::default()` keeps the
//! // historical three-pane modern dark-mode appearance.
//! let app = router.mount_docs(
//! api_doc,
//! MountOpts::default()
//! .scalar(
//! ScalarConfig::default()
//! .layout(ScalarLayout::Classic)
//! .theme(ScalarTheme::Solarized)
//! .dark_mode(false),
//! ),
//! );
//! # // The body envelope `ApiErrorBody` is the shape every error
//! # // response carries; reference it here so the import is exercised.
//! # let _: ApiErrorBody<()> = ApiErrorBody::new(500, "internal", "boom", ());
//! # let _ = app;
//! # }
//! ```
//!
//! The crate's public surface contains **no project-specific types** —
//! everything is generic over `utoipa`'s native types so it can be lifted
//! into any axum project.
pub use ;
pub use ;
pub use DocHeaderEntry;
pub use DocResponseBody;
pub use ;
pub use Header;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use OpenApiRouterExt;
pub use ;
pub use ;
// Re-export the companion proc-macro crate so consumers only need
// `doxa` on their dependency list to write an event-stream
// handler or derive `ApiError`. Gated on the `macros` feature
// (enabled by default) so the proc-macro compile cost can be opted
// out of when the derives aren't needed.
pub use ;
// Re-export the underlying utoipa types so consumers depend on a single
// crate. Each re-export is explicit (no glob) so the public surface is
// auditable from one place.
pub use OpenApi;
pub use ;
pub use OpenApiRouter;
// Our `routes!` macro wraps `utoipa_axum::routes!` and extends the
// collected schemas with those referenced by handler-argument types
// via [`ApidocHandlerSchemas`]. See [`routes_macro`] for the
// implementation.
/// Convenience alias for handler return types whose error half implements
/// [`IntoResponses`]. Equivalent to [`Result<T, E>`] but signals intent.
pub type ApiResult<T, E> = ;
/// Re-exports used exclusively by the `doxa-macros` proc-macro
/// crate. Not part of the public API — paths inside this module may
/// change between minor versions. The macros reference items here so
/// consumer crates do not need to depend on `tracing` directly just to
/// use `#[derive(ApiError)]`.