()
| 24 | |
| 25 | #[test] |
| 26 | fn parse_example() -> Result<(), Box<dyn std::error::Error>> { |
| 27 | let content = |
| 28 | std::fs::read_to_string(env::current_dir()?.join("tests/example/example.yaml"))?; |
| 29 | |
| 30 | let openapi: OpenAPI = OpenAPI::yaml(&content)?; |
| 31 | |
| 32 | // Validate general OpenAPI properties |
| 33 | assert_eq!(openapi.openapi, "3.1.0"); |
| 34 | assert_eq!(openapi.info.title, "Example API"); |
| 35 | assert!(openapi.components.is_some()); |
| 36 | |
| 37 | let components = openapi.components.as_ref().unwrap(); |
| 38 | |
| 39 | // Validate schemas' presence of "oneOf" and "allOf" |
| 40 | let schemas_check = [("ExampleRequest", false), ("ExampleResponse", false)]; |
| 41 | for (schema_name, expected_one_of) in schemas_check.iter() { |
| 42 | let schema = components |
| 43 | .schemas |
| 44 | .get(*schema_name) |
| 45 | .ok_or(format!("Missing schema: {}", *schema_name))?; |
| 46 | assert_eq!(schema.one_of.is_some(), *expected_one_of); |
| 47 | } |
| 48 | |
| 49 | let schemas_check_all_of = [("ExampleRequest", false), ("ExampleResponse", true)]; |
| 50 | for (schema_name, expected_all_of) in schemas_check_all_of.iter() { |
| 51 | let schema = components |
| 52 | .schemas |
| 53 | .get(*schema_name) |
| 54 | .ok_or(format!("Missing schema: {}", *schema_name))?; |
| 55 | assert_eq!(schema.all_of.is_some(), *expected_all_of); |
| 56 | } |
| 57 | |
| 58 | // Validate paths |
| 59 | let example_path = openapi |
| 60 | .paths |
| 61 | .get("/example/{uuid}") |
| 62 | .ok_or("Missing path: /example/{uuid}")?; |
| 63 | let get_value = example_path |
| 64 | .operations |
| 65 | .get("get") |
| 66 | .ok_or("Missing GET method for /example/{uuid}")?; |
| 67 | |
| 68 | // Validate GET parameters |
| 69 | let parameter = get_value |
| 70 | .parameters |
| 71 | .as_ref() |
| 72 | .and_then(|params| params.first()) |
| 73 | .ok_or("Missing parameter")?; |
| 74 | |
| 75 | // Since Parameter is now a struct, access fields directly |
| 76 | assert_eq!(parameter.name.as_deref(), Some("uuid")); |
| 77 | assert_eq!( |
| 78 | parameter.description.as_deref(), |
| 79 | Some("The UUID for this example.") |
| 80 | ); |
| 81 | assert_eq!(parameter.r#in.as_ref(), Some(&In::Path)); |
| 82 | if let Some(schema) = ¶meter.schema { |
| 83 | assert_eq!(schema.r#type, Some(TypeOrUnion::Single(Type::String))); |
nothing calls this directly
no outgoing calls
no test coverage detected