rovo 0.1.0

A drop-in replacement for axum::Router with effortless OpenAPI documentation
Documentation
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# rovo

A drop-in replacement for axum's Router that adds automatic OpenAPI documentation using doc comments.

Built on top of [aide](https://github.com/tamasfe/aide), rovo provides a seamless way to document your axum APIs without writing separate documentation functions.

## Features

- ðŸŽŊ **Drop-in replacement**: Use `rovo::Router` instead of `axum::Router` with the exact same API
- 📝 **Doc-comment driven**: Write API docs as Rust doc comments with special annotations
- ✅ **Compile-time validation**: Catches documentation errors at compile time, not runtime
- 🔄 **Method chaining**: Supports `.post()`, `.patch()`, `.delete()` just like axum
- 🚀 **Simplified setup**: Helper methods for Swagger UI and OpenAPI JSON endpoints
- 🏷ïļ **Rich annotations**: Support for tags, security, deprecation, examples, and more
- ⚡ **Type-safe**: Full type checking for response types and examples
- ðŸŠķ **Lightweight**: Minimal overhead over plain axum

## Installation

```toml
[dependencies]
rovo = { version = "0.1", features = ["swagger"] }  # Choose your UI: swagger, redoc, or scalar
aide = { version = "0.15", features = ["axum"] }
axum = "0.8"
schemars = "0.8"
serde = { version = "1.0", features = ["derive"] }
```

### Feature Flags

Rovo supports multiple OpenAPI documentation UIs through feature flags. **Note: No UI is enabled by default** - you must explicitly choose which UI(s) to use:

- **`swagger`** - Enables Swagger UI support
- **`redoc`** - Enables Redoc UI support
- **`scalar`** - Enables Scalar UI support

You can enable one or multiple UIs:

```toml
[dependencies]
# Use Swagger UI
rovo = { version = "0.1", features = ["swagger"] }

# Use Redoc
rovo = { version = "0.1", features = ["redoc"] }

# Use Scalar
rovo = { version = "0.1", features = ["scalar"] }

# Use all three UIs
rovo = { version = "0.1", features = ["swagger", "redoc", "scalar"] }
```

## Quick Start

```rust
use aide::axum::IntoApiResponse;
use aide::openapi::OpenApi;
use axum::{extract::State, response::Json, Extension};
use rovo::{rovo, Router, routing::get};
use schemars::JsonSchema;
use serde::Serialize;

#[derive(Clone)]
struct AppState {}

#[derive(Serialize, JsonSchema)]
struct User {
    id: u64,
    name: String,
}

/// Get user information.
///
/// Returns the current user's profile information.
///
/// @tag users
/// @response 200 Json<User> User profile retrieved successfully.
#[rovo]
async fn get_user(State(_state): State<AppState>) -> impl IntoApiResponse {
    Json(User {
        id: 1,
        name: "Alice".to_string(),
    })
}

async fn serve_api(Extension(api): Extension<OpenApi>) -> axum::Json<OpenApi> {
    axum::Json(api)
}

#[tokio::main]
async fn main() {
    let state = AppState {};

    let mut api = OpenApi::default();
    api.info.title = "My API".to_string();

    let app = Router::new()
        .route("/user", get(get_user))
        .with_swagger("/", "/api.json")
        .with_api_json("/api.json", serve_api)
        .with_state(state)
        .finish_api_with_extension(api);

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();

    axum::serve(listener, app).await.unwrap();
}
```

Visit `http://127.0.0.1:3000` to see your interactive Swagger UI documentation!

## Documentation Annotations

### Basic Structure

```rust
/// Title (first line becomes the summary)
///
/// Description paragraph can span multiple lines
/// and provides detailed information about the endpoint.
///
/// @tag category_name
/// @response 200 Json<ResponseType> Success description
/// @response 404 () Not found description
#[rovo]
async fn handler() -> impl IntoApiResponse {
    // ...
}
```

### Available Annotations

#### `@response <code> <type> <description>`

Document response status codes:

```rust
/// @response 200 Json<User> User found successfully
/// @response 404 () User not found
/// @response 500 Json<ErrorResponse> Internal server error
```

#### `@example <code> <expression>`

Provide example responses:

```rust
/// @response 200 Json<User> User information
/// @example 200 User::default()
```

Or with custom values:

```rust
/// @example 200 User { id: 1, name: "Alice".into(), email: "alice@example.com".into() }
```

#### `@tag <tag_name>`

Group operations by tags (can be used multiple times):

```rust
/// @tag users
/// @tag authentication
```

Tags help organize your API in Swagger UI by grouping related endpoints together.

#### `@security <scheme_name>`

Specify security requirements (can be used multiple times):

```rust
/// @security bearer_auth
/// @security api_key
```

Note: You need to define security schemes in your OpenAPI spec separately.

#### `@id <operation_id>`

Set a custom operation ID (defaults to function name):

```rust
/// @id getUserById
```

#### `@hidden`

Hide an operation from the documentation:

```rust
/// @hidden
```

#### `#[deprecated]`

Use Rust's built-in deprecation attribute to mark endpoints as deprecated:

```rust
/// Old endpoint, use /v2/users instead
#[deprecated]
#[rovo]
async fn old_handler() -> impl IntoApiResponse {
    // ...
}
```

#### `@rovo-ignore`

Stop processing doc comment annotations after this point:

```rust
/// Get user information.
///
/// Returns the current user's profile information.
///
/// @tag users
/// @response 200 Json<User> User found successfully
/// @rovo-ignore
/// Everything after this line is treated as regular documentation
/// and won't be processed for OpenAPI annotations.
/// You can write @anything here and it won't cause errors.
#[rovo]
async fn handler() -> impl IntoApiResponse {
    // ...
}
```

This is useful when you want to include additional developer documentation that shouldn't be part of the API specification.

## Router API

### Creating a Router

```rust
use rovo::Router;

let app = Router::new()
    .route("/path", get(handler))
    .with_state(state);
```

### Method Chaining

Rovo supports the same method chaining as axum:

```rust
use rovo::routing::{get, post, patch, delete};

Router::new()
    .route("/items", get(list_items).post(create_item))
    .route("/items/{id}", get(get_item).patch(update_item).delete(delete_item))
```

### Nesting Routes

```rust
Router::new()
    .nest(
        "/api",
        Router::new()
            .route("/users", get(list_users))
            .route("/posts", get(list_posts))
    )
```

### Adding Documentation UI

#### Swagger UI

```rust
Router::new()
    .route("/users", get(list_users))
    .with_swagger("/docs", "/api.json")  // Swagger UI at /docs
    .with_api_json("/api.json", serve_api)
    .with_state(state)
    .finish_api_with_extension(api)
```

#### Redoc UI

```rust
Router::new()
    .route("/users", get(list_users))
    .with_redoc("/docs", "/api.json")  // Redoc UI at /docs
    .with_api_json("/api.json", serve_api)
    .with_state(state)
    .finish_api_with_extension(api)
```

#### Scalar UI

```rust
Router::new()
    .route("/users", get(list_users))
    .with_scalar("/docs", "/api.json")  // Scalar UI at /docs
    .with_api_json("/api.json", serve_api)
    .with_state(state)
    .finish_api_with_extension(api)
```

#### Multiple UIs

You can serve multiple UIs at different paths:

```rust
Router::new()
    .route("/users", get(list_users))
    .with_swagger("/swagger", "/api.json")  // Swagger UI at /swagger
    .with_redoc("/redoc", "/api.json")      // Redoc UI at /redoc
    .with_scalar("/scalar", "/api.json")    // Scalar UI at /scalar
    .with_api_json("/api.json", serve_api)
    .with_state(state)
    .finish_api_with_extension(api)
```

## Complete Example

See [examples/todo_api.rs](./examples/todo_api.rs) for a full CRUD API with:
- Create, Read, Update, Delete operations
- Swagger UI integration
- Proper error handling
- Request/response validation
- Nested routing

Run it with:

```bash
cargo run -F swagger --example todo_api
# Visit http://127.0.0.1:3000 for Swagger UI
```

## Migration Guide

### From Axum 0.8+

Migrating an existing axum project to rovo is straightforward:

#### Step 1: Update Dependencies

```toml
[dependencies]
# Add these
rovo = "0.1"
aide = { version = "0.15", features = ["axum"] }
schemars = "0.8"

# Keep your existing axum
axum = "0.8"
```

#### Step 2: Replace Router Import

```rust
// Before
use axum::Router;

// After
use rovo::Router;
```

#### Step 3: Update Handler Return Types

```rust
// Before
use axum::response::IntoResponse;
async fn handler() -> impl IntoResponse {
    Json(data)
}

// After
use aide::axum::IntoApiResponse;
async fn handler() -> impl IntoApiResponse {
    Json(data)
}
```

#### Step 4: Add the #[rovo] Macro and Docs

```rust
// Before
async fn get_user(State(state): State<AppState>) -> impl IntoApiResponse {
    Json(user)
}

// After
/// Get user by ID
///
/// @tag users
/// @response 200 Json<User> User found
/// @response 404 () User not found
#[rovo]
async fn get_user(State(state): State<AppState>) -> impl IntoApiResponse {
    Json(user)
}
```

#### Step 5: Update Routing Imports

```rust
// Before
use axum::routing::{get, post};

// After
use rovo::routing::{get, post};
```

#### Step 6: Add OpenAPI Setup

```rust
use aide::openapi::OpenApi;
use axum::Extension;

async fn serve_api(Extension(api): Extension<OpenApi>) -> axum::Json<OpenApi> {
    axum::Json(api)
}

#[tokio::main]
async fn main() {
    let state = AppState::new();

    let mut api = OpenApi::default();
    api.info.title = "My API".to_string();
    api.info.description = Some("API description".to_string());

    let app = Router::new()
        .route("/users", get(list_users))
        // ... your other routes
        .with_swagger("/", "/api.json")
        .with_api_json("/api.json", serve_api)
        .with_state(state)
        .finish_api_with_extension(api);

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();

    axum::serve(listener, app).await.unwrap();
}
```

### Migration Checklist

- [ ] Add `rovo` and `aide` dependencies
- [ ] Change `axum::Router` to `rovo::Router`
- [ ] Change `IntoResponse` to `IntoApiResponse`
- [ ] Add `#[rovo]` macro to handlers
- [ ] Add doc comments with `@response` annotations
- [ ] Change `axum::routing::*` to `rovo::routing::*`
- [ ] Add OpenAPI configuration
- [ ] Add Swagger UI setup
- [ ] Test all endpoints

### Incremental Migration

You can migrate gradually by mixing rovo and aide routing:

```rust
use rovo::routing::get as rovo_get;
use aide::axum::routing::get as aide_get;

Router::new()
    .route("/documented", rovo_get(documented_handler))  // Migrated with #[rovo]
    .route("/legacy", aide_get(legacy_handler))          // Not yet migrated
```

However, we recommend fully migrating to `#[rovo]` for all endpoints to maintain consistency.

## Comparison with aide

| Feature | aide | rovo |
|---------|------|------|
| Documentation location | Separate `_docs` function | With handler (doc comments) |
| Routing API | aide's `api_route()` | Drop-in axum replacement |
| Method chaining | Custom implementation | Native axum syntax |
| Setup complexity | Manual | Helper methods |
| Lines of code per endpoint | ~15-20 | ~5-10 |

## Tips and Best Practices

### Path Parameters

Use structs with `JsonSchema` for proper documentation:

```rust
#[derive(Deserialize, JsonSchema)]
struct UserId {
    /// The unique user identifier
    id: Uuid,
}

#[rovo]
async fn get_user(Path(UserId { id }): Path<UserId>) -> impl IntoApiResponse {
    // ...
}
```

### Complex Response Types

For handlers that return multiple types, use `impl IntoApiResponse`:

```rust
#[rovo]
async fn handler() -> impl IntoApiResponse {
    if condition {
        (StatusCode::OK, Json(data)).into_response()
    } else {
        StatusCode::NOT_FOUND.into_response()
    }
}
```

### Tags for Organization

Use consistent tags to organize your API:

```rust
/// @tag users
/// @tag admin
```

### Security Documentation

Define security schemes in your OpenAPI object:

```rust
use aide::openapi::{SecurityScheme, SecuritySchemeData};

let mut api = OpenApi::default();
api.components.get_or_insert_default()
    .security_schemes
    .insert(
        "bearer_auth".to_string(),
        SecurityScheme {
            data: SecuritySchemeData::Http {
                scheme: "bearer".to_string(),
                bearer_format: Some("JWT".to_string()),
            },
            ..Default::default()
        },
    );
```

Then reference it in handlers:

```rust
/// @security bearer_auth
#[rovo]
async fn protected_handler() -> impl IntoApiResponse {
    // ...
}
```

## Troubleshooting

### Handler doesn't implement required traits

**Error**: "doesn't implement `IntoApiMethodRouter`"

**Solution**: Make sure you added the `#[rovo]` macro to your handler:

```rust
#[rovo]  // Don't forget this!
async fn handler() -> impl IntoApiResponse {
    // ...
}
```

### Type mismatch errors with `.with_state()`

**Error**: Type mismatch when calling `.with_state()`

**Solution**: Add explicit type annotation:

```rust
let router: Router<()> = Router::<AppState>::new()
    .route("/path", get(handler))
    .with_state(state);
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

MIT OR Apache-2.0