Skip to content

Commit

Permalink
Add yaml string support (#84)
Browse files Browse the repository at this point in the history
* Add to_yaml
* Add some documentation for yaml feature

Co-authored-by: Katie Cleary <katie@tpfs.io>
  • Loading branch information
clearydude and Katie Cleary authored Apr 18, 2022
1 parent 23e0387 commit 96e0228
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ json = ["serde_json", "utoipa-gen/json"]
chrono_types = ["utoipa-gen/chrono_types"]
chrono_types_with_format = ["utoipa-gen/chrono_types_with_format"]
decimal = ["utoipa-gen/decimal"]
yaml = ["serde_yaml"]

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
serde_yaml = { version = "0.8", optional = true }
utoipa-gen = { version = "0.1.3", path = "./utoipa-gen" }

[dev-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ and the `ipa` is _api_ reversed. Aaand... `ipa` is also awesome type of beer :be
## Features

* **default** Default enabled features are **json**.
* **json** Enables **serde_json** what allow to use json values in OpenAPI specification values. Thus is
* **json** Enables **serde_json** what allow to use json values in OpenAPI specification values. This is
enabled by default.
* **yaml** Enables **serde_yaml** serialization of OpenApi objects.
* **actix_extras** Enhances [actix-web](https://github.com/actix/actix-web/) intgration with being able to
parse `path` and `path parameters` from actix web path attribute macros. See the
[path attribute macro](https://docs.rs/utoipa/0.1.2/utoipa/attr.path.html) for more details.
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
//!
//! * **default** Default enabled features are **json**.
//! * **json** Enables **serde_json** what allow to use json values in OpenAPI specification values.
//! Thus is enabled by default.
//! This is enabled by default.
//! * **yaml** Enables **serde_yaml** serialization of OpenApi objects.
//! * **actix_extras** Enhances [actix-web](https://github.com/actix/actix-web/) intgration with being able to
//! parse `path` and `path parameters` from actix web path attribute macros. See [`utoipa::path(...)`][path]
//! for more details.
Expand Down
16 changes: 12 additions & 4 deletions src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,27 @@ impl OpenApi {

/// Converts this [`OpenApi`] to JSON String. This method essentially calls [`serde_json::to_string`] method. [^json]
///
/// [^json]: **serde_json** feature is needed.
#[cfg(feature = "serde_json")]
/// [^json]: **json** feature is needed.
#[cfg(feature = "json")]
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}

/// Converts this [`OpenApi`] to pretty JSON String. This method essentially calls [`serde_json::to_string_pretty`] method. [^json]
///
/// [^json]: **serde_json** feature is needed.
#[cfg(feature = "serde_json")]
/// [^json]: **json** feature is needed.
#[cfg(feature = "json")]
pub fn to_pretty_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(self)
}

/// Converts this [`OpenApi`] to YAML String. This method essentially calls [`serde_yaml::to_string`] method. [^yaml]
///
/// [^yaml]: **yaml** feature is needed.
#[cfg(feature = "yaml")]
pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(self)
}
}

impl OpenApiBuilder {
Expand Down

0 comments on commit 96e0228

Please sign in to comment.