astrea 0.3.1

A file-system based routing framework for Axum with automatic code generation
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
//! Integration tests for OpenAPI generation
//!
//! Tests document annotation parsing and metadata extraction
//! in real user scenarios with #[route] macro.

#![cfg(feature = "openapi")]

use astrea::openapi::ParamLocation;
use astrea::prelude::*;

// ---------------------------------------------------------------------------
// Test 1: Auto-summary from first doc line
// 测试 1: 从首行文档自动提取摘要
// ---------------------------------------------------------------------------

#[test]
fn test_auto_summary() {
    mod handler {
        use super::*;

        /// Get user by ID
        /// Retrieves detailed user information from the database.
        #[route]
        pub async fn auto_summary_handler(event: Event) -> Result<Response> {
            let id = get_param_required(&event, "id")?;
            json(json!({ "id": id }))
        }
    }

    let meta = handler::__openapi_meta();

    assert_eq!(meta.summary, Some("Get user by ID".to_string()));
    assert_eq!(
        meta.description,
        Some("Retrieves detailed user information from the database.".to_string())
    );
    assert!(meta.tags.is_empty());
    assert!(!meta.deprecated);
}

// ---------------------------------------------------------------------------
// Test 2: Explicit annotations
// 测试 2: 显式标注
// ---------------------------------------------------------------------------

#[test]
fn test_explicit_annotations() {
    mod handler {
        use super::*;

        /// @summary List all users
        /// @description Returns a paginated list of all users in the system.
        /// @tag Users
        /// @tag Admin
        /// @security bearer
        #[route]
        pub async fn explicit_annotations_handler(event: Event) -> Result<Response> {
            let page: u32 = get_query_param(&event, "page")
                .and_then(|s| s.parse().ok())
                .unwrap_or(1);
            json(json!({ "page": page, "users": [] }))
        }
    }

    let meta = handler::__openapi_meta();

    assert_eq!(meta.summary, Some("List all users".to_string()));
    assert_eq!(
        meta.description,
        Some("Returns a paginated list of all users in the system.".to_string())
    );
    assert_eq!(meta.tags, vec!["Users", "Admin"]);
    assert_eq!(meta.security, vec!["bearer"]);
    assert!(!meta.deprecated);
}

// ---------------------------------------------------------------------------
// Test 3: Deprecated endpoint
// 测试 3: 已弃用的端点
// ---------------------------------------------------------------------------

#[test]
fn test_deprecated_endpoint() {
    mod handler {
        use super::*;

        /// Old API endpoint
        /// This endpoint is deprecated. Use /v2/users instead.
        /// @deprecated
        /// @tag Legacy
        #[route]
        pub async fn deprecated_handler(_event: Event) -> Result<Response> {
            Ok::<_, RouteError>(text("This endpoint is deprecated"))
        }
    }

    let meta = handler::__openapi_meta();

    assert_eq!(meta.summary, Some("Old API endpoint".to_string()));
    assert!(meta.deprecated);
    assert_eq!(meta.tags, vec!["Legacy"]);
}

// ---------------------------------------------------------------------------
// Test 4: Multiple response codes
// 测试 4: 多状态码响应
// ---------------------------------------------------------------------------

#[test]
fn test_multiple_responses() {
    mod handler {
        use super::*;

        /// Update user profile
        /// @tag Users
        /// @security bearer
        /// @response 200 Profile updated successfully
        /// @response 400 Invalid input data
        /// @response 401 Authentication required
        /// @response 404 User not found
        #[route]
        pub async fn multiple_responses_handler(event: Event) -> Result<Response> {
            let id = get_param_required(&event, "id")?;
            json(json!({ "id": id, "updated": true }))
        }
    }

    let meta = handler::__openapi_meta();

    assert_eq!(meta.summary, Some("Update user profile".to_string()));
    assert_eq!(meta.responses.len(), 4);
    assert_eq!(
        meta.responses[0],
        (
            "200".to_string(),
            "Profile updated successfully".to_string()
        )
    );
    assert_eq!(
        meta.responses[1],
        ("400".to_string(), "Invalid input data".to_string())
    );
    assert_eq!(
        meta.responses[2],
        ("401".to_string(), "Authentication required".to_string())
    );
    assert_eq!(
        meta.responses[3],
        ("404".to_string(), "User not found".to_string())
    );
}

// ---------------------------------------------------------------------------
// Test 5: Path and query parameters
// 测试 5: 路径和查询参数
// ---------------------------------------------------------------------------

