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 abc55ed commit ccba2f4
Show file tree
Hide file tree
Showing 21 changed files with 86 additions and 95 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/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod im;
pub mod util;
#[cfg(feature = "v2")]
pub mod v2;
#[cfg(feature = "v3")]
// #[cfg(feature = "v3")]
pub mod v3;

pub use self::error::ValidationError;
Expand Down
3 changes: 2 additions & 1 deletion core/src/v2/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub enum DataTypeFormat {
Other,
}

#[allow(clippy::to_string_trait_impl)]
impl ToString for DataTypeFormat {
fn to_string(&self) -> String {
match self {
Expand Down Expand Up @@ -439,7 +440,7 @@ pub struct PathItem<P, R> {
impl<S> PathItem<Parameter<S>, Response<S>> {
/// Normalizes this operation map.
/// - Collects and removes parameters shared across operations
/// and adds them to the list global to this map.
/// and adds them to the list global to this map.
pub fn normalize(&mut self) {
// We're using `Option<BTreeSet>` over `BTreeSet` because we need to
// differentiate between the first operation that we use for initial
Expand Down
6 changes: 3 additions & 3 deletions core/src/v2/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ impl_type_simple!(std::net::Ipv6Addr, DataType::String, DataTypeFormat::IpV6);
/// framework-specific macros:
///
/// - [`Apiv2Schema`](https://paperclip-rs.github.io/paperclip/paperclip_actix/derive.Apiv2Schema.html)
/// for schema objects.
/// for schema objects.
/// - [`Apiv2Security`](https://paperclip-rs.github.io/paperclip/paperclip_actix/derive.Apiv2Security.html)
/// for security scheme objects.
/// for security scheme objects.
/// - [`Apiv2Header`](https://paperclip-rs.github.io/paperclip/paperclip_actix/derive.Apiv2Header.html)
/// for header parameters objects.
/// for header parameters objects.
///
/// This is implemented for primitive types by default.
pub trait Apiv2Schema {
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
Loading

0 comments on commit ccba2f4

Please sign in to comment.