Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
15 changes: 12 additions & 3 deletions ohkami_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ mod worker;
///
/// ### Container attributes
///
/// - `#[openapi(component)]` : define the schema in `components`
/// #### `#[openapi(component)]`
/// Define the schema in `components`
///
/// ### Field attributes
///
/// no field attributes yet
/// #### `#[openapi(schema_with = "schema_fn")]`
/// Use `schema_fn()` instead for the field. `schema_fn`:
///
/// - must be callable as `fn() -> impl Into<ohkami::openapi::SchemaRef>`
/// - can be a path like `schema_fns::a_schema`
///
/// ### Variant attributes
///
/// no variant attributes yet
/// #### `#[openapi(schema_with = "schema_fn")]`
/// Use `schema_fn()` instead for the variant. `schema_fn`:
///
/// - must be callable as `fn() -> impl Into<ohkami::openapi::SchemaRef>`
/// - can be a path like `schema_fns::a_schema`
///
/// <br>
///
Expand Down
45 changes: 38 additions & 7 deletions ohkami_macros/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use self::attributes::{ContainerAttributes, FieldAttributes, VariantAttributes};
use crate::util::{inner_Option, extract_doc_comment, extract_doc_attrs};
use proc_macro2::{TokenStream, Span};
use quote::quote;
use syn::{Item, ItemFn, ItemStruct, ItemEnum, Fields, FieldsNamed, FieldsUnnamed, Variant, Visibility, Ident, LitInt, LitStr, Type, token, Token, punctuated::Punctuated};
use syn::{Item, ItemFn, ItemStruct, ItemEnum, Fields, FieldsNamed, FieldsUnnamed, Variant, Visibility, Ident, LitInt, LitStr, Type, Path, token, Token, punctuated::Punctuated};

pub(super) fn derive_schema(input: TokenStream) -> syn::Result<TokenStream> {
return match syn::parse2::<Item>(input)? {
Expand Down Expand Up @@ -147,6 +147,15 @@ pub(super) fn derive_schema(input: TokenStream) -> syn::Result<TokenStream> {
ident = Ident::new(&rename, span);
}

if let Some(schema_with) = &field_attrs.openapi.schema_with {
let property_name = LitStr::new(&ident.to_string(), ident.span());
let schema_with = syn::parse_str::<Path>(schema_with)?;
properties.push(quote! {
schema = schema.property(#property_name, #schema_with());
});
continue
}

let ty = &f.ty;
let inner_option = inner_Option(ty);

Expand Down Expand Up @@ -212,11 +221,18 @@ pub(super) fn derive_schema(input: TokenStream) -> syn::Result<TokenStream> {

let ty = &f.ty;

let mut schema = quote! {
::ohkami::openapi::schema::Schema::<::ohkami::openapi::schema::Type::any>::from(
<#ty as ::ohkami::openapi::Schema>::schema()
.into(/* SchemaRef */).into_inline().unwrap()
)
let mut schema = if let Some(schema_with) = &FieldAttributes::new(&f.attrs)?.openapi.schema_with {
let schema_with = syn::parse_str::<Path>(schema_with)?;
quote! {
#schema_with()
}
} else {
quote! {
::ohkami::openapi::schema::Schema::<::ohkami::openapi::schema::Type::any>::from(
<#ty as ::ohkami::openapi::Schema>::schema()
.into(/* SchemaRef */).into_inline().unwrap()
)
}
};

if let Some(description) = extract_doc_comment(&f.attrs) {
Expand Down Expand Up @@ -254,6 +270,14 @@ pub(super) fn derive_schema(input: TokenStream) -> syn::Result<TokenStream> {
continue
}

if let Some(schema_with) = &field_attrs.openapi.schema_with {
let schema_with = syn::parse_str::<Path>(schema_with)?;
type_schemas.push(quote! {
#schema_with()
});
continue
}

let ty = match inner_Option(&u.ty) {
Some(inner_option) => inner_option,
None => u.ty.clone()
Expand Down Expand Up @@ -354,7 +378,14 @@ pub(super) fn derive_schema(input: TokenStream) -> syn::Result<TokenStream> {
}
}

let mut schema = schema_of_fields(v.fields, &container_attrs)?;
let mut schema = if let Some(schema_with) = &variant_attrs.openapi.schema_with {
let schema_with = syn::parse_str::<Path>(schema_with)?;
quote! {
#schema_with()
}
} else {
schema_of_fields(v.fields, &container_attrs)?
};

schema = match (
&*container_attrs.serde.tag,
Expand Down
2 changes: 0 additions & 2 deletions ohkami_macros/src/openapi/attributes/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub(crate) struct FieldAttributes {
impl syn::parse::Parse for FieldAttributes {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut this = FieldAttributes::default();

while let Ok(i) = input.parse::<Ident>() {
match &*i.to_string() {
"schema_with" => {
Expand Down Expand Up @@ -69,7 +68,6 @@ pub(crate) struct VariantAttributes {
impl syn::parse::Parse for VariantAttributes {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut this = VariantAttributes::default();

while let Ok(i) = input.parse::<Ident>() {
match &*i.to_string() {
"schema_with" => {
Expand Down
1 change: 1 addition & 0 deletions ohkami_openapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod _util;
#[cfg(test)] mod _test;

pub mod schema;
pub use schema::SchemaRef;

pub mod security;
pub use security::SecurityScheme;
Expand Down
1 change: 1 addition & 0 deletions samples/openapi-schema-enums/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openapi.json
Loading