| 17 | |
| 18 | impl OpenAPIGenerator { |
| 19 | pub fn generate(routes: &[Route]) -> Spec { |
| 20 | let mut paths = BTreeMap::new(); |
| 21 | let mut tags = Vec::new(); |
| 22 | |
| 23 | for route in routes { |
| 24 | let route_name = route.prefix.trim_matches('/').to_string(); |
| 25 | |
| 26 | if !route_name.is_empty() { |
| 27 | tags.push(Tag { |
| 28 | name: route_name.clone(), |
| 29 | description: Some(route.docs.clone()), |
| 30 | extensions: BTreeMap::new(), |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | for endpoint in &route.endpoints { |
| 35 | for path_spec in &endpoint.path_specs { |
| 36 | let path_item = Self::create_path_item(route, endpoint, path_spec); |
| 37 | let path = if route.prefix.starts_with('/') { |
| 38 | route.prefix.clone() |
| 39 | } else { |
| 40 | format!("/{}", route.prefix) |
| 41 | }; |
| 42 | let full_path = if path_spec.path.starts_with('/') { |
| 43 | format!("{}{}", path, path_spec.path) |
| 44 | } else { |
| 45 | format!("{}/{}", path, path_spec.path) |
| 46 | }; |
| 47 | // Skip hidden routes if applicable |
| 48 | if route.annotation.docs.as_ref().is_some_and(|d| d.hidden) { |
| 49 | continue; |
| 50 | } |
| 51 | paths.insert(full_path.replace("//", "/"), path_item); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | Spec { |
| 57 | openapi: "3.0.3".to_string(), |
| 58 | info: Info { |
| 59 | title: "API Documentation".to_string(), |
| 60 | summary: None, |
| 61 | description: Some("API Documentation generated from routes".to_string()), |
| 62 | terms_of_service: None, |
| 63 | version: "1.0.0".to_string(), |
| 64 | contact: None, |
| 65 | license: None, |
| 66 | extensions: BTreeMap::new(), |
| 67 | }, |
| 68 | servers: vec![Server { |
| 69 | url: "/".to_string(), |
| 70 | description: None, |
| 71 | variables: BTreeMap::new(), |
| 72 | }], |
| 73 | paths: Some(paths), |
| 74 | components: Some(Self::create_default_components()), |
| 75 | tags, |
| 76 | webhooks: BTreeMap::new(), |