Skip to content

Commit

Permalink
fix #21
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Sep 6, 2024
1 parent 4306bf3 commit 89434a0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
32 changes: 22 additions & 10 deletions core/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Schema> {
ReferenceOr::Item(Self::schema())
}
Expand Down Expand Up @@ -63,7 +64,10 @@ macro_rules! impl_oa_schema {
#[macro_export]
macro_rules! impl_oa_schema_passthrough {
($t:ty) => {
impl<T> $crate::OaSchema for $t where T: $crate::OaSchema {
impl<T> $crate::OaSchema for $t
where
T: $crate::OaSchema,
{
fn schema_ref() -> $crate::ReferenceOr<$crate::Schema> {
T::schema_ref()
}
Expand All @@ -80,7 +84,9 @@ impl OaSchema for () {
panic!("Call body_schema() for (), not schema().")
}

fn body_schema() -> Option<ReferenceOr<Schema>> { None }
fn body_schema() -> Option<ReferenceOr<Schema>> {
None
}
}

impl_oa_schema!(bool, Schema::new_bool());
Expand All @@ -105,7 +111,10 @@ impl_oa_schema!(f64, Schema::new_number());

impl_oa_schema!(String, Schema::new_string());

impl<T> OaSchema for Vec<T> where T: OaSchema {
impl<T> OaSchema for Vec<T>
where
T: OaSchema,
{
fn schema_ref() -> ReferenceOr<Schema> {
let inner = T::schema_ref();
ReferenceOr::Item(Schema::new_array(inner))
Expand All @@ -117,7 +126,10 @@ impl<T> OaSchema for Vec<T> where T: OaSchema {
}
}

impl<T> OaSchema for Option<T> where T: OaSchema {
impl<T> OaSchema for Option<T>
where
T: OaSchema,
{
fn schema_ref() -> ReferenceOr<Schema> {
let mut schema = T::schema_ref();
let Some(s) = schema.as_mut() else {
Expand All @@ -135,8 +147,8 @@ impl<T> OaSchema for Option<T> where T: OaSchema {
}

impl<T, E> OaSchema for Result<T, E>
where
T: OaSchema,
where
T: OaSchema,
{
fn schema_ref() -> ReferenceOr<Schema> {
T::schema_ref()
Expand All @@ -152,8 +164,8 @@ impl<T, E> OaSchema for Result<T, E>
}

impl<K, V> OaSchema for HashMap<K, V>
where
V: OaSchema,
where
V: OaSchema,
{
fn schema_ref() -> ReferenceOr<Schema> {
ReferenceOr::Item(Schema::new_map(V::schema_ref()))
Expand Down
5 changes: 4 additions & 1 deletion core/src/schema/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ impl<T> OaSchema for actix_web::web::Data<T> {
fn schema() -> Schema {
panic!("Call parameters() for Data, not schema().");
}
fn body_schema() -> Option<ReferenceOr<Schema>> {
None
}
}

impl OaSchema for actix_web::HttpRequest {
Expand Down Expand Up @@ -68,4 +71,4 @@ impl<T: OaSchema> OaSchema for serde_qs::actix::QsQuery<T> {
let p = oa::Parameter::query("query", T::schema_ref());
vec![ReferenceOr::Item(p)]
}
}
}
19 changes: 13 additions & 6 deletions macro/src/attr.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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(),
));
}
}
}
Expand All @@ -43,7 +46,8 @@ impl TryFrom<&Vec<syn::Attribute>> for FieldAttributes {
type Error = syn::Error;

fn try_from(attrs: &Vec<syn::Attribute>) -> Result<Self, Self::Error> {
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::<Result<Vec<FieldAttributes>, syn::Error>>()?;
Expand Down Expand Up @@ -85,7 +89,10 @@ pub(crate) fn get_docstring(attrs: &[syn::Attribute]) -> syn::Result<Option<Stri
_ => 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",
Expand All @@ -104,4 +111,4 @@ pub(crate) fn get_docstring(attrs: &[syn::Attribute]) -> syn::Result<Option<Stri
.collect();

Ok(Some(trimmed.join("\n")))
}
}
14 changes: 13 additions & 1 deletion macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ pub fn oasgen(attr: TokenStream, input: TokenStream) -> 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<::oasgen::RefOr<::oasgen::Parameter>>> = vec![
Expand All @@ -101,8 +112,9 @@ pub fn oasgen(attr: TokenStream, input: TokenStream) -> TokenStream {
.flatten()
.collect::<Vec<::oasgen::RefOr<::oasgen::Parameter>>>();
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
Expand Down
4 changes: 2 additions & 2 deletions oasgen/tests/test-actix/01-hello.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -30,7 +30,7 @@ pub struct CodeResponse {
}

#[oasgen]
async fn get_code(Query(GetCode { code }): Query<GetCode>) -> Json<CodeResponse> {
async fn get_code(Query(GetCode { code }): Query<GetCode>, _data: Data<String>) -> Json<CodeResponse> {
Json(CodeResponse {
found_code: matches!(&*code, "1234" | "5678"),
})
Expand Down

0 comments on commit 89434a0

Please sign in to comment.