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

Integrate poem-openapi with bson::oid::ObjectId #185

Merged
merged 3 commits into from
Jan 31, 2022
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
1 change: 1 addition & 0 deletions poem-openapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ hostname-validator = { version = "1.1.0", optional = true }
chrono = { version = "0.4.19", optional = true }
uuid = { version = "0.8.2", optional = true }
url = { version = "2.2.2", optional = true }
bson = { version = "2.0.0", optional = true }
once_cell = "1.9.0"

[dev-dependencies]
Expand Down
23 changes: 12 additions & 11 deletions poem-openapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,18 @@ important business implementations.

To avoid compiling unused dependencies, Poem gates certain features, some of which are disabled by default:

| Feature | Description |
|--------------|-----------------------------------------------------------------------|
| chrono | Integrate with the [`chrono` crate](https://crates.io/crates/chrono). |
| swagger-ui | Add swagger UI support |
| rapidoc | Add RapiDoc UI support |
| redoc | Add Redoc UI support |
| email | Support for email address string |
| hostname | Support for hostname string |
| uuid | Integrate with the [`uuid` crate](https://crates.io/crates/uuid) |
| url | Integrate with the [`url` crate](https://crates.io/crates/url) |
| static-files | Support for static file response |
| Feature | Description |
|------------|-----------------------------------------------------------------------|
| chrono | Integrate with the [`chrono` crate](https://crates.io/crates/chrono). |
| swagger-ui | Add swagger UI support |
| rapidoc | Add RapiDoc UI support |
| redoc | Add Redoc UI support |
| email | Support for email address string |
| hostname | Support for hostname string |
| uuid | Integrate with the [`uuid` crate](https://crates.io/crates/uuid) |
| url | Integrate with the [`url` crate](https://crates.io/crates/url) |
| bson | Integrate with the [`bson` crate](https://crates.io/crates/bson) |
| static-files | Support for static file response |

## Safety

Expand Down
1 change: 1 addition & 0 deletions poem-openapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
//! | hostname | Support for hostname string |
//! | uuid | Integrate with the [`uuid` crate](https://crates.io/crates/uuid)|
//! | url | Integrate with the [`url` crate](https://crates.io/crates/url) |
//! | bson | Integrate with the [`bson` crate](https://crates.io/crates/bson) |
//! | static-files | Support for static file response |

#![doc(html_favicon_url = "https://raw.githubusercontent.com/poem-web/poem/master/favicon.ico")]
Expand Down
74 changes: 74 additions & 0 deletions poem-openapi/src/types/external/bson.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::{borrow::Cow};

use poem::{http::HeaderValue, web::Field};
use serde_json::Value;
use bson::oid::ObjectId;

use crate::{
registry::{MetaSchema, MetaSchemaRef},
types::{
ParseError, ParseFromJSON, ParseFromMultipartField, ParseFromParameter, ParseResult,
ToHeader, ToJSON, Type,
},
};

impl Type for ObjectId {
const IS_REQUIRED: bool = true;

type RawValueType = Self;

type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"object(ObjectID)".into()
}

fn schema_ref() -> MetaSchemaRef {
MetaSchemaRef::Inline(Box::new(MetaSchema::new_with_format("object", "oid")))
}

fn as_raw_value(&self) -> Option<&Self::RawValueType> {
Some(self)
}

fn raw_element_iter<'a>(
&'a self,
) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
todo!()
}
}

impl ParseFromJSON for ObjectId {
fn parse_from_json(value: Value) -> ParseResult<Self> {
let v: ObjectId = serde_json::from_value(value)?;
Ok(v)
}
}

impl ParseFromParameter for ObjectId {
fn parse_from_parameter(value: &str) -> ParseResult<Self> {
ParseResult::Ok(ObjectId::parse_str(value)?)
}
}

#[poem::async_trait]
impl ParseFromMultipartField for ObjectId {
async fn parse_from_multipart(field: Option<Field>) -> ParseResult<Self> {
match field {
Some(field) => Ok(ObjectId::parse_str(field.text().await?)?),
None => Err(ParseError::expected_input()),
}
}
}

impl ToJSON for ObjectId {
fn to_json(&self) -> Value {
serde_json::to_value(self).unwrap()
}
}

impl ToHeader for ObjectId {
fn to_header(&self) -> Option<HeaderValue> {
HeaderValue::from_str(&self.to_hex()).ok()
}
}
2 changes: 2 additions & 0 deletions poem-openapi/src/types/external/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ mod url;
#[cfg(feature = "uuid")]
mod uuid;
mod vec;
#[cfg(feature = "bson")]
mod bson;