Set "default" on generated schemas (#6)
This commit is contained in:
parent
81eb53b590
commit
7e23e2ad7a
7 changed files with 69 additions and 12 deletions
|
@ -2,12 +2,13 @@ use crate::doc_attrs;
|
|||
use proc_macro2::TokenStream;
|
||||
use syn::Attribute;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct SchemaMetadata {
|
||||
pub title: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub read_only: bool,
|
||||
pub write_only: bool,
|
||||
pub default: Option<TokenStream>,
|
||||
}
|
||||
|
||||
pub fn set_metadata_on_schema_from_docs(
|
||||
|
@ -40,6 +41,7 @@ pub fn set_metadata_on_schema(schema_expr: TokenStream, metadata: &SchemaMetadat
|
|||
metadata.description = Some(#description.to_owned());
|
||||
})
|
||||
}
|
||||
|
||||
if metadata.read_only {
|
||||
setters.push(quote! {
|
||||
metadata.read_only = true;
|
||||
|
@ -51,6 +53,15 @@ pub fn set_metadata_on_schema(schema_expr: TokenStream, metadata: &SchemaMetadat
|
|||
})
|
||||
}
|
||||
|
||||
if let Some(default) = &metadata.default {
|
||||
setters.push(quote! {
|
||||
metadata.default = match serde_json::json!(#default) {
|
||||
serde_json::value::Value::Null => None,
|
||||
d => Some(d),
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
if setters.is_empty() {
|
||||
return schema_expr;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue