From a38e7d859844b9e28ac2091cb4b0c9c552e8983c Mon Sep 17 00:00:00 2001 From: wuaoxiang Date: Fri, 1 Oct 2021 20:42:48 +0800 Subject: [PATCH 1/2] poem_openapi::Object support bool type --- poem-openapi/src/types/external/bool.rs | 44 +++++++++++++++++++++++++ poem-openapi/src/types/external/mod.rs | 1 + 2 files changed, 45 insertions(+) create mode 100644 poem-openapi/src/types/external/bool.rs diff --git a/poem-openapi/src/types/external/bool.rs b/poem-openapi/src/types/external/bool.rs new file mode 100644 index 0000000000..b3e1db1fd6 --- /dev/null +++ b/poem-openapi/src/types/external/bool.rs @@ -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: "bool", + format: Some("bool"), + }; + + fn schema_ref() -> MetaSchemaRef { + MetaSchemaRef::Inline(Self::NAME.into()) + } + + impl_value_type!(); +} + +impl ParseFromJSON for bool { + fn parse_from_json(value: Value) -> ParseResult { + 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 { + 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) + } +} diff --git a/poem-openapi/src/types/external/mod.rs b/poem-openapi/src/types/external/mod.rs index 9709e9bc18..54ab800d9d 100644 --- a/poem-openapi/src/types/external/mod.rs +++ b/poem-openapi/src/types/external/mod.rs @@ -1,4 +1,5 @@ mod any; +mod bool; #[cfg(feature = "chrono")] mod datetime; mod floats; From 6547e4b4bd520edee7186e494a3d4ae87f3b4080 Mon Sep 17 00:00:00 2001 From: Sunli Date: Sat, 2 Oct 2021 08:01:52 +0800 Subject: [PATCH 2/2] Change type from `bool` to `boolean`. --- poem-openapi/src/types/external/bool.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/poem-openapi/src/types/external/bool.rs b/poem-openapi/src/types/external/bool.rs index b3e1db1fd6..b03a5076b1 100644 --- a/poem-openapi/src/types/external/bool.rs +++ b/poem-openapi/src/types/external/bool.rs @@ -7,8 +7,8 @@ use crate::{ impl Type for bool { const NAME: TypeName = TypeName::Normal { - ty: "bool", - format: Some("bool"), + ty: "boolean", + format: None, }; fn schema_ref() -> MetaSchemaRef {