diff --git a/Cargo.toml b/Cargo.toml index 379a9f6..21ce74f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ http = "1" indoc = "2" log = "0.4" maplit = "1" -oas3 = "0.14" +oas3 = { version = "0.14", default-features = false } once_cell = "1" pretty_assertions = "1" pretty_env_logger = "0.5" diff --git a/crates/oas3/Cargo.toml b/crates/oas3/Cargo.toml index 8e926fd..ffe9ec2 100644 --- a/crates/oas3/Cargo.toml +++ b/crates/oas3/Cargo.toml @@ -11,8 +11,9 @@ edition = { workspace = true } rust-version = { workspace = true } [features] -default = ["validation"] +default = ["validation", "yaml_spec"] validation = [] +yaml_spec = ["dep:serde_yaml"] [dependencies] derive_more = { workspace = true, features = ["display", "error", "from"] } @@ -23,7 +24,7 @@ regex = { workspace = true } semver = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } -serde_yaml = { workspace = true } +serde_yaml = { workspace = true, optional = true } url = { workspace = true, features = ["serde"] } [dev-dependencies] @@ -31,5 +32,10 @@ eyre = { workspace = true } indoc = { workspace = true } pretty_assertions = { workspace = true } +[[example]] +name = "printers" +path = "examples/printer.rs" +required-features = ["yaml_spec"] + [lints] workspace = true diff --git a/crates/oas3/src/error.rs b/crates/oas3/src/error.rs index 7fcf9af..5b6f3c8 100644 --- a/crates/oas3/src/error.rs +++ b/crates/oas3/src/error.rs @@ -15,6 +15,7 @@ pub enum Error { /// YAML error. #[display("YAML error")] + #[cfg(feature = "yaml_spec")] Yaml(serde_yaml::Error), /// JSON error. diff --git a/crates/oas3/src/lib.rs b/crates/oas3/src/lib.rs index ce0c025..22c57bd 100644 --- a/crates/oas3/src/lib.rs +++ b/crates/oas3/src/lib.rs @@ -32,6 +32,8 @@ pub use self::{error::Error, spec::Spec}; pub type OpenApiV3Spec = spec::Spec; /// Try deserializing an OpenAPI spec (YAML or JSON) from a file, giving the path. +/// +/// If the `yaml` feature flag is disabled only `JSON` specs are supported pub fn from_path

(path: P) -> Result where P: AsRef, @@ -40,19 +42,38 @@ where } /// Try deserializing an OpenAPI spec (YAML or JSON) from a [`Read`] type. +/// +/// If the `yaml` feature flag is disabled only `JSON` specs are supported pub fn from_reader(read: R) -> Result where R: Read, { - Ok(serde_yaml::from_reader::(read)?) + #[cfg(feature = "yaml_spec")] + { + Ok(serde_yaml::from_reader::(read)?) + } + #[cfg(not(feature = "yaml_spec"))] + { + Ok(serde_json::from_reader::(read)?) + } } /// Try deserializing an OpenAPI spec (YAML or JSON) from string. +/// +/// If the `yaml` feature flag is disabled only `JSON` specs are supported pub fn from_str(val: impl AsRef) -> Result { - Ok(serde_yaml::from_str::(val.as_ref())?) + #[cfg(feature = "yaml_spec")] + { + Ok(serde_yaml::from_str::(val.as_ref())?) + } + #[cfg(not(feature = "yaml_spec"))] + { + Ok(serde_json::from_str::(val.as_ref())?) + } } /// Try serializing to a YAML string. +#[cfg(feature = "yaml_spec")] pub fn to_yaml(spec: &OpenApiV3Spec) -> Result { Ok(serde_yaml::to_string(spec)?) } @@ -62,7 +83,7 @@ pub fn to_json(spec: &OpenApiV3Spec) -> Result { Ok(serde_json::to_string_pretty(spec)?) } -#[cfg(test)] +#[cfg(all(test, feature = "yaml_spec"))] mod tests { use std::{ fs::{self, read_to_string, File}, diff --git a/crates/oas3/src/spec/discriminator.rs b/crates/oas3/src/spec/discriminator.rs index edc0374..83f971a 100644 --- a/crates/oas3/src/spec/discriminator.rs +++ b/crates/oas3/src/spec/discriminator.rs @@ -24,7 +24,7 @@ pub struct Discriminator { pub mapping: Option>, } -#[cfg(test)] +#[cfg(all(test, feature = "yaml_spec"))] mod tests { use super::*; diff --git a/crates/oas3/src/spec/media_type_examples.rs b/crates/oas3/src/spec/media_type_examples.rs index 8691e60..f675136 100644 --- a/crates/oas3/src/spec/media_type_examples.rs +++ b/crates/oas3/src/spec/media_type_examples.rs @@ -83,6 +83,7 @@ impl MediaTypeExamples { #[cfg(test)] mod tests { + #[cfg(feature = "yaml_spec")] use serde_json::json; use super::*; @@ -98,6 +99,7 @@ mod tests { } #[test] + #[cfg(feature = "yaml_spec")] fn deserialize() { assert_eq!( serde_yaml::from_str::(indoc::indoc! {" @@ -119,6 +121,7 @@ mod tests { } #[test] + #[cfg(feature = "yaml_spec")] fn serialize() { let mt_examples = MediaTypeExamples::default(); assert_eq!( @@ -140,6 +143,7 @@ mod tests { } #[test] + #[cfg(feature = "yaml_spec")] fn single_example() { let spec = serde_yaml::from_str::(indoc::indoc! {" openapi: 3.1.0 @@ -167,6 +171,7 @@ paths: {} } #[test] + #[cfg(feature = "yaml_spec")] fn resolve_references() { let spec = serde_yaml::from_str::(indoc::indoc! {" openapi: 3.1.0 diff --git a/crates/oas3/src/spec/mod.rs b/crates/oas3/src/spec/mod.rs index 73651f4..721e31b 100644 --- a/crates/oas3/src/spec/mod.rs +++ b/crates/oas3/src/spec/mod.rs @@ -228,7 +228,7 @@ impl Spec { } } -#[cfg(test)] +#[cfg(all(test, feature = "yaml_spec"))] mod tests { use pretty_assertions::assert_eq; diff --git a/crates/oas3/src/spec/parameter.rs b/crates/oas3/src/spec/parameter.rs index a16e08f..a04761d 100644 --- a/crates/oas3/src/spec/parameter.rs +++ b/crates/oas3/src/spec/parameter.rs @@ -223,7 +223,7 @@ impl FromRef for Parameter { } } -#[cfg(test)] +#[cfg(all(test, feature = "yaml_spec"))] mod tests { use indoc::indoc; diff --git a/crates/oas3/src/spec/schema.rs b/crates/oas3/src/spec/schema.rs index 951a897..4c66385 100644 --- a/crates/oas3/src/spec/schema.rs +++ b/crates/oas3/src/spec/schema.rs @@ -593,7 +593,7 @@ where T::deserialize(de).map(Some) } -#[cfg(test)] +#[cfg(all(test, feature = "yaml_spec"))] mod tests { use super::*; diff --git a/crates/oas3/src/spec/spec_extensions.rs b/crates/oas3/src/spec/spec_extensions.rs index 9e105c6..a05e006 100644 --- a/crates/oas3/src/spec/spec_extensions.rs +++ b/crates/oas3/src/spec/spec_extensions.rs @@ -5,6 +5,11 @@ use std::{ use serde::{de, Deserializer, Serializer}; +#[cfg(feature = "yaml_spec")] +type KeyType = serde_yaml::Value; +#[cfg(not(feature = "yaml_spec"))] +type KeyType = serde_json::Value; + /// Deserializes fields of a map beginning with `x-`. pub(crate) fn deserialize<'de, D>( deserializer: D, @@ -25,7 +30,7 @@ where where M: de::MapAccess<'de>, { - let mut map = HashMap::::new(); + let mut map = HashMap::::new(); while let Some((key, value)) = access.next_entry()? { map.insert(key, value); diff --git a/crates/roast/Cargo.toml b/crates/roast/Cargo.toml index 2681cc8..520fbbc 100644 --- a/crates/roast/Cargo.toml +++ b/crates/roast/Cargo.toml @@ -17,7 +17,7 @@ derive_more = { workspace = true, features = ["display", "error", "from"] } futures-util = { workspace = true } http = { workspace = true } log = { workspace = true } -oas3 = { workspace = true } +oas3 = { workspace = true, default-features = false } once_cell = { workspace = true } prettytable-rs = { workspace = true } reqwest = { workspace = true, features = ["json"] }