Skip to content

Commit cf5a47f

Browse files
feat: implement FromRef for SecurityScheme (#221)
Co-authored-by: Rob Ede <[email protected]>
1 parent 6116c01 commit cf5a47f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

crates/oas3/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Document compatibility with OAS v3.1.1.
66
- Remove non-spec-compliant `spec::Header::allow_empty_value` field.
77
- Minimum supported Rust version (MSRV) is now 1.80.
8+
- Implement `FromRef` for `spec::SecurityScheme`
89

910
## 0.16.1
1011

crates/oas3/src/spec/security_scheme.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use serde::{Deserialize, Serialize};
22

33
use super::Flows;
4+
use crate::{
5+
spec::{FromRef, Ref, RefError, RefType},
6+
Spec,
7+
};
48

59
/// Defines a security scheme that can be used by the operations.
610
///
@@ -101,6 +105,22 @@ pub enum SecurityScheme {
101105
},
102106
}
103107

108+
impl FromRef for SecurityScheme {
109+
fn from_ref(spec: &Spec, path: &str) -> Result<Self, RefError> {
110+
let refpath = path.parse::<Ref>()?;
111+
112+
match refpath.kind {
113+
RefType::SecurityScheme => spec
114+
.components
115+
.as_ref()
116+
.and_then(|cs| cs.security_schemes.get(&refpath.name))
117+
.ok_or_else(|| RefError::Unresolvable(path.to_owned()))
118+
.and_then(|oor| oor.resolve(spec)),
119+
typ => Err(RefError::MismatchedType(typ, RefType::SecurityScheme)),
120+
}
121+
}
122+
}
123+
104124
#[cfg(test)]
105125
mod tests {
106126
use url::Url;

0 commit comments

Comments
 (0)