#[test]
fn test_parameters_extraction() {
    mod handler {
        use super::*;

        /// Get user posts
        /// Returns all posts by a specific user with optional filtering.
        /// @tag Users
        /// @tag Posts
        #[route]
        pub async fn params_handler(event: Event) -> Result<Response> {
            let user_id = get_param_required(&event, "user_id")?;
            let limit: u32 = get_query_param(&event, "limit")
                .unwrap_or("10".to_string())
                .parse::<u32>()
                .unwrap_or(10);
            let offset: u32 = get_query_param(&event, "offset")
                .unwrap_or("0".to_string())
                .parse::<u32>()
                .unwrap_or(0);

            json(json!({
                "user_id": user_id,
                "limit": limit,
                "offset": offset,
                "posts": []
            }))
        }
    }

    let meta = handler::__openapi_meta();

    assert_eq!(meta.parameters.len(), 3);

    // Path parameter
    let user_id_param = &meta.parameters[0];
    assert_eq!(user_id_param.name, "user_id");
    assert!(user_id_param.required);
    match user_id_param.location {
        ParamLocation::Path => {}
        _ => panic!("Expected path parameter"),
    }

    // Query parameters (detected from parse::<u32>())
    let limit_param = &meta.parameters[1];
    assert_eq!(limit_param.name, "limit");
    assert!(!limit_param.required); // get_query_param is optional
    match limit_param.location {
        ParamLocation::Query => {}
        _ => panic!("Expected query parameter"),
    }
    assert_eq!(limit_param.schema_type, "integer");
    assert_eq!(limit_param.schema_format, Some("uint32".to_string()));
}

// ---------------------------------------------------------------------------
// Test 6: Request body detection
// 测试 6: 请求体检测
// ---------------------------------------------------------------------------

#[test]
fn test_request_body_detection() {
    mod handler {
        use super::*;
        use serde::{Deserialize, Serialize};

        #[derive(Debug, Serialize, Deserialize)]
        #[allow(dead_code)] // Struct is used by #[route] macro expansion
        pub struct CreateUserRequest {
            pub username: String,
            pub email: String,
        }

        /// Create a new user
        /// @tag Users
        /// @tag Authentication
        /// @response 201 User created successfully
        /// @response 409 Email already exists
        #[route]
        pub async fn create_user_handler(event: Event) -> Result<Response> {
            let body: CreateUserRequest = get_body(&event)?;
            json(json!({
                "username": body.username,
                "email": body.email,
                "id": 123
            }))
        }
    }

    let meta = handler::__openapi_meta();

    assert!(meta.request_body.is_some());
    let body = meta.request_body.unwrap();
    assert_eq!(body.schema_type_name, "CreateUserRequest");
    assert_eq!(body.content_type, "application/json");
}

// ---------------------------------------------------------------------------
// Test 7: Response content type detection
// 测试 7: 响应内容类型检测
// ---------------------------------------------------------------------------

#[test]
fn test_response_content_types() {
    mod html_mod {
        use super::*;

        /// Get HTML page
        /// @tag Frontend
        #[route]
        pub async fn html_handler(_event: Event) -> Result<Response> {
            Ok::<_, RouteError>(html("<h1>Hello World</h1>"))
        }
    }

    mod text_mod {
        use super::*;

        /// Get plain text
        /// @tag Frontend
        #[route]
        pub async fn text_handler(_event: Event) -> Result<Response> {
            Ok::<_, RouteError>(text("Hello World"))
        }
    }

    mod bytes_mod {
        use super::*;

        /// Download binary file
        /// @tag Files
        #[route]
        pub async fn bytes_handler(_event: Event) -> Result<Response> {
            Ok::<_, RouteError>(bytes(vec![0x48, 0x65, 0x6c, 0x6c, 0x6f]))
        }
    }

    let html_meta = html_mod::__openapi_meta();
    assert_eq!(html_meta.response_content_type, "text/html");

    let text_meta = text_mod::__openapi_meta();
    assert_eq!(text_meta.response_content_type, "text/plain");

    let bytes_meta = bytes_mod::__openapi_meta();
    assert_eq!(bytes_meta.response_content_type, "application/octet-stream");
}

// ---------------------------------------------------------------------------
// Test 8: JSON response schema fields
// 测试 8: JSON 响应模式字段
// ---------------------------------------------------------------------------

#[test]
fn test_json_response_schema() {
    mod handler {
        use super::*;

        /// Get user statistics
        /// @tag Analytics
        #[route]
        pub async fn json_schema_handler(event: Event) -> Result<Response> {
            let id = get_param_required(&event, "id")?;
            json(json!({
                "user_id": id,
                "total_posts": 42,
                "total_likes": 128,
                "joined_at": "2024-01-01T00:00:00Z"
            }))
        }
    }

    let meta = handler::__openapi_meta();

    assert_eq!(meta.response_content_type, "application/json");
    assert_eq!(meta.response_schema_fields.len(), 4);
    assert!(meta.response_schema_fields.contains(&"user_id".to_string()));
    assert!(
        meta.response_schema_fields
            .contains(&"total_posts".to_string())
    );
    assert!(
        meta.response_schema_fields
            .contains(&"total_likes".to_string())
    );
    assert!(
        meta.response_schema_fields
            .contains(&"joined_at".to_string())
    );
}

