Skip to content

Commit

Permalink
fix(openapiv3/indexmap): don't use the indexmap type directly
Browse files Browse the repository at this point in the history
This helps decouple us from the internals of the openapiv3 crate.

Signed-off-by: Tiago Castro <[email protected]>
  • Loading branch information
tiagolobocastro committed Jun 27, 2024
1 parent f733d92 commit 6379d04
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 79 deletions.
3 changes: 1 addition & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ actix-web-validator3 = { version = "3.0", optional = true, package = "actix-web-
validator12 = { version = "0.12", features = ["derive"], optional = true, package = "validator" }
validator14 = { version = "0.14", features = ["derive"], optional = true, package = "validator" }
openapiv3 = { version = "1.0.3", optional = true }
indexmap = { version = "2.0", features = ["serde"], optional = true }

[features]
# actix-web support
Expand All @@ -54,7 +53,7 @@ nightly = ["paperclip-macros/nightly"]

# OpenAPI support (v2 and codegen)
v2 = ["paperclip-macros/v2"]
v3 = ["v2", "openapiv3", "indexmap"]
v3 = ["v2", "openapiv3"]
codegen = ["v2", "heck", "log"]
uuid = ["uuid0"]
uuid0 = ["uuid0_dep"]
Expand Down
2 changes: 1 addition & 1 deletion core/src/v3/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ impl From<v2::Contact> for openapiv3::Contact {
name: v2.name,
url: v2.url,
email: v2.email,
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
}
}
}
2 changes: 1 addition & 1 deletion core/src/v3/external_documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ impl From<v2::ExternalDocs> for openapiv3::ExternalDocumentation {
openapiv3::ExternalDocumentation {
description: v2.description,
url: v2.url,
extensions: indexmap::IndexMap::new(),
extensions: Self::default().extensions,
}
}
}
6 changes: 3 additions & 3 deletions core/src/v3/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ impl From<v2::Header> for openapiv3::Header {
style: Default::default(),
required: false,
deprecated: None,
format: openapiv3::ParameterSchemaOrContent::Content(indexmap::IndexMap::new()),
format: openapiv3::ParameterSchemaOrContent::Content(Default::default()),
example: None,
extensions: indexmap::IndexMap::new(),
examples: indexmap::IndexMap::new(),
extensions: Default::default(),
examples: Default::default(),
}
}
}
10 changes: 5 additions & 5 deletions core/src/v3/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ impl From<v2::Info> for openapiv3::Info {
contact: v2.contact.map(|c| c.into()),
license: v2.license.map(From::from),
version: v2.version,
extensions: v2.extensions.into_iter().fold(
indexmap::IndexMap::new(),
|mut i, (k, v)| {
extensions: v2
.extensions
.into_iter()
.fold(Default::default(), |mut i, (k, v)| {
i.insert(k, v);
i
},
),
}),
}
}
}
2 changes: 1 addition & 1 deletion core/src/v3/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ impl From<v2::License> for openapiv3::License {
openapiv3::License {
name: v2.name.unwrap_or_default(),
url: v2.url,
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
}
}
}
40 changes: 17 additions & 23 deletions core/src/v3/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@ impl From<v2::DefaultApiRaw> for openapiv3::OpenAPI {
.security_schemes
.insert(name, openapiv3::ReferenceOr::Item(scheme.into()));
}
components.responses = v2
.responses
.iter()
.fold(indexmap::IndexMap::new(), |mut i, b| {
i.insert(b.0.to_string(), b.1.clone().into());
components.responses = v2.responses.iter().fold(Default::default(), |mut i, b| {
i.insert(b.0.to_string(), b.1.clone().into());
i
});
spec.extensions = v2
.extensions
.into_iter()
.fold(Default::default(), |mut i, (k, v)| {
i.insert(k, v);
i
});
spec.extensions =
v2.extensions
.into_iter()
.fold(indexmap::IndexMap::new(), |mut i, (k, v)| {
i.insert(k, v);
i
});
spec.paths = openapiv3::Paths {
paths: v2.paths.iter().fold(indexmap::IndexMap::new(), |mut i, b| {
paths: v2.paths.iter().fold(Default::default(), |mut i, b| {
i.insert(
b.0.to_string(),
openapiv3::ReferenceOr::Item(b.1.clone().into()),
Expand All @@ -42,13 +39,10 @@ impl From<v2::DefaultApiRaw> for openapiv3::OpenAPI {
..Default::default()
};

components.schemas = v2
.definitions
.iter()
.fold(indexmap::IndexMap::new(), |mut i, b| {
i.insert(b.0.to_string(), b.1.clone().into());
i
});
components.schemas = v2.definitions.iter().fold(Default::default(), |mut i, b| {
i.insert(b.0.to_string(), b.1.clone().into());
i
});
spec.components = Some(components);

spec
Expand Down Expand Up @@ -77,7 +71,7 @@ fn openapi3_servers(
url: format!("{}://{}{}", scheme_str, host, base.as_deref().unwrap_or("")),
description: None,
variables: None,
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
}
})
.collect()
Expand All @@ -86,15 +80,15 @@ fn openapi3_servers(
url: format!("//{}{}", host, base.as_deref().unwrap_or("")),
description: None,
variables: None,
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
}]
}
} else {
vec![openapiv3::Server {
url: base.unwrap_or_else(|| "/".to_string()),
description: None,
variables: None,
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
}]
}
}
23 changes: 13 additions & 10 deletions core/src/v3/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ impl From<v2::Operation<v2::DefaultParameterRaw, v2::DefaultResponseRaw>> for op
request_body,
responses: openapiv3::Responses {
default: None,
responses: v2
.responses
.iter()
.fold(indexmap::IndexMap::new(), |mut i, (k, v)| {
responses: v2.responses.iter().fold(
openapiv3::Responses::default().responses,
|mut i, (k, v)| {
if let Ok(code) = k.parse::<u16>() {
let code = openapiv3::StatusCode::Code(code);
i.insert(
Expand All @@ -101,7 +100,8 @@ impl From<v2::Operation<v2::DefaultParameterRaw, v2::DefaultResponseRaw>> for op
);
}
i
}),
},
),
..Default::default()
},
deprecated: v2.deprecated,
Expand All @@ -112,16 +112,19 @@ impl From<v2::Operation<v2::DefaultParameterRaw, v2::DefaultResponseRaw>> for op
v2.security
.iter()
.map(|s| {
s.iter().fold(indexmap::IndexMap::new(), |mut i, (k, v)| {
i.insert(k.to_string(), v.clone());
i
})
s.iter().fold(
openapiv3::SecurityRequirement::default(),
|mut i, (k, v)| {
i.insert(k.to_string(), v.clone());
i
},
)
})
.collect(),
)
},
servers: vec![],
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
}
}
}
2 changes: 1 addition & 1 deletion core/src/v3/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl From<v2::DefaultPathItemRaw> for openapiv3::PathItem {
})
.parameters
},
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
description: None,
summary: None,
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/v3/request_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ impl From<v2::DefaultParameterRaw>
))),
},
example: None,
examples: indexmap::IndexMap::new(),
examples: Default::default(),
explode: None,
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
};

