rovo 0.4.4

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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
use rovo::aide::axum::IntoApiResponse;
use rovo::extract::{Path, State};
use rovo::http::StatusCode;
use rovo::response::Json;
use rovo::schemars::JsonSchema;
use rovo::{routing::get, rovo, Router};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Clone)]
struct AppState {}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
struct TodoItem {
    id: Uuid,
    title: String,
    completed: bool,
}

impl Default for TodoItem {
    fn default() -> Self {
        Self {
            id: Uuid::nil(),
            title: "Buy milk".into(),
            completed: false,
        }
    }
}

#[derive(Serialize, JsonSchema)]
struct ErrorResponse {
    error: String,
    code: String,
}

#[allow(dead_code)]
#[derive(Deserialize, JsonSchema)]
struct TodoId {
    id: Uuid,
}

// Test 1: Single-line responses
/// Get a todo item.
///
/// # Responses
///
/// 200: Json<TodoItem> - Successfully retrieved the todo item
/// 404: () - Todo item was not found
#[rovo]
async fn get_todo_single_line(
    State(_app): State<AppState>,
    Path(_id): Path<TodoId>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

// Test 2: Multi-line response descriptions
/// Get a todo item with detailed info.
///
/// # Responses
///
/// 200: Json<TodoItem> - Successfully retrieved the todo item from the
///      database with all associated metadata
/// 404: () - Todo item was not found in the database or has been
///      deleted by another user
/// 500: Json<ErrorResponse> - Internal server error occurred while
///      processing the request
#[rovo]
async fn get_todo_multiline_desc(
    State(_app): State<AppState>,
    Path(_id): Path<TodoId>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

// Test 3: Single-line examples
/// Create a todo item with examples.
///
/// # Responses
///
/// 201: Json<TodoItem> - Todo item created successfully
/// 400: Json<ErrorResponse> - Invalid input data
///
/// # Examples
///
/// 201: TodoItem { id: Uuid::nil(), title: "Buy milk".into(), completed: false }
/// 400: ErrorResponse { error: "Title cannot be empty".into(), code: "VALIDATION_ERROR".into() }
#[rovo]
async fn create_todo_single_line(
    State(_app): State<AppState>,
    Json(_input): Json<TodoItem>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

// Test 4: Multi-line examples
/// Create a todo item with multi-line examples.
///
/// # Responses
///
/// 201: Json<TodoItem> - Todo item created successfully
/// 400: Json<ErrorResponse> - Invalid input data
///
/// # Examples
///
/// 201: TodoItem {
///          id: Uuid::nil(),
///          title: "Buy milk".into(),
///          completed: false
///      }
/// 400: ErrorResponse {
///          error: "Title cannot be empty".into(),
///          code: "VALIDATION_ERROR".into()
///      }
#[rovo]
async fn create_todo_multiline(
    State(_app): State<AppState>,
    Json(_input): Json<TodoItem>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

// Test 5: Primitive type examples
/// Get status with primitive examples.
///
/// # Responses
///
/// 200: Json<String> - Operation successful
/// 201: Json<i32> - Count of items
/// 202: Json<bool> - Validation result
/// 203: Json<f64> - Progress percentage
///
/// # Examples
///
/// 200: "success"
/// 201: 42
/// 202: true
/// 203: 99.9
#[rovo]
async fn get_status_primitives(State(_app): State<AppState>) -> impl IntoApiResponse {
    Json("success")
}

// Test 6: Metadata section with single tag
/// List todos with single tag.
///
/// # Responses
///
/// 200: Json<Vec<TodoItem>> - List of todo items
///
/// # Metadata
///
/// @tag todos
#[rovo]
async fn list_todos_single_tag(State(_app): State<AppState>) -> impl IntoApiResponse {
    Json(Vec::<TodoItem>::new())
}

// Test 7: Metadata section with multiple tags
/// List todos with multiple tags.
///
/// # Responses
///
/// 200: Json<Vec<TodoItem>> - List of todo items
///
/// # Metadata
///
/// @tag todos
/// @tag lists
#[rovo]
async fn list_todos_multiple_tags(State(_app): State<AppState>) -> impl IntoApiResponse {
    Json(Vec::<TodoItem>::new())
}

// Test 8: Metadata with security
/// Protected endpoint.
///
/// # Responses
///
/// 200: Json<TodoItem> - Success
/// 401: () - Unauthorized
///
/// # Metadata
///
/// @tag todos
/// @security bearer_auth
#[rovo]
async fn get_protected_todo(
    State(_app): State<AppState>,
    Path(_id): Path<TodoId>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

// Test 9: Metadata with custom operation ID
/// Get todo with custom ID.
///
/// # Responses
///
/// 200: Json<TodoItem> - Success
///
/// # Metadata
///
/// @id get_todo_by_id
/// @tag todos
#[rovo]
async fn get_todo_custom_id(
    State(_app): State<AppState>,
    Path(_id): Path<TodoId>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

// Test 10: Metadata with hidden
/// Internal endpoint.
///
/// # Responses
///
/// 200: Json<String> - Success
///
/// # Metadata
///
/// @hidden
#[rovo]
async fn internal_endpoint(State(_app): State<AppState>) -> impl IntoApiResponse {
    Json("internal")
}

// Test 11: Complete example with all sections
/// Create a todo item with full documentation.
///
/// Creates a new todo item in the database. The title must be non-empty
/// and the item starts in an incomplete state by default.
///
/// # Responses
///
/// 201: Json<TodoItem> - Todo item created successfully
/// 400: Json<ErrorResponse> - Invalid input data provided
/// 401: () - Authentication required
/// 500: Json<ErrorResponse> - Internal server error
///
/// # Examples
///
/// 201: TodoItem {
///     id: Uuid::nil(),
///     title: "Buy groceries".into(),
///     completed: false,
/// }
/// 400: ErrorResponse {
///     error: "Title cannot be empty".into(),
///     code: "VALIDATION_ERROR".into(),
/// }
///
/// # Metadata
///
/// @id create_todo_item
/// @tag todos
/// @security bearer_auth
#[rovo]
async fn create_todo_complete(
    State(_app): State<AppState>,
    Json(_input): Json<TodoItem>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

// Test 12: Using @rovo-ignore
/// Experimental endpoint.
///
/// # Responses
///
/// 200: Json<String> - Success
///
/// # Metadata
///
/// @tag experimental
///
/// @rovo-ignore
///
/// TODO: Add more response types
/// TODO: Add authentication
/// @invalid_annotation this won't cause errors
#[rovo]
async fn experimental_endpoint(State(_app): State<AppState>) -> impl IntoApiResponse {
    Json("experimental")
}

// Test 13: 204 No Content response
/// Delete a todo item.
///
/// # Responses
///
/// 204: () - Todo item deleted successfully
/// 404: () - Todo item not found
///
/// # Metadata
///
/// @tag todos
#[rovo]
async fn delete_todo(
    State(_app): State<AppState>,
    Path(_id): Path<TodoId>,
) -> impl IntoApiResponse {
    StatusCode::NO_CONTENT
}

// Test 14: Complex nested response types
/// Get list of todo items.
///
/// # Responses
///
/// 200: Json<Vec<TodoItem>> - List of all todo items
/// 404: () - Not found
///
/// # Examples
///
/// 200: vec![
///     TodoItem {
///         id: Uuid::nil(),
///         title: "Task 1".into(),
///         completed: false,
///     },
///     TodoItem {
///         id: Uuid::nil(),
///         title: "Task 2".into(),
///         completed: true,
///     }
/// ]
///
/// # Metadata
///
/// @tag todos
#[rovo]
async fn get_nested_response(State(_app): State<AppState>) -> impl IntoApiResponse {
    Json(Vec::<TodoItem>::new())
}

#[test]
fn test_single_line_responses() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todo/{id}", get(get_todo_single_line))
        .with_state(_state)
        .finish();
}

#[test]
fn test_multiline_responses() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todo/{id}", get(get_todo_multiline_desc))
        .with_state(_state)
        .finish();
}

#[test]
fn test_single_line_examples() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todo", get(create_todo_single_line))
        .with_state(_state)
        .finish();
}

#[test]
fn test_multiline_examples() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todo", get(create_todo_multiline))
        .with_state(_state)
        .finish();
}

#[test]
fn test_primitive_examples() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/status", get(get_status_primitives))
        .with_state(_state)
        .finish();
}

#[test]
fn test_metadata_single_tag() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todos", get(list_todos_single_tag))
        .with_state(_state)
        .finish();
}

#[test]
fn test_metadata_multiple_tags() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todos", get(list_todos_multiple_tags))
        .with_state(_state)
        .finish();
}

