Skip to content

Commit

Permalink
Merge pull request #46 from pymongo/master
Browse files Browse the repository at this point in the history
poem_openapi::Object support bool type
  • Loading branch information
sunli829 authored Oct 2, 2021
2 parents ebd8043 + 6547e4b commit cb10b50
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
44 changes: 44 additions & 0 deletions poem-openapi/src/types/external/bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use serde_json::Value;

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

impl Type for bool {
const NAME: TypeName = TypeName::Normal {
ty: "boolean",
format: None,
};

fn schema_ref() -> MetaSchemaRef {
MetaSchemaRef::Inline(Self::NAME.into())
}

impl_value_type!();
}

impl ParseFromJSON for bool {
fn parse_from_json(value: Value) -> ParseResult<Self> {
if let Value::Bool(value) = value {
Ok(value)
} else {
Err(ParseError::expected_type(value))
}
}
}

impl ParseFromParameter for bool {
fn parse_from_parameter(value: Option<&str>) -> ParseResult<Self> {
match value {
Some(value) => value.parse().map_err(ParseError::custom),
None => Err(ParseError::expected_input()),
}
}
}

impl ToJSON for bool {
fn to_json(&self) -> Value {
Value::Bool(*self)
}
}
1 change: 1 addition & 0 deletions poem-openapi/src/types/external/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod any;
mod bool;
#[cfg(feature = "chrono")]
mod datetime;
mod floats;
Expand Down

0 comments on commit cb10b50

Please sign in to comment.