Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make serde_yaml dependency optional #168

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 8 additions & 2 deletions crates/oas3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -23,13 +24,18 @@ 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]
eyre = { workspace = true }
indoc = { workspace = true }
pretty_assertions = { workspace = true }

[[example]]
name = "printers"
path = "examples/printer.rs"
required-features = ["yaml_spec"]

[lints]
workspace = true
1 change: 1 addition & 0 deletions crates/oas3/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum Error {

/// YAML error.
#[display("YAML error")]
#[cfg(feature = "yaml_spec")]
Yaml(serde_yaml::Error),
Comment on lines 17 to 19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feature guarding enum variants is a no-go for me, i'll get this solved another way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainer PR edits are not enabled, see #169 for suggested change

Copy link
Member

@robjtede robjtede Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still open to feedback on the approach shown there. It's unfortunately to lose the I/O methods but I can't think of a good solution immediately, but fns can be added if we come up with something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking over, that's really appreciated. I do not have any better idea regarding to the error type than what's already implemented.


/// JSON error.
Expand Down
27 changes: 24 additions & 3 deletions crates/oas3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P>(path: P) -> Result<OpenApiV3Spec, Error>
where
P: AsRef<Path>,
Expand All @@ -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<R>(read: R) -> Result<OpenApiV3Spec, Error>
where
R: Read,
{
Ok(serde_yaml::from_reader::<R, OpenApiV3Spec>(read)?)
#[cfg(feature = "yaml_spec")]
{
Ok(serde_yaml::from_reader::<R, OpenApiV3Spec>(read)?)
}
#[cfg(not(feature = "yaml_spec"))]
{
Ok(serde_json::from_reader::<R, OpenApiV3Spec>(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<str>) -> Result<OpenApiV3Spec, Error> {
Ok(serde_yaml::from_str::<OpenApiV3Spec>(val.as_ref())?)
#[cfg(feature = "yaml_spec")]
{
Ok(serde_yaml::from_str::<OpenApiV3Spec>(val.as_ref())?)
}
#[cfg(not(feature = "yaml_spec"))]
{
Ok(serde_json::from_str::<OpenApiV3Spec>(val.as_ref())?)
}
}

/// Try serializing to a YAML string.
#[cfg(feature = "yaml_spec")]
pub fn to_yaml(spec: &OpenApiV3Spec) -> Result<String, Error> {
Ok(serde_yaml::to_string(spec)?)
}
Expand All @@ -62,7 +83,7 @@ pub fn to_json(spec: &OpenApiV3Spec) -> Result<String, Error> {
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},
Expand Down
2 changes: 1 addition & 1 deletion crates/oas3/src/spec/discriminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Discriminator {
pub mapping: Option<BTreeMap<String, String>>,
}

#[cfg(test)]
#[cfg(all(test, feature = "yaml_spec"))]
mod tests {
use super::*;

Expand Down
5 changes: 5 additions & 0 deletions crates/oas3/src/spec/media_type_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl MediaTypeExamples {

#[cfg(test)]
mod tests {
#[cfg(feature = "yaml_spec")]
use serde_json::json;

use super::*;
Expand All @@ -98,6 +99,7 @@ mod tests {
}

#[test]
#[cfg(feature = "yaml_spec")]
fn deserialize() {
assert_eq!(
serde_yaml::from_str::<MediaTypeExamples>(indoc::indoc! {"
Expand All @@ -119,6 +121,7 @@ mod tests {
}

#[test]
#[cfg(feature = "yaml_spec")]
fn serialize() {
let mt_examples = MediaTypeExamples::default();
assert_eq!(
Expand All @@ -140,6 +143,7 @@ mod tests {
}

#[test]
#[cfg(feature = "yaml_spec")]
fn single_example() {
let spec = serde_yaml::from_str::<Spec>(indoc::indoc! {"
openapi: 3.1.0
Expand Down Expand Up @@ -167,6 +171,7 @@ paths: {}
}

#[test]
#[cfg(feature = "yaml_spec")]
fn resolve_references() {
let spec = serde_yaml::from_str::<Spec>(indoc::indoc! {"
openapi: 3.1.0
Expand Down
2 changes: 1 addition & 1 deletion crates/oas3/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl Spec {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "yaml_spec"))]
mod tests {
use pretty_assertions::assert_eq;

Expand Down
2 changes: 1 addition & 1 deletion crates/oas3/src/spec/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl FromRef for Parameter {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "yaml_spec"))]
mod tests {
use indoc::indoc;

Expand Down
2 changes: 1 addition & 1 deletion crates/oas3/src/spec/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ where
T::deserialize(de).map(Some)
}

#[cfg(test)]
#[cfg(all(test, feature = "yaml_spec"))]
mod tests {
use super::*;

Expand Down
7 changes: 6 additions & 1 deletion crates/oas3/src/spec/spec_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,7 +30,7 @@ where
where
M: de::MapAccess<'de>,
{
let mut map = HashMap::<serde_yaml::Value, serde_json::Value>::new();
let mut map = HashMap::<KeyType, serde_json::Value>::new();

while let Some((key, value)) = access.next_entry()? {
map.insert(key, value);
Expand Down
2 changes: 1 addition & 1 deletion crates/roast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down