#[test]
fn test_metadata_with_security() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todo/{id}", get(get_protected_todo))
        .with_state(_state)
        .finish();
}

#[test]
fn test_metadata_custom_id() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todo/{id}", get(get_todo_custom_id))
        .with_state(_state)
        .finish();
}

#[test]
fn test_metadata_hidden() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/internal", get(internal_endpoint))
        .with_state(_state)
        .finish();
}

#[test]
fn test_complete_documentation() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todo", get(create_todo_complete))
        .with_state(_state)
        .finish();
}

#[test]
fn test_rovo_ignore() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/experimental", get(experimental_endpoint))
        .with_state(_state)
        .finish();
}

#[test]
fn test_delete_endpoint() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todo/{id}", get(delete_todo))
        .with_state(_state)
        .finish();
}

#[test]
fn test_nested_response_types() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/nested", get(get_nested_response))
        .with_state(_state)
        .finish();
}

// Test: Examples starting on next line
/// Get todo with example on next line.
///
/// # Path Parameters
///
/// id: The todo ID
///
/// # Responses
///
/// 200: Json<TodoItem> - Success
///
/// # Examples
///
/// 200:
/// TodoItem {
///     id: Uuid::nil(),
///     title: "Buy milk".into(),
///     completed: false
/// }
#[rovo]
#[allow(unused_variables)]
async fn get_todo_next_line_example(
    State(_app): State<AppState>,
    Path(id): Path<Uuid>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

#[test]
fn test_example_starts_next_line() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todos/{id}", get(get_todo_next_line_example))
        .with_state(_state)
        .finish();
}

