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

refactor: change type name delimiters from <> () [] to _ #904

Merged
merged 1 commit into from
Nov 25, 2024
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
5 changes: 2 additions & 3 deletions poem-openapi-derive/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,12 @@ pub(crate) fn create_object_name(
use ::std::convert::From;
let mut name = ::std::string::String::from(#name);

name.push('<');
name.push('_');
name.push_str(&<#first as #crate_name::types::Type>::name());
#(
name.push_str(", ");
name.push_str("_");
name.push_str(&<#tail as #crate_name::types::Type>::name());
)*
name.push('>');

name
})
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/base64_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<T: AsRef<[u8]> + Send + Sync> Type for Base64<T> {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(bytes)".into()
"string_bytes".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<T: AsRef<[u8]> + Send + Sync> Type for Binary<T> {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(binary)".into()
"string_binary".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<T: Type, const LEN: usize> Type for [T; LEN] {
type RawElementValueType = T::RawValueType;

fn name() -> Cow<'static, str> {
format!("[{}]", T::name()).into()
format!("array_{}", T::name()).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Type for ObjectId {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(oid)".into()
"string_oid".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ where
type RawElementValueType = V::RawValueType;

fn name() -> Cow<'static, str> {
format!("map<string, {}>", V::name()).into()
format!("map_{}", V::name()).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/btreeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<T: Type> Type for BTreeSet<T> {
type RawElementValueType = T::RawValueType;

fn name() -> Cow<'static, str> {
format!("[{}]", T::name()).into()
format!("set_{}", T::name()).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
4 changes: 2 additions & 2 deletions poem-openapi/src/types/external/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ macro_rules! impl_datetime_types {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
concat!($type_name, "(", $format, ")").into()
concat!($type_name, "_", $format).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down Expand Up @@ -88,7 +88,7 @@ macro_rules! impl_naive_datetime_types {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
concat!($type_name, "(", $format, ")").into()
concat!($type_name, "_", $format).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Type for Decimal {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(decimal)".into()
"string_decimal".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/floats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ macro_rules! impl_type_for_floats {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
format!("number({})", $format).into()
format!("number_{}", $format).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro_rules! impl_geojson_types {
type RawElementValueType = Self;

fn name() -> ::std::borrow::Cow<'static, str> {
concat!("GeoJSON<", $name, ">").into()
concat!("GeoJSON_", $name).into()
}

fn schema_ref() -> crate::registry::MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
type RawElementValueType = V::RawValueType;

fn name() -> Cow<'static, str> {
format!("map<string, {}>", V::name()).into()
format!("map_{}", V::name()).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/hashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<T: Type, R: Send + Sync> Type for HashSet<T, R> {
type RawElementValueType = T::RawValueType;

fn name() -> Cow<'static, str> {
format!("[{}]", T::name()).into()
format!("set_{}", T::name()).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/humantime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Type for Duration {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(duration)".into()
"string_duration".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/humantime_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Type for Duration {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(duration)".into()
"string_duration".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/integers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ macro_rules! impl_type_for_integers {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
format!("integer({})", $format).into()
format!("integer_{}", $format).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ macro_rules! impl_type_for_ip {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
format!("string({})", $format).into()
format!("string_{}", $format).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<T: Type> Type for Option<T> {
type RawElementValueType = T::RawElementValueType;

fn name() -> Cow<'static, str> {
format!("optional<{}>", T::name()).into()
format!("optional_{}", T::name()).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/prost_wkt_types/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Type for prost_wkt_types::Struct {
type RawElementValueType = <Value as Type>::RawValueType;

fn name() -> Cow<'static, str> {
"Protobuf Struct".into()
"Protobuf_Struct".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/prost_wkt_types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Type for prost_wkt_types::Value {
type RawElementValueType = prost_wkt_types::Value;

fn name() -> Cow<'static, str> {
"Protobuf Value".into()
"Protobuf_Value".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Type for Regex {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(regex)".into()
"string_regex".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<T: Type> Type for &[T] {
type RawElementValueType = T::RawValueType;

fn name() -> Cow<'static, str> {
format!("[{}]", T::name()).into()
format!("slice_{}", T::name()).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
4 changes: 2 additions & 2 deletions poem-openapi/src/types/external/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Type for OffsetDateTime {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(date-time)".into()
"string_date-time".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down Expand Up @@ -83,7 +83,7 @@ macro_rules! impl_naive_datetime_types {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
concat!($type_name, "(", $format, ")").into()
concat!($type_name, "_", $format).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Type for Uri {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(uri)".into()
"string_uri".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Type for Url {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(url)".into()
"string_url".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Type for Uuid {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(uuid)".into()
"string_uuid".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/external/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<T: Type> Type for Vec<T> {
type RawElementValueType = T::RawValueType;

fn name() -> Cow<'static, str> {
format!("[{}]", T::name()).into()
format!("list_{}", T::name()).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/maybe_undefined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl<T: Type> Type for MaybeUndefined<T> {
type RawElementValueType = T::RawElementValueType;

fn name() -> Cow<'static, str> {
format!("optional<{}>", T::name()).into()
format!("optional_{}", T::name()).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
4 changes: 2 additions & 2 deletions poem-openapi/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ mod tests {
#[allow(clippy::assertions_on_constants)]
fn arc_type() {
assert!(Arc::<i32>::IS_REQUIRED);
assert_eq!(Arc::<i32>::name(), "integer(int32)");
assert_eq!(Arc::<i32>::name(), "integer_int32");
assert_eq!(Arc::new(100).as_raw_value(), Some(&100));

let value: Arc<i32> =
Expand Down Expand Up @@ -472,7 +472,7 @@ mod tests {
#[allow(unused_allocation)]
fn box_type() {
assert!(Box::<i32>::IS_REQUIRED);
assert_eq!(Box::<i32>::name(), "integer(int32)");
assert_eq!(Box::<i32>::name(), "integer_int32");
assert_eq!(Box::new(100).as_raw_value(), Some(&100));

let value: Box<i32> =
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/multipart/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Type for Upload {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
"string(binary)".into()
"string_binary".into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion poem-openapi/src/types/string_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ macro_rules! impl_string_types {
type RawElementValueType = Self;

fn name() -> Cow<'static, str> {
concat!($type_name, "(", $format, ")").into()
concat!($type_name, "_", $format).into()
}

fn schema_ref() -> MetaSchemaRef {
Expand Down
4 changes: 2 additions & 2 deletions poem-openapi/tests/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn generics() {

assert_eq!(
<Obj<i32, i64>>::name(),
"Obj<integer(int32), integer(int64)>"
"Obj_integer_int32_integer_int64"
);
let meta = get_meta::<Obj<i32, i64>>();
assert_eq!(meta.properties[0].1.unwrap_inline().ty, "integer");
Expand All @@ -56,7 +56,7 @@ fn generics() {

assert_eq!(
<Obj<f32, f64>>::name(),
"Obj<number(float), number(double)>"
"Obj_number_float_number_double"
);
let meta = get_meta::<Obj<f32, f64>>();
assert_eq!(meta.properties[0].1.unwrap_inline().ty, "number");
Expand Down
68 changes: 68 additions & 0 deletions poem-openapi/tests/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use poem::{
http::{Method, StatusCode},
test::TestClient,
Error,
};
use poem_openapi::{
param::Query,
payload::{Json, PlainText},
registry::MetaApi,
types::{MaybeUndefined, ToJSON},
ApiResponse, OpenApi, OpenApiService,
};
use serde_json::Value;

#[tokio::test]
async fn query_explode_false() {
#[derive(ApiResponse)]
#[oai(bad_request_handler = "bad_request_handler")]
enum MyResponse {
/// Ok
#[oai(status = 200)]
Ok(Json<Option<Value>>),
/// Bad Request
#[oai(status = 400)]
BadRequest(PlainText<String>),
}

fn bad_request_handler(err: Error) -> MyResponse {
MyResponse::BadRequest(PlainText(format!("!!! {err}")))
}

const fn none<T>() -> MaybeUndefined<T> {
MaybeUndefined::Undefined
}

struct Api;

#[OpenApi]
impl Api {
#[oai(path = "/abc", method = "post")]
async fn test(
&self,
#[oai(explode = false, default = "none::<Vec<u32>>")] fields: Query<
MaybeUndefined<Vec<u32>>,
>,
) -> MyResponse {
MyResponse::Ok(Json(fields.0.to_json()))
}
}

let meta: MetaApi = Api::meta().remove(0);
assert_eq!(meta.paths[0].path, "/abc");
assert_eq!(meta.paths[0].operations[0].method, Method::POST);

let ep = OpenApiService::new(Api, "test", "1.0");
let cli = TestClient::new(ep);

let resp = cli.post("/abc").query("fields", &"1,2,3").send().await;
resp.assert_status_is_ok();
resp.assert_json(&[1, 2, 3]).await;

let resp = cli.post("/abc").query("fields", &"").send().await;
resp.assert_status(StatusCode::BAD_REQUEST);

let resp = cli.post("/abc").send().await;
resp.assert_status_is_ok();
resp.assert_json(Value::Null).await;
}
Loading