diff --git a/crates/oapi-macros/src/operation/request_body.rs b/crates/oapi-macros/src/operation/request_body.rs index 284f6add1..43d7a770a 100644 --- a/crates/oapi-macros/src/operation/request_body.rs +++ b/crates/oapi-macros/src/operation/request_body.rs @@ -161,7 +161,7 @@ impl ToTokens for RequestBodyAttr<'_> { }) .collect::>(); content.extend(quote!( - .examples_from_iter(#examples) + .extend_examples(#examples) )) } diff --git a/crates/oapi-macros/src/response/mod.rs b/crates/oapi-macros/src/response/mod.rs index 6a5fd36de..04ca7353e 100644 --- a/crates/oapi-macros/src/response/mod.rs +++ b/crates/oapi-macros/src/response/mod.rs @@ -291,7 +291,7 @@ impl ToTokens for ResponseTuple<'_> { quote!((#name, #example)) }) .collect::>(); - content.extend(quote!( .examples_from_iter(#examples))) + content.extend(quote!( .extend_examples(#examples))) } quote! { #content diff --git a/crates/oapi/src/openapi/components.rs b/crates/oapi/src/openapi/components.rs index 4dd24b3f1..d70d83305 100644 --- a/crates/oapi/src/openapi/components.rs +++ b/crates/oapi/src/openapi/components.rs @@ -45,6 +45,7 @@ impl Components { pub fn new() -> Self { Default::default() } + /// Add [`SecurityScheme`] to [`Components`] and returns `Self`. /// /// Accepts two arguments where first is the name of the [`SecurityScheme`]. This is later when @@ -67,21 +68,7 @@ impl Components { /// referenced by [`SecurityRequirement`][requirement]s. Second parameter is the [`SecurityScheme`]. /// /// [requirement]: ../security/struct.SecurityRequirement.html - pub fn add_security_schemes_from_iter, N: Into, S: Into>( - &mut self, - schemas: I, - ) { - self.security_schemes - .extend(schemas.into_iter().map(|(name, item)| (name.into(), item.into()))); - } - - /// Add iterator of [`SecurityScheme`]s to [`Components`]. - /// - /// Accepts two arguments where first is the name of the [`SecurityScheme`]. This is later when - /// referenced by [`SecurityRequirement`][requirement]s. Second parameter is the [`SecurityScheme`]. - /// - /// [requirement]: ../security/struct.SecurityRequirement.html - pub fn security_schemes_from_iter, N: Into, S: Into>( + pub fn extend_security_schemes, N: Into, S: Into>( mut self, schemas: I, ) -> Self { @@ -103,7 +90,7 @@ impl Components { /// # Examples /// ``` /// # use salvo_oapi::{Components, Object, SchemaType, Schema}; - /// Components::new().schemas_from_iter([( + /// Components::new().extend_schemas([( /// "Pet", /// Schema::from( /// Object::new() @@ -115,7 +102,7 @@ impl Components { /// ), /// )]); /// ``` - pub fn schemas_from_iter(mut self, schemas: I) -> Self + pub fn extend_schemas(mut self, schemas: I) -> Self where I: IntoIterator, C: Into>, diff --git a/crates/oapi/src/openapi/content.rs b/crates/oapi/src/openapi/content.rs index a9af53b8e..f15eaf9cb 100644 --- a/crates/oapi/src/openapi/content.rs +++ b/crates/oapi/src/openapi/content.rs @@ -66,7 +66,7 @@ impl Content { /// `examples` will override value in `example`. /// /// [example]: ../example/Example.html - pub fn examples_from_iter, N: Into, V: Into>>( + pub fn extend_examples, N: Into, V: Into>>( mut self, examples: E, ) -> Self { diff --git a/crates/oapi/src/openapi/mod.rs b/crates/oapi/src/openapi/mod.rs index dc2231560..520d17596 100644 --- a/crates/oapi/src/openapi/mod.rs +++ b/crates/oapi/src/openapi/mod.rs @@ -229,6 +229,40 @@ impl OpenApi { self } + /// Add [`SecurityScheme`] to [`Components`] and returns `Self`. + /// + /// Accepts two arguments where first is the name of the [`SecurityScheme`]. This is later when + /// referenced by [`SecurityRequirement`][requirement]s. Second parameter is the [`SecurityScheme`]. + /// + /// [requirement]: ../security/struct.SecurityRequirement.html + pub fn add_security_scheme, S: Into>( + mut self, + name: N, + security_scheme: S, + ) -> Self { + self.components + .security_schemes + .insert(name.into(), security_scheme.into()); + + self + } + + /// Add iterator of [`SecurityScheme`]s to [`Components`]. + /// + /// Accepts two arguments where first is the name of the [`SecurityScheme`]. This is later when + /// referenced by [`SecurityRequirement`][requirement]s. Second parameter is the [`SecurityScheme`]. + /// + /// [requirement]: ../security/struct.SecurityRequirement.html + pub fn extend_security_schemes, N: Into, S: Into>( + mut self, + schemas: I, + ) -> Self { + self.components + .security_schemes + .extend(schemas.into_iter().map(|(name, item)| (name.into(), item.into()))); + self + } + /// Add iterator of [`Tag`]s to add additional documentation for **operations** tags. pub fn tags(mut self, tags: I) -> Self where diff --git a/crates/oapi/src/openapi/schema/mod.rs b/crates/oapi/src/openapi/schema/mod.rs index cd9039ee4..c5fb9ade7 100644 --- a/crates/oapi/src/openapi/schema/mod.rs +++ b/crates/oapi/src/openapi/schema/mod.rs @@ -541,7 +541,7 @@ mod tests { #[test] fn reserialize_deserialized_schema_components() { let components = Components::new() - .schemas_from_iter(vec![( + .extend_schemas(vec![( "Comp", Schema::from( Object::new() diff --git a/crates/oapi/src/openapi/security.rs b/crates/oapi/src/openapi/security.rs index 420aa8461..205111964 100644 --- a/crates/oapi/src/openapi/security.rs +++ b/crates/oapi/src/openapi/security.rs @@ -115,6 +115,21 @@ pub enum SecurityScheme { description: Option, }, } +impl From for SecurityScheme { + fn from(oauth2: OAuth2) -> Self { + Self::OAuth2(oauth2) + } +} +impl From for SecurityScheme { + fn from(api_key: ApiKey) -> Self { + Self::ApiKey(api_key) + } +} +impl From for SecurityScheme { + fn from(open_id_connect: OpenIdConnect) -> Self { + Self::OpenIdConnect(open_id_connect) + } +} /// Api key authentication [`SecurityScheme`]. #[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]