Skip to content

Commit 56f62e2

Browse files
committed
Make serde_yaml dependency optional
Fixes #152
1 parent e758e2f commit 56f62e2

11 files changed

+50
-12
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ http = "1"
2727
indoc = "2"
2828
log = "0.4"
2929
maplit = "1"
30-
oas3 = "0.14"
30+
oas3 = { version = "0.14", default-features = false }
3131
once_cell = "1"
3232
pretty_assertions = "1"
3333
pretty_env_logger = "0.5"

crates/oas3/Cargo.toml

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ edition = { workspace = true }
1111
rust-version = { workspace = true }
1212

1313
[features]
14-
default = ["validation"]
14+
default = ["validation", "yaml_spec"]
1515
validation = []
16+
yaml_spec = ["dep:serde_yaml"]
1617

1718
[dependencies]
1819
derive_more = { workspace = true, features = ["display", "error", "from"] }
@@ -23,13 +24,18 @@ regex = { workspace = true }
2324
semver = { workspace = true }
2425
serde = { workspace = true, features = ["derive"] }
2526
serde_json = { workspace = true }
26-
serde_yaml = { workspace = true }
27+
serde_yaml = { workspace = true, optional = true }
2728
url = { workspace = true, features = ["serde"] }
2829

2930
[dev-dependencies]
3031
eyre = { workspace = true }
3132
indoc = { workspace = true }
3233
pretty_assertions = { workspace = true }
3334

35+
[[example]]
36+
name = "printers"
37+
path = "examples/printer.rs"
38+
required-features = ["yaml_spec"]
39+
3440
[lints]
3541
workspace = true

crates/oas3/src/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub enum Error {
1515

1616
/// YAML error.
1717
#[display("YAML error")]
18+
#[cfg(feature = "yaml_spec")]
1819
Yaml(serde_yaml::Error),
1920

2021
/// JSON error.

crates/oas3/src/lib.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub use self::{error::Error, spec::Spec};
3232
pub type OpenApiV3Spec = spec::Spec;
3333

3434
/// Try deserializing an OpenAPI spec (YAML or JSON) from a file, giving the path.
35+
///
36+
/// If the `yaml` feature flag is disabled only `JSON` specs are supported
3537
pub fn from_path<P>(path: P) -> Result<OpenApiV3Spec, Error>
3638
where
3739
P: AsRef<Path>,
@@ -40,19 +42,38 @@ where
4042
}
4143

4244
/// Try deserializing an OpenAPI spec (YAML or JSON) from a [`Read`] type.
45+
///
46+
/// If the `yaml` feature flag is disabled only `JSON` specs are supported
4347
pub fn from_reader<R>(read: R) -> Result<OpenApiV3Spec, Error>
4448
where
4549
R: Read,
4650
{
47-
Ok(serde_yaml::from_reader::<R, OpenApiV3Spec>(read)?)
51+
#[cfg(feature = "yaml_spec")]
52+
{
53+
Ok(serde_yaml::from_reader::<R, OpenApiV3Spec>(read)?)
54+
}
55+
#[cfg(not(feature = "yaml_spec"))]
56+
{
57+
Ok(serde_json::from_reader::<R, OpenApiV3Spec>(read)?)
58+
}
4859
}
4960

5061
/// Try deserializing an OpenAPI spec (YAML or JSON) from string.
62+
///
63+
/// If the `yaml` feature flag is disabled only `JSON` specs are supported
5164
pub fn from_str(val: impl AsRef<str>) -> Result<OpenApiV3Spec, Error> {
52-
Ok(serde_yaml::from_str::<OpenApiV3Spec>(val.as_ref())?)
65+
#[cfg(feature = "yaml_spec")]
66+
{
67+
Ok(serde_yaml::from_str::<OpenApiV3Spec>(val.as_ref())?)
68+
}
69+
#[cfg(not(feature = "yaml_spec"))]
70+
{
71+
Ok(serde_json::from_str::<OpenApiV3Spec>(val.as_ref())?)
72+
}
5373
}
5474

5575
/// Try serializing to a YAML string.
76+
#[cfg(feature = "yaml_spec")]
5677
pub fn to_yaml(spec: &OpenApiV3Spec) -> Result<String, Error> {
5778
Ok(serde_yaml::to_string(spec)?)
5879
}
@@ -62,7 +83,7 @@ pub fn to_json(spec: &OpenApiV3Spec) -> Result<String, Error> {
6283
Ok(serde_json::to_string_pretty(spec)?)
6384
}
6485

