diff --git a/crates/oapi/src/openapi/example.rs b/crates/oapi/src/openapi/example.rs index e082f0ad0..b2ff8a962 100644 --- a/crates/oapi/src/openapi/example.rs +++ b/crates/oapi/src/openapi/example.rs @@ -76,3 +76,30 @@ impl Example { self } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_example() { + let example = Example::new(); + assert!(example.summary.is_empty()); + assert!(example.description.is_empty()); + assert!(example.value.is_none()); + assert!(example.external_value.is_empty()); + + let example = example.summary("summary"); + assert!(example.summary == "summary"); + + let example = example.description("description"); + assert!(example.description == "description"); + + let example = example.external_value("external_value"); + assert!(example.external_value == "external_value"); + + let example = example.value(serde_json::Value::String("value".to_string())); + assert!(example.value.is_some()); + assert!(example.value.unwrap() == serde_json::Value::String("value".to_string())); + } +} diff --git a/crates/oapi/src/openapi/request_body.rs b/crates/oapi/src/openapi/request_body.rs index 8b49f1b45..a0629de1f 100644 --- a/crates/oapi/src/openapi/request_body.rs +++ b/crates/oapi/src/openapi/request_body.rs @@ -93,8 +93,7 @@ mod tests { "application/json", Content::new(crate::Ref::from_schema_name("EmailPayload")), ); - let serialized = serde_json::to_string_pretty(&request_body)?; - println!("serialized json:\n {serialized}"); + assert_json_eq!( request_body, json!({ @@ -111,4 +110,32 @@ mod tests { ); Ok(()) } + + #[test] + fn request_body_merge() { + let mut request_body = RequestBody::new(); + let other_request_body = RequestBody::new() + .description("Merged requestBody") + .required(Required::True) + .add_content( + "application/json", + Content::new(crate::Ref::from_schema_name("EmailPayload")), + ); + + request_body.merge(other_request_body); + assert_json_eq!( + request_body, + json!({ + "description": "Merged requestBody", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/EmailPayload" + } + } + }, + "required": true + }) + ); + } } diff --git a/crates/oapi/src/openapi/response.rs b/crates/oapi/src/openapi/response.rs index e353bb999..b366575d6 100644 --- a/crates/oapi/src/openapi/response.rs +++ b/crates/oapi/src/openapi/response.rs @@ -187,7 +187,7 @@ impl From for RefOr { #[cfg(test)] mod tests { - use super::{Content, Response, Responses}; + use super::{BTreeMap, Content, Header, Ref, RefOr, Response, Responses}; use assert_json_diff::assert_json_eq; use serde_json::json; @@ -199,25 +199,114 @@ mod tests { #[test] fn response_builder() -> Result<(), serde_json::Error> { - let request_body = Response::new("A sample response").add_content( - "application/json", - Content::new(crate::Ref::from_schema_name("MySchemaPayload")), - ); - let serialized = serde_json::to_string_pretty(&request_body)?; - println!("serialized json:\n {serialized}"); + let request_body = Response::new("A sample response") + .description("A sample response description") + .add_content( + "application/json", + Content::new(crate::Ref::from_schema_name("MySchemaPayload")), + ) + .add_header("content-type", Header::default().description("application/json")); + assert_json_eq!( request_body, json!({ - "description": "A sample response", + "description": "A sample response description", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/MySchemaPayload" } } + }, + "headers": { + "content-type": { + "description": "application/json", + "schema": { + "type": "string" + } + } } }) ); Ok(()) } + + #[test] + fn test_responses_from_btree_map() { + let input = BTreeMap::from([ + ("response1".to_string(), Response::new("response1")), + ("response2".to_string(), Response::new("response2")), + ]); + + let expected = Responses { + 0: BTreeMap::from([ + ("response1".to_string(), RefOr::T(Response::new("response1"))), + ("response2".to_string(), RefOr::T(Response::new("response2"))), + ]), + }; + + let actual = Responses::from(input); + + assert_eq!(expected, actual); + } + + #[test] + fn test_responses_from_kv_sequence() { + let input = [ + ("response1".to_string(), Response::new("response1")), + ("response2".to_string(), Response::new("response2")), + ]; + + let expected = Responses { + 0: BTreeMap::from([ + ("response1".to_string(), RefOr::T(Response::new("response1"))), + ("response2".to_string(), RefOr::T(Response::new("response2"))), + ]), + }; + + let actual = Responses::from(input); + + assert_eq!(expected, actual); + } + + #[test] + fn test_responses_from_iter() { + let input = [ + ("response1".to_string(), Response::new("response1")), + ("response2".to_string(), Response::new("response2")), + ]; + + let expected = Responses { + 0: BTreeMap::from([ + ("response1".to_string(), RefOr::T(Response::new("response1"))), + ("response2".to_string(), RefOr::T(Response::new("response2"))), + ]), + }; + + let actual = Responses::from_iter(input); + + assert_eq!(expected, actual); + } + + #[test] + fn test_responses_into_iter() { + let responses = Responses::new(); + let responses = responses.response("response1", Response::new("response1")); + assert_eq!(1, responses.into_iter().collect::>().len()); + } + + #[test] + fn test_btree_map_from_responses() { + let expected = BTreeMap::from([ + ("response1".to_string(), RefOr::T(Response::new("response1"))), + ("response2".to_string(), RefOr::T(Response::new("response2"))), + ]); + + let actual = BTreeMap::from( + Responses::new() + .response("response1", Response::new("response1")) + .response("response2", Response::new("response2")), + ); + assert_eq!(expected, actual); + } }