match v2.in_ {
Expand Down Expand Up @@ -49,12 +49,12 @@ impl From<v2::DefaultParameterRaw>
schema: v2.schema.map(|s| s.into()),
..Default::default()
};
let mut map = indexmap::IndexMap::new();
let mut map = openapiv3::RequestBody::default().content;
map.insert(v2::SpecFormat::Json.mime().0.to_string(), media);
map
},
required: v2.required,
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
})),
}
}
Expand Down
18 changes: 9 additions & 9 deletions core/src/v3/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl From<OperationResponse<'_>> for openapiv3::Response {
.response
.headers
.iter()
.fold(indexmap::IndexMap::new(), |mut i, b| {
.fold(Default::default(), |mut i, b| {
i.insert(
b.0.to_string(),
openapiv3::ReferenceOr::Item(b.1.clone().into()),
Expand All @@ -34,7 +34,7 @@ impl From<OperationResponse<'_>> for openapiv3::Response {
..Default::default()
};

let mut map = indexmap::IndexMap::new();
let mut map = openapiv3::Response::default().content;
match v2.operation.produces.as_ref() {
Some(range) => {
for mime in range {
Expand All @@ -52,11 +52,11 @@ impl From<OperationResponse<'_>> for openapiv3::Response {
}
map
}
None => indexmap::IndexMap::new(),
None => Default::default(),
}
},
extensions: indexmap::IndexMap::new(),
links: indexmap::IndexMap::new(),
extensions: Default::default(),
links: Default::default(),
}
}
}
Expand All @@ -72,13 +72,13 @@ impl From<OperationEitherResponse<'_>> for openapiv3::ReferenceOr<openapiv3::Res
Either::Left(reference) => {
let response = openapiv3::Response {
description: "".to_string(),
headers: indexmap::IndexMap::new(),
headers: Default::default(),
content: {
let media = openapiv3::MediaType {
schema: Some(reference.into()),
..Default::default()
};
let mut map = indexmap::IndexMap::new();
let mut map = openapiv3::Response::default().content;
match v2.operation.produces.as_ref() {
Some(range) => {
for mime in range {
Expand All @@ -92,8 +92,8 @@ impl From<OperationEitherResponse<'_>> for openapiv3::ReferenceOr<openapiv3::Res
}
map
},
links: indexmap::IndexMap::new(),
extensions: indexmap::IndexMap::new(),
links: Default::default(),
extensions: Default::default(),
};
openapiv3::ReferenceOr::Item(response)
}
Expand Down
12 changes: 5 additions & 7 deletions core/src/v3/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl From<v2::DefaultSchemaRaw> for openapiv3::ReferenceOr<openapiv3::Schema> {
description: v2.description,
discriminator: None,
default: None,
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
},
schema_kind: {
if let Some(data_type) = v2.data_type {
Expand Down Expand Up @@ -175,12 +175,10 @@ fn v2_data_type_to_v3(
v2::DataType::Object => {
openapiv3::SchemaKind::Type(openapiv3::Type::Object(openapiv3::ObjectType {
properties: {
properties
.iter()
.fold(indexmap::IndexMap::new(), |mut i, b| {
i.insert(b.0.to_string(), b.1.deref().clone().into());
i
})
properties.iter().fold(Default::default(), |mut i, b| {
i.insert(b.0.to_string(), b.1.deref().clone().into());
i
})
},
required: required.iter().cloned().collect::<Vec<_>>(),
additional_properties: None,
Expand Down
24 changes: 13 additions & 11 deletions core/src/v3/security_scheme.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use super::v2;

macro_rules! to_indexmap {
($v2:expr) => {
$v2.scopes.iter().fold(Default::default(), |mut i, (k, v)| {
i.insert(k.to_string(), v.to_string());
i
})
};
}

impl From<v2::SecurityScheme> for openapiv3::SecurityScheme {
fn from(v2: v2::SecurityScheme) -> Self {
match v2.type_.as_str() {
Expand All @@ -18,37 +27,30 @@ impl From<v2::SecurityScheme> for openapiv3::SecurityScheme {
description: v2.description,
},
"oauth2" => {
let scopes = v2
.scopes
.iter()
.fold(indexmap::IndexMap::new(), |mut i, (k, v)| {
i.insert(k.clone(), v.clone());
i
});
let flow = v2.flow.unwrap_or_default();
openapiv3::SecurityScheme::OAuth2 {
flows: openapiv3::OAuth2Flows {
implicit: match flow.as_str() {
"implicit" => Some(openapiv3::OAuth2Flow::Implicit {
authorization_url: v2.auth_url.clone().unwrap_or_default(),
refresh_url: None,
scopes: scopes.clone(),
scopes: to_indexmap!(v2),
}),
_ => None,
},
password: match flow.as_str() {
"password" => Some(openapiv3::OAuth2Flow::Password {
refresh_url: None,
token_url: v2.token_url.clone().unwrap_or_default(),
scopes: scopes.clone(),
scopes: to_indexmap!(v2),
}),
_ => None,
},
client_credentials: match flow.as_str() {
"application" => Some(openapiv3::OAuth2Flow::ClientCredentials {
refresh_url: None,
token_url: v2.token_url.clone().unwrap_or_default(),
scopes: scopes.clone(),
scopes: to_indexmap!(v2),
}),
_ => None,
},
Expand All @@ -57,7 +59,7 @@ impl From<v2::SecurityScheme> for openapiv3::SecurityScheme {
authorization_url: v2.auth_url.clone().unwrap_or_default(),
token_url: v2.token_url.clone().unwrap_or_default(),
refresh_url: None,
scopes,
scopes: to_indexmap!(v2),
}),
_ => None,
},
Expand Down
2 changes: 1 addition & 1 deletion core/src/v3/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ impl From<v2::Tag> for openapiv3::Tag {
name: v2.name,
description: v2.description,
external_docs: v2.external_docs.map(openapiv3::ExternalDocumentation::from),
extensions: indexmap::IndexMap::new(),
extensions: Default::default(),
}
}
}

0 comments on commit 6379d04

Please sign in to comment.