65-
#[cfg(test)]
86+
#[cfg(all(test, feature = "yaml_spec"))]
6687
mod tests {
6788
use std::{
6889
fs::{self, read_to_string, File},

crates/oas3/src/spec/discriminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct Discriminator {
2424
pub mapping: Option<BTreeMap<String, String>>,
2525
}
2626

27-
#[cfg(test)]
27+
#[cfg(all(test, feature = "yaml_spec"))]
2828
mod tests {
2929
use super::*;
3030

crates/oas3/src/spec/media_type_examples.rs

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl MediaTypeExamples {
8383

8484
#[cfg(test)]
8585
mod tests {
86+
#[cfg(feature = "yaml_spec")]
8687
use serde_json::json;
8788

8889
use super::*;
@@ -98,6 +99,7 @@ mod tests {
9899
}
99100

100101
#[test]
102+
#[cfg(feature = "yaml_spec")]
101103
fn deserialize() {
102104
assert_eq!(
103105
serde_yaml::from_str::<MediaTypeExamples>(indoc::indoc! {"
@@ -119,6 +121,7 @@ mod tests {
119121
}
120122

121123
#[test]
124+
#[cfg(feature = "yaml_spec")]
122125
fn serialize() {
123126
let mt_examples = MediaTypeExamples::default();
124127
assert_eq!(
@@ -140,6 +143,7 @@ mod tests {
140143
}
141144

142145
#[test]
146+
#[cfg(feature = "yaml_spec")]
143147
fn single_example() {
144148
let spec = serde_yaml::from_str::<Spec>(indoc::indoc! {"
145149
openapi: 3.1.0
@@ -167,6 +171,7 @@ paths: {}
167171
}
168172

169173
#[test]
174+
#[cfg(feature = "yaml_spec")]
170175
fn resolve_references() {
171176
let spec = serde_yaml::from_str::<Spec>(indoc::indoc! {"
172177
openapi: 3.1.0

crates/oas3/src/spec/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl Spec {
228228
}
229229
}
230230

231-
#[cfg(test)]
231+
#[cfg(all(test, feature = "yaml_spec"))]
232232
mod tests {
233233
use pretty_assertions::assert_eq;
234234

crates/oas3/src/spec/parameter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl FromRef for Parameter {
223223
}
224224
}
225225

226-
#[cfg(test)]
226+
#[cfg(all(test, feature = "yaml_spec"))]
227227
mod tests {
228228
use indoc::indoc;
229229

crates/oas3/src/spec/schema.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ where
593593
T::deserialize(de).map(Some)
594594
}
595595

596-
#[cfg(test)]
596+
#[cfg(all(test, feature = "yaml_spec"))]
597597
mod tests {
598598
use super::*;
599599

crates/oas3/src/spec/spec_extensions.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ use std::{
55

66
use serde::{de, Deserializer, Serializer};
77

8+
#[cfg(feature = "yaml_spec")]
9+
type KeyType = serde_yaml::Value;
10+
#[cfg(not(feature = "yaml_spec"))]
11+
type KeyType = serde_json::Value;
12+
813
/// Deserializes fields of a map beginning with `x-`.
914
pub(crate) fn deserialize<'de, D>(
1015
deserializer: D,
@@ -25,7 +30,7 @@ where
2530
where
2631
M: de::MapAccess<'de>,
2732
{
28-
let mut map = HashMap::<serde_yaml::Value, serde_json::Value>::new();
33+
let mut map = HashMap::<KeyType, serde_json::Value>::new();
2934

3035
while let Some((key, value)) = access.next_entry()? {
3136
map.insert(key, value);

crates/roast/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ derive_more = { workspace = true, features = ["display", "error", "from"] }
1717
futures-util = { workspace = true }
1818
http = { workspace = true }
1919
log = { workspace = true }
20-
oas3 = { workspace = true }
20+
oas3 = { workspace = true, default-features = false }
2121
once_cell = { workspace = true }
2222
prettytable-rs = { workspace = true }
2323
reqwest = { workspace = true, features = ["json"] }

0 commit comments

Comments
 (0)