From c97f0e631fdf5743617ecbdd99a51460ffde49c4 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 14 Oct 2024 16:40:44 +0100 Subject: [PATCH] test: improve coverage of MediaTypeExamples --- crates/oas3/src/spec/media_type_examples.rs | 132 +++++++++++++++++++- crates/oas3/src/spec/tag.rs | 2 + 2 files changed, 133 insertions(+), 1 deletion(-) diff --git a/crates/oas3/src/spec/media_type_examples.rs b/crates/oas3/src/spec/media_type_examples.rs index a44e34c..4657618 100644 --- a/crates/oas3/src/spec/media_type_examples.rs +++ b/crates/oas3/src/spec/media_type_examples.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use super::{Example, ObjectOrReference, Spec}; /// Examples for a media type. -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[serde(untagged)] pub enum MediaTypeExamples { /// Example of the media type. @@ -80,3 +80,133 @@ impl MediaTypeExamples { } } } + +#[cfg(test)] +mod tests { + use serde_json::json; + + use super::*; + + #[test] + fn is_empty() { + assert!(MediaTypeExamples::default().is_empty()); + + assert!(!MediaTypeExamples::Example { + example: serde_json::Value::Null + } + .is_empty()); + } + + #[test] + fn deserialize() { + assert_eq!( + serde_yml::from_str::(indoc::indoc! {" + examples: {} + "}) + .unwrap(), + MediaTypeExamples::default(), + ); + + assert_eq!( + serde_yml::from_str::(indoc::indoc! {" + example: null + "}) + .unwrap(), + MediaTypeExamples::Example { + example: json!(null) + }, + ); + } + + #[test] + fn serialize() { + let mt_examples = MediaTypeExamples::default(); + assert_eq!( + serde_yml::to_string(&mt_examples).unwrap(), + indoc::indoc! {" + examples: {} + "}, + ); + + let mt_examples = MediaTypeExamples::Example { + example: json!(null), + }; + assert_eq!( + serde_yml::to_string(&mt_examples).unwrap(), + indoc::indoc! {" + example: null + "}, + ); + } + + #[test] + fn single_example() { + let spec = serde_yml::from_str::(indoc::indoc! {" +openapi: 3.1.0 +info: + title: Mascots + version: 0.0.0 +paths: {} + "}) + .unwrap(); + + let mt_examples = MediaTypeExamples::Example { + example: json!("ferris"), + }; + + let examples = mt_examples.resolve_all(&spec); + + // no example with this name defined + assert_eq!(examples.get("Go"), None); + + // single examples are put in "default" key of returned map + assert_eq!( + examples.get("default").unwrap().value.as_ref().unwrap(), + &json!("ferris"), + ); + } + + #[test] + fn resolve_references() { + let spec = serde_yml::from_str::(indoc::indoc! {" +openapi: 3.1.0 +info: + title: Mascots + version: 0.0.0 +paths: {} +components: + examples: + RustMascot: + value: ferris + "}) + .unwrap(); + + let mt_examples = MediaTypeExamples::Examples { + examples: BTreeMap::from([ + ( + "Rust".to_owned(), + ObjectOrReference::Ref { + ref_path: "#/components/examples/RustMascot".to_owned(), + }, + ), + ( + "Go".to_owned(), + ObjectOrReference::Ref { + ref_path: "#/components/examples/GoMascot".to_owned(), + }, + ), + ]), + }; + + let examples = mt_examples.resolve_all(&spec); + + // reference points to non-existent component + assert_eq!(examples.get("Go"), None); + + // reference points to valid example component + assert_eq!( + examples.get("Rust").unwrap().value.as_ref().unwrap(), + &json!("ferris"), + ); + } +} diff --git a/crates/oas3/src/spec/tag.rs b/crates/oas3/src/spec/tag.rs index 9ae548f..f260d50 100644 --- a/crates/oas3/src/spec/tag.rs +++ b/crates/oas3/src/spec/tag.rs @@ -20,6 +20,8 @@ pub struct Tag { /// [CommonMark syntax](http://spec.commonmark.org/) MAY be used for rich text representation. #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, + + // // /// Additional external documentation for this tag. // #[serde(default)] // #[serde(skip_serializing_if = "Vec::is_empty")]