From 29067a0331ec09997e116faa1f1a773e375284c7 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Fri, 9 Aug 2024 11:03:12 +0100 Subject: [PATCH] Add `GenTransform::as_any_mut` and add examples --- schemars/src/gen.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/schemars/src/gen.rs b/schemars/src/gen.rs index c825172..f6553eb 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/gen.rs @@ -528,7 +528,40 @@ fn json_pointer_mut<'a>( /// ``` pub trait GenTransform: Transform + DynClone + Any + Send { /// Upcasts this transform into an [`Any`], which can be used to inspect and manipulate it as its concrete type. + /// + /// # Example + /// To remove a specific transform from an instance of `SchemaSettings`: + /// ``` + /// use schemars::gen::SchemaSettings; + /// use schemars::transform::ReplaceBoolSchemas; + /// + /// let mut settings = SchemaSettings::openapi3(); + /// let original_len = settings.transforms.len(); + /// + /// settings + /// .transforms + /// .retain(|t| !t.as_any().is::()); + /// + /// assert_eq!(settings.transforms.len(), original_len - 1); + /// ``` fn as_any(&self) -> &dyn Any; + + /// Mutably upcasts this transform into an [`Any`], which can be used to inspect and manipulate it as its concrete type. + /// + /// # Example + /// To modify a specific transform in an instance of `SchemaSettings`: + /// ``` + /// use schemars::gen::SchemaSettings; + /// use schemars::transform::ReplaceBoolSchemas; + /// + /// let mut settings = SchemaSettings::openapi3(); + /// for t in &mut settings.transforms { + /// if let Some(replace_bool_schemas) = t.as_any_mut().downcast_mut::() { + /// replace_bool_schemas.skip_additional_properties = false; + /// } + /// } + /// ``` + fn as_any_mut(&mut self) -> &mut dyn Any; } dyn_clone::clone_trait_object!(GenTransform); @@ -540,6 +573,10 @@ where fn as_any(&self) -> &dyn Any { self } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } } impl Debug for Box {