diff --git a/Cargo.toml b/Cargo.toml index 67dc36d..84fb93b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,8 @@ edition = "2021" features = ["full", "aide"] [dependencies] -axum = { version = "0.7.1", default-features = false } -garde = { version = "0.16.3", optional = true } +axum = { version = "0.7.3", default-features = false } +garde = { version = "0.17.0", optional = true } validator = { version = "0.16.1", optional = true} validify = { version = "1.3.0", optional = true} @@ -44,20 +44,20 @@ version = "0.11.0" optional = true [dependencies.serde] -version = "1.0.193" +version = "1.0.195" optional = true [dependencies.aide] -version = "0.13.0" +version = "0.13.1" optional = true [dev-dependencies] -anyhow = "1.0.72" +anyhow = "1.0.75" axum = { version = "0.7.1", features = ["macros"] } tokio = { version = "1.34.0", features = ["full"] } hyper = { version = "0.14.27", features = ["full"] } -reqwest = { version = "0.11.22", features = ["json", "multipart"] } -serde = { version = "1.0.193", features = ["derive"] } +reqwest = { version = "0.11.23", features = ["json", "multipart"] } +serde = { version = "1.0.195", features = ["derive"] } validator = { version = "0.16.1", features = ["derive"] } serde_json = "1.0.108" serde_yaml = "0.9.27" diff --git a/src/garde.rs b/src/garde.rs index f16ce0c..46b9f30 100644 --- a/src/garde.rs +++ b/src/garde.rs @@ -25,7 +25,6 @@ use std::ops::{Deref, DerefMut}; /// If using arguments, you must pass the arguments to Garde extractor via state, meaning implementing `FromRef` for your validation arguments type. /// #[derive(Debug, Clone, Copy, Default)] -#[cfg_attr(feature = "aide", derive(aide::OperationIo))] pub struct Garde(pub E); impl Deref for Garde { @@ -58,6 +57,16 @@ impl Garde { } } +#[cfg(feature = "aide")] +impl aide::OperationInput for Garde +where + T: aide::OperationInput, +{ + fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) { + T::operation_input(ctx, operation); + } +} + /// `GardeRejection` is returned when the `Garde` extractor fails. /// pub type GardeRejection = ValidationRejection; diff --git a/src/validator.rs b/src/validator.rs index dca0a24..fb48965 100644 --- a/src/validator.rs +++ b/src/validator.rs @@ -28,7 +28,6 @@ use validator::{Validate, ValidateArgs, ValidationErrors}; /// For examples with custom extractors, check out the `tests/custom.rs` file. /// #[derive(Debug, Clone, Copy, Default)] -#[cfg_attr(feature = "aide", derive(aide::OperationIo))] pub struct Valid(pub E); impl Deref for Valid { @@ -45,7 +44,7 @@ impl DerefMut for Valid { } } -impl Display for ValidEx { +impl Display for Valid { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } @@ -58,6 +57,16 @@ impl Valid { } } +#[cfg(feature = "aide")] +impl aide::OperationInput for Valid +where + T: aide::OperationInput, +{ + fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) { + T::operation_input(ctx, operation); + } +} + /// # `ValidEx` data extractor /// /// `ValidEx` can be incorporated with extractors from various modules, similar to `Valid`. @@ -72,7 +81,6 @@ impl Valid { /// Although current module documentation predominantly showcases `Valid` examples, the usage of `ValidEx` is analogous. /// #[derive(Debug, Clone, Copy, Default)] -#[cfg_attr(feature = "aide", derive(aide::OperationIo))] pub struct ValidEx(pub E, pub A); impl Deref for ValidEx { @@ -89,7 +97,7 @@ impl DerefMut for ValidEx { } } -impl Display for Valid { +impl Display for ValidEx { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } @@ -117,6 +125,16 @@ impl ValidEx { } } +#[cfg(feature = "aide")] +impl aide::OperationInput for ValidEx +where + T: aide::OperationInput, +{ + fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) { + T::operation_input(ctx, operation); + } +} + /// `Arguments` provides the validation arguments for the data type `T`. /// /// This trait has an associated type `T` which represents the data type to diff --git a/src/validify.rs b/src/validify.rs index 43232e3..6ca5f5e 100644 --- a/src/validify.rs +++ b/src/validify.rs @@ -24,7 +24,6 @@ use validify::{Modify, Validate, ValidationErrors, ValidifyPayload}; /// It only does validation, usage is similar to `Valid`. /// #[derive(Debug, Clone, Copy, Default)] -#[cfg_attr(feature = "aide", derive(aide::OperationIo))] pub struct Validated(pub E); impl Deref for Validated { @@ -57,6 +56,16 @@ impl Validated { } } +#[cfg(feature = "aide")] +impl aide::OperationInput for Validated +where + T: aide::OperationInput, +{ + fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) { + T::operation_input(ctx, operation); + } +} + /// # `Modified` data extractor / response /// /// ## Extractor @@ -74,7 +83,6 @@ impl Validated { /// /// This allows applying modifications during response conversion by leveraging validify. #[derive(Debug, Clone, Copy, Default)] -#[cfg_attr(feature = "aide", derive(aide::OperationIo))] pub struct Modified(pub E); impl Deref for Modified { @@ -114,6 +122,38 @@ impl IntoResponse for Modified { } } +#[cfg(feature = "aide")] +impl aide::OperationInput for Modified +where + T: aide::OperationInput, +{ + fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) { + T::operation_input(ctx, operation); + } +} + +#[cfg(feature = "aide")] +impl aide::OperationOutput for Modified +where + T: aide::OperationOutput, +{ + type Inner = T::Inner; + + fn operation_response( + ctx: &mut aide::gen::GenContext, + operation: &mut aide::openapi::Operation, + ) -> Option { + T::operation_response(ctx, operation) + } + + fn inferred_responses( + ctx: &mut aide::gen::GenContext, + operation: &mut aide::openapi::Operation, + ) -> Vec<(Option, aide::openapi::Response)> { + T::inferred_responses(ctx, operation) + } +} + /// # `Validified` data extractor /// /// `Validified` provides construction, modification and validation abilities based on `validify`. @@ -123,7 +163,6 @@ impl IntoResponse for Modified { /// And can treat missing fields as validation errors. /// #[derive(Debug, Clone, Copy, Default)] -#[cfg_attr(feature = "aide", derive(aide::OperationIo))] pub struct Validified(pub E); impl Deref for Validified { @@ -156,6 +195,16 @@ impl Validified { } } +#[cfg(feature = "aide")] +impl aide::OperationInput for Validified +where + T: aide::OperationInput, +{ + fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) { + T::operation_input(ctx, operation); + } +} + /// # `ValidifiedByRef` data extractor /// /// `ValidifiedByRef` is similar to `Validified`, but operates via reference. @@ -163,7 +212,6 @@ impl Validified { /// Suitable for inner extractors not based on `serde`. /// #[derive(Debug, Clone, Copy, Default)] -#[cfg_attr(feature = "aide", derive(aide::OperationIo))] pub struct ValidifiedByRef(pub E); impl Deref for ValidifiedByRef { @@ -196,6 +244,16 @@ impl ValidifiedByRef { } } +#[cfg(feature = "aide")] +impl aide::OperationInput for ValidifiedByRef +where + T: aide::OperationInput, +{ + fn operation_input(ctx: &mut aide::gen::GenContext, operation: &mut aide::openapi::Operation) { + T::operation_input(ctx, operation); + } +} + /// `ValidifyRejection` is returned when the `Validated` / `Modified` / `Validified` / `ValidifiedByRef` extractor fails. /// pub type ValidifyRejection = ValidationRejection;