Skip to content

Commit

Permalink
Feature security shema (#33)
Browse files Browse the repository at this point in the history
* Add security schema OpenAPI types
* Add security schema documentation
* Add Unit tests
  • Loading branch information
juhaku authored Feb 27, 2022
1 parent cbd0063 commit 8a9306d
Show file tree
Hide file tree
Showing 4 changed files with 889 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/openapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use self::{
Array, Component, ComponentFormat, ComponentType, Object, OneOf, Property, Ref, Schema,
ToArray,
},
security::Security,
security::SecurityRequirement,
server::Server,
tag::Tag,
};
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct OpenApi {
pub components: Option<Schema>,

#[serde(skip_serializing_if = "Option::is_none")]
pub security: Option<Vec<Security>>,
pub security: Option<Vec<SecurityRequirement>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<Tag>>,
Expand Down
11 changes: 7 additions & 4 deletions src/openapi/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use super::{
request_body::RequestBody,
response::{Response, Responses},
Component, Deprecated, ExternalDocs, Required, Security, Server,
Component, Deprecated, ExternalDocs, Required, SecurityRequirement, Server,
};

#[non_exhaustive]
Expand Down Expand Up @@ -184,7 +184,7 @@ pub struct Operation {
pub deprecated: Option<Deprecated>,

#[serde(skip_serializing_if = "Option::is_none")]
pub security: Option<Vec<Security>>,
pub security: Option<Vec<SecurityRequirement>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub servers: Option<Vec<Server>>,
Expand Down Expand Up @@ -272,13 +272,16 @@ impl Operation {
self
}

pub fn with_securities<I: IntoIterator<Item = Security>>(mut self, securities: I) -> Self {
pub fn with_securities<I: IntoIterator<Item = SecurityRequirement>>(
mut self,
securities: I,
) -> Self {
self.security = Some(securities.into_iter().collect());

self
}

pub fn with_security(mut self, security: Security) -> Self {
pub fn with_security(mut self, security: SecurityRequirement) -> Self {
self.security.as_mut().unwrap().push(security);

self
Expand Down
15 changes: 14 additions & 1 deletion src/openapi/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "serde_json")]
use serde_json::Value;

use super::Deprecated;
use super::{security::SecuritySchema, Deprecated};

#[non_exhaustive]
#[derive(Serialize, Deserialize, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Schema {
schemas: HashMap<String, Component>,

security_schemas: HashMap<String, SecuritySchema>,
}

impl Schema {
Expand All @@ -30,6 +32,17 @@ impl Schema {

self
}

pub fn with_security_schemas<N: Into<String>, S: Into<SecuritySchema>>(
mut self,
name: N,
security_schema: S,
) -> Self {
self.security_schemas
.insert(name.into(), security_schema.into());

self
}
}

#[non_exhaustive]
Expand Down
Loading

0 comments on commit 8a9306d

Please sign in to comment.