From 89434a049d2cc962522fd7ceba184620c2cc3635 Mon Sep 17 00:00:00 2001 From: Kurt Wolf Date: Fri, 6 Sep 2024 15:08:42 -0700 Subject: [PATCH] fix #21 --- core/src/schema.rs | 32 ++++++++++++++++++++--------- core/src/schema/actix.rs | 5 ++++- macro/src/attr.rs | 19 +++++++++++------ macro/src/lib.rs | 14 ++++++++++++- oasgen/tests/test-actix/01-hello.rs | 4 ++-- 5 files changed, 54 insertions(+), 20 deletions(-) diff --git a/core/src/schema.rs b/core/src/schema.rs index ddba98a..90a928c 100644 --- a/core/src/schema.rs +++ b/core/src/schema.rs @@ -20,15 +20,16 @@ mod sqlx; #[cfg(feature = "time")] mod time; +#[cfg(feature = "bigdecimal")] +mod bigdecimal; mod http; #[cfg(feature = "sid")] mod sid; -#[cfg(feature = "bigdecimal")] -mod bigdecimal; mod tuple; pub trait OaSchema { fn schema() -> Schema; + fn schema_ref() -> ReferenceOr { ReferenceOr::Item(Self::schema()) } @@ -63,7 +64,10 @@ macro_rules! impl_oa_schema { #[macro_export] macro_rules! impl_oa_schema_passthrough { ($t:ty) => { - impl $crate::OaSchema for $t where T: $crate::OaSchema { + impl $crate::OaSchema for $t + where + T: $crate::OaSchema, + { fn schema_ref() -> $crate::ReferenceOr<$crate::Schema> { T::schema_ref() } @@ -80,7 +84,9 @@ impl OaSchema for () { panic!("Call body_schema() for (), not schema().") } - fn body_schema() -> Option> { None } + fn body_schema() -> Option> { + None + } } impl_oa_schema!(bool, Schema::new_bool()); @@ -105,7 +111,10 @@ impl_oa_schema!(f64, Schema::new_number()); impl_oa_schema!(String, Schema::new_string()); -impl OaSchema for Vec where T: OaSchema { +impl OaSchema for Vec +where + T: OaSchema, +{ fn schema_ref() -> ReferenceOr { let inner = T::schema_ref(); ReferenceOr::Item(Schema::new_array(inner)) @@ -117,7 +126,10 @@ impl OaSchema for Vec where T: OaSchema { } } -impl OaSchema for Option where T: OaSchema { +impl OaSchema for Option +where + T: OaSchema, +{ fn schema_ref() -> ReferenceOr { let mut schema = T::schema_ref(); let Some(s) = schema.as_mut() else { @@ -135,8 +147,8 @@ impl OaSchema for Option where T: OaSchema { } impl OaSchema for Result - where - T: OaSchema, +where + T: OaSchema, { fn schema_ref() -> ReferenceOr { T::schema_ref() @@ -152,8 +164,8 @@ impl OaSchema for Result } impl OaSchema for HashMap - where - V: OaSchema, +where + V: OaSchema, { fn schema_ref() -> ReferenceOr { ReferenceOr::Item(Schema::new_map(V::schema_ref())) diff --git a/core/src/schema/actix.rs b/core/src/schema/actix.rs index f8f390a..7d0a897 100644 --- a/core/src/schema/actix.rs +++ b/core/src/schema/actix.rs @@ -17,6 +17,9 @@ impl OaSchema for actix_web::web::Data { fn schema() -> Schema { panic!("Call parameters() for Data, not schema()."); } + fn body_schema() -> Option> { + None + } } impl OaSchema for actix_web::HttpRequest { @@ -68,4 +71,4 @@ impl OaSchema for serde_qs::actix::QsQuery { let p = oa::Parameter::query("query", T::schema_ref()); vec![ReferenceOr::Item(p)] } -} \ No newline at end of file +} diff --git a/macro/src/attr.rs b/macro/src/attr.rs index 8c5b15e..c0110cb 100644 --- a/macro/src/attr.rs +++ b/macro/src/attr.rs @@ -1,8 +1,8 @@ use quote::ToTokens; +use serde_derive_internals::ast::Field; use structmeta::StructMeta; -use syn::LitStr; -use serde_derive_internals::ast::{Field}; use syn::spanned::Spanned; +use syn::LitStr; /// Available attributes on a struct /// For attributes that have the same name as `serde` attributes, you can use either one. @@ -34,7 +34,10 @@ impl FieldAttributes { self.skip = true; } if let Some(skip_serializing_if) = other.attrs.skip_serializing_if() { - self.skip_serializing_if = Some(LitStr::new(&skip_serializing_if.to_token_stream().to_string(), proc_macro2::Span::call_site())); + self.skip_serializing_if = Some(LitStr::new( + &skip_serializing_if.to_token_stream().to_string(), + proc_macro2::Span::call_site(), + )); } } } @@ -43,7 +46,8 @@ impl TryFrom<&Vec> for FieldAttributes { type Error = syn::Error; fn try_from(attrs: &Vec) -> Result { - let attrs = attrs.into_iter() + let attrs = attrs + .into_iter() .filter(|a| a.path().get_ident().map(|i| i == "oasgen").unwrap_or(false)) .map(|a| a.parse_args()) .collect::, syn::Error>>()?; @@ -85,7 +89,10 @@ pub(crate) fn get_docstring(attrs: &[syn::Attribute]) -> syn::Result None, }) .map(|expr| match expr { - syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(s), .. }) => Ok(s.value()), + syn::Expr::Lit(syn::ExprLit { + lit: syn::Lit::Str(s), + .. + }) => Ok(s.value()), other => Err(syn::Error::new( other.span(), "Doc comment is not a string literal", @@ -104,4 +111,4 @@ pub(crate) fn get_docstring(attrs: &[syn::Attribute]) -> syn::Result TokenStream { } }).unwrap_or_default(); let name = ast.sig.ident.to_string(); + let deprecated = attr.deprecated; + let operation_id = if let Some(id) = attr.operation_id { + let id = id.value(); + quote! { + op.operation_id = Some(#id.to_string()) + } + } else { + quote! { + ::oasgen::__private::fn_path_to_op_id(concat!(module_path!(), "::", #name)) + } + }; let submit = quote! { ::oasgen::register_operation!(concat!(module_path!(), "::", #name), || { let parameters: Vec>> = vec![ @@ -101,8 +112,9 @@ pub fn oasgen(attr: TokenStream, input: TokenStream) -> TokenStream { .flatten() .collect::>>(); let mut op = ::oasgen::Operation::default(); - op.operation_id = ::oasgen::__private::fn_path_to_op_id(concat!(module_path!(), "::", #name)); + op.operation_id = #operation_id; op.parameters = parameters; + op.deprecated = #deprecated; #body #ret #description diff --git a/oasgen/tests/test-actix/01-hello.rs b/oasgen/tests/test-actix/01-hello.rs index d160e6f..c7edaab 100644 --- a/oasgen/tests/test-actix/01-hello.rs +++ b/oasgen/tests/test-actix/01-hello.rs @@ -1,4 +1,4 @@ -use actix_web::web::{Json, Query}; +use actix_web::web::{Json, Query, Data}; use oasgen::{oasgen, OaSchema, Server}; use serde::{Deserialize, Serialize}; @@ -30,7 +30,7 @@ pub struct CodeResponse { } #[oasgen] -async fn get_code(Query(GetCode { code }): Query) -> Json { +async fn get_code(Query(GetCode { code }): Query, _data: Data) -> Json { Json(CodeResponse { found_code: matches!(&*code, "1234" | "5678"), })