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

Add impl From for SecurityRequirement #548

Merged
merged 2 commits into from
Dec 9, 2023
Merged
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 crates/oapi-macros/src/operation/request_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl ToTokens for RequestBodyAttr<'_> {
})
.collect::<Array<TokenStream2>>();
content.extend(quote!(
.examples_from_iter(#examples)
.extend_examples(#examples)
))
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oapi-macros/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl ToTokens for ResponseTuple<'_> {
quote!((#name, #example))
})
.collect::<Array<TokenStream2>>();
content.extend(quote!( .examples_from_iter(#examples)))
content.extend(quote!( .extend_examples(#examples)))
}
quote! {
#content
Expand Down
21 changes: 4 additions & 17 deletions crates/oapi/src/openapi/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<I: IntoIterator<Item = (N, S)>, N: Into<String>, S: Into<SecurityScheme>>(
&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<I: IntoIterator<Item = (N, S)>, N: Into<String>, S: Into<SecurityScheme>>(
pub fn extend_security_schemes<I: IntoIterator<Item = (N, S)>, N: Into<String>, S: Into<SecurityScheme>>(
mut self,
schemas: I,
) -> Self {
Expand All @@ -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()
Expand All @@ -115,7 +102,7 @@ impl Components {
/// ),
/// )]);
/// ```
pub fn schemas_from_iter<I, C, S>(mut self, schemas: I) -> Self
pub fn extend_schemas<I, C, S>(mut self, schemas: I) -> Self
where
I: IntoIterator<Item = (S, C)>,
C: Into<RefOr<Schema>>,
Expand Down
2 changes: 1 addition & 1 deletion crates/oapi/src/openapi/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Content {
/// `examples` will override value in `example`.
///
/// [example]: ../example/Example.html
pub fn examples_from_iter<E: IntoIterator<Item = (N, V)>, N: Into<String>, V: Into<RefOr<Example>>>(
pub fn extend_examples<E: IntoIterator<Item = (N, V)>, N: Into<String>, V: Into<RefOr<Example>>>(
mut self,
examples: E,
) -> Self {
Expand Down
34 changes: 34 additions & 0 deletions crates/oapi/src/openapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<N: Into<String>, S: Into<SecurityScheme>>(
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<I: IntoIterator<Item = (N, S)>, N: Into<String>, S: Into<SecurityScheme>>(
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<I, T>(mut self, tags: I) -> Self
where
Expand Down
2 changes: 1 addition & 1 deletion crates/oapi/src/openapi/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions crates/oapi/src/openapi/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ pub enum SecurityScheme {
description: Option<String>,
},
}
impl From<OAuth2> for SecurityScheme {
fn from(oauth2: OAuth2) -> Self {
Self::OAuth2(oauth2)
}
}
impl From<ApiKey> for SecurityScheme {
fn from(api_key: ApiKey) -> Self {
Self::ApiKey(api_key)
}
}
impl From<OpenIdConnect> 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)]
Expand Down
Loading