// ---------------------------------------------------------------------------
// Test 9: Complex real-world scenario
// 测试 9: 复杂的真实场景
// ---------------------------------------------------------------------------

#[test]
fn test_complex_real_world_scenario() {
    mod handler {
        use super::*;
        use serde::Deserialize;

        /// Update user profile
        ///
        /// Updates the authenticated user's profile information.
        /// Only the fields provided in the request body will be updated.
        /// All other fields remain unchanged.
        ///
        /// @tag Users
        /// @tag Profile
        /// @security bearer
        /// @response 200 Profile updated successfully
        /// @response 400 Invalid input data
        /// @response 401 Authentication required
        /// @response 403 Forbidden
        /// @response 404 User not found
        #[route]
        pub async fn complex_handler(event: Event) -> Result<Response> {
            let user_id = get_param_required(&event, "user_id")?;
            let version: u32 = get_query_param_required(&event, "version")?
                .parse()
                .map_err(|e| RouteError::bad_request(format!("Invalid version: {e}")))?;

            #[derive(Deserialize)]
            struct UpdateRequest {
                display_name: Option<String>,
                bio: Option<String>,
            }

            let req: UpdateRequest = get_body(&event)?;

            // Manually list fields that were provided in request
            let mut updated_fields = Vec::new();
            if req.display_name.is_some() {
                updated_fields.push("display_name");
            }
            if req.bio.is_some() {
                updated_fields.push("bio");
            }

            json(json!({
                "user_id": user_id,
                "version": version,
                "updated_fields": updated_fields,
                "success": true
            }))
        }
    }

    let meta = handler::__openapi_meta();

    // Summary and description
    assert_eq!(meta.summary, Some("Update user profile".to_string()));
    assert!(meta.description.is_some());
    let desc = meta.description.unwrap();
    assert!(desc.contains("authenticated user's profile"));
    assert!(desc.contains("Only the fields provided"));

    // Tags
    assert_eq!(meta.tags, vec!["Users", "Profile"]);

    // Security
    assert_eq!(meta.security, vec!["bearer"]);

    // Parameters
    assert_eq!(meta.parameters.len(), 2);
    assert_eq!(meta.parameters[0].name, "user_id");
    assert!(meta.parameters[0].required);
    assert_eq!(meta.parameters[1].name, "version");
    assert!(meta.parameters[1].required);

    // Request body
    assert!(meta.request_body.is_some());

    // Responses
    assert_eq!(meta.responses.len(), 5);
    assert_eq!(meta.responses[0].0, "200");
    assert_eq!(meta.responses[4].0, "404");

    // Response content type and schema
    assert_eq!(meta.response_content_type, "application/json");
    assert_eq!(meta.response_schema_fields.len(), 4);
}

// ---------------------------------------------------------------------------
// Test 10: No documentation at all
// 测试 10: 完全没有文档
// ---------------------------------------------------------------------------

#[test]
fn test_no_documentation() {
    mod handler {
        use super::*;

        #[route]
        pub async fn no_docs_handler(event: Event) -> Result<Response> {
            let id = get_param(&event, "id").unwrap_or("default");
            json(json!({ "id": id }))
        }
    }

    let meta = handler::__openapi_meta();

    assert_eq!(meta.summary, None);
    assert_eq!(meta.description, None);
    assert!(meta.tags.is_empty());
    assert!(meta.security.is_empty());
    assert!(!meta.deprecated);
    assert!(meta.responses.is_empty());

    // But parameters are still extracted from code
    assert_eq!(meta.parameters.len(), 1);
    assert_eq!(meta.parameters[0].name, "id");
}

// ---------------------------------------------------------------------------
// Test 11: Multiple security schemes
// 测试 11: 多种安全方案
// ---------------------------------------------------------------------------

#[test]
fn test_multiple_security_schemes() {
    mod handler {
        use super::*;

        /// Admin-only endpoint
        /// @tag Admin
        /// @security bearer
        /// @security apiKey
        /// @security oauth2
        #[route]
        pub async fn multi_security_handler(_event: Event) -> Result<Response> {
            json(json!({ "status": "authorized" }))
        }
    }

    let meta = handler::__openapi_meta();

    assert_eq!(meta.security, vec!["bearer", "apiKey", "oauth2"]);
}

// ---------------------------------------------------------------------------
// Test 12: No content response
// 测试 12: 无内容响应
// ---------------------------------------------------------------------------

#[test]
fn test_no_content_response() {
    mod handler {
        use super::*;

        /// Delete user
        /// @tag Users
        /// @security bearer
        /// @response 204 User deleted successfully
        /// @response 404 User not found
        #[route]
        pub async fn no_content_handler(_event: Event) -> Result<Response> {
            Ok::<_, RouteError>(no_content())
        }
    }

    let meta = handler::__openapi_meta();

    assert_eq!(meta.response_content_type, "none");
    assert!(meta.response_schema_fields.is_empty());
}