// Test: Unmarked code blocks
/// Get todo with unmarked code block.
///
/// # Responses
///
/// 200: Json<TodoItem> - Success
///
/// # Examples
///
/// 200:
/// ```
/// TodoItem {
///     id: Uuid::nil(),
///     title: "Buy milk".into(),
///     completed: false
/// }
/// ```
#[rovo]
async fn get_todo_code_block_unmarked(
    State(_app): State<AppState>,
    Path(_id): Path<Uuid>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

#[test]
fn test_code_block_unmarked() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todos/{id}", get(get_todo_code_block_unmarked))
        .with_state(_state)
        .finish();
}

// Test: Code block marked with rust
/// Get todo with rust-marked code block.
///
/// # Responses
///
/// 200: Json<TodoItem> - Success
///
/// # Examples
///
/// 200:
/// ```rust
/// TodoItem {
///     id: Uuid::nil(),
///     title: "Buy milk".into(),
///     completed: false
/// }
/// ```
#[rovo]
async fn get_todo_code_block_rust(
    State(_app): State<AppState>,
    Path(_id): Path<Uuid>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

#[test]
fn test_code_block_rust() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todos/{id}", get(get_todo_code_block_rust))
        .with_state(_state)
        .finish();
}

// Test: Code block marked with rs
/// Get todo with rs-marked code block.
///
/// # Responses
///
/// 200: Json<TodoItem> - Success
///
/// # Examples
///
/// 200:
/// ```rs
/// TodoItem {
///     id: Uuid::nil(),
///     title: "Buy milk".into(),
///     completed: false
/// }
/// ```
#[rovo]
async fn get_todo_code_block_rs(
    State(_app): State<AppState>,
    Path(_id): Path<Uuid>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

#[test]
fn test_code_block_rs() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todos/{id}", get(get_todo_code_block_rs))
        .with_state(_state)
        .finish();
}

// Test: Code block on same line as status code
/// Get todo with code block on same line.
///
/// # Responses
///
/// 200: Json<TodoItem> - Success
///
/// # Examples
///
/// 200: ```
/// TodoItem {
///     id: Uuid::nil(),
///     title: "Buy milk".into(),
///     completed: false
/// }
/// ```
#[rovo]
async fn get_todo_code_block_same_line(
    State(_app): State<AppState>,
    Path(_id): Path<Uuid>,
) -> impl IntoApiResponse {
    Json(TodoItem::default())
}

#[test]
fn test_code_block_same_line() {
    let _state = AppState {};
    let _router: ::axum::Router = Router::<AppState>::new()
        .route("/todos/{id}", get(get_todo_code_block_same_line))
        .with_state(_state)
        .finish();
}