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

support custom properties #446

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions core/src/v3/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{invalid_referenceor, v2};
use std::ops::Deref;
use std::{iter::FromIterator, ops::Deref};

impl From<v2::DefaultSchemaRaw> for openapiv3::ReferenceOr<Box<openapiv3::Schema>> {
fn from(v2: v2::DefaultSchemaRaw) -> Self {
Expand Down Expand Up @@ -30,7 +30,7 @@ impl From<v2::DefaultSchemaRaw> for openapiv3::ReferenceOr<openapiv3::Schema> {
description: v2.description,
discriminator: None,
default: None,
extensions: indexmap::IndexMap::new(),
extensions: indexmap::IndexMap::from_iter(v2.extensions.into_iter()),
},
schema_kind: {
if let Some(data_type) = v2.data_type {
Expand Down
45 changes: 43 additions & 2 deletions macros/src/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,38 @@ fn extract_example(attrs: &[Attribute]) -> Option<String> {
None
}

fn extract_extensions(attrs: &[Attribute]) -> proc_macro2::TokenStream {
let mut ext_attrs = HashMap::new();
let attrs = extract_openapi_attrs(attrs);
for attr in attrs.flat_map(|attr| attr.into_iter()) {
if let NestedMeta::Meta(Meta::NameValue(nv)) = attr {
if let Some(id) = nv.path.get_ident() {
let s_id = id.to_string();
if s_id.starts_with('x') {
ext_attrs.insert(id.clone(), nv.lit);
}
}
}
}

if ext_attrs.is_empty() {
quote! { std::collections::BTreeMap::new() }
} else {
// ext_attrs
let mut items = quote!();
for (k, v) in ext_attrs {
let lit_key = syn::LitStr::new(&k.to_string().replace('_', "-"), k.span());
items.extend(quote!((#lit_key.to_string(),serde_json::to_value(&#v).unwrap()),));
}

let gen = quote! {
std::collections::BTreeMap::from([#items])
};

gen
}
}

/// Actual parser and emitter for `api_v2_schema` macro.
pub fn emit_v2_definition(input: TokenStream) -> TokenStream {
let item_ast = match crate::expect_struct_or_enum(input) {
Expand All @@ -779,6 +811,8 @@ pub fn emit_v2_definition(input: TokenStream) -> TokenStream {
quote!(None)
};

let extensions = extract_extensions(&item_ast.attrs);

let props = SerdeProps::from_item_attrs(&item_ast.attrs);

let name = &item_ast.ident;
Expand Down Expand Up @@ -846,6 +880,7 @@ pub fn emit_v2_definition(input: TokenStream) -> TokenStream {
let mut schema = DefaultSchemaRaw {
name: Some(#schema_name.into()),
example: #example,
extensions:#extensions,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add space?

..Default::default()
};
};
Expand All @@ -855,6 +890,7 @@ pub fn emit_v2_definition(input: TokenStream) -> TokenStream {
let mut schema = DefaultSchemaRaw {
name: Some(Self::__paperclip_schema_name()), // Add name for later use.
example: #example,
extensions:#extensions,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add space?

.. Default::default()
};
};
Expand Down Expand Up @@ -1572,12 +1608,17 @@ fn handle_field_struct(
quote!({})
};

let extensions = extract_extensions(&field.attrs);

let gen = if !SerdeFlatten::exists(&field.attrs) {
quote!({
let mut s = #ty_ref::raw_schema();
if !#docs.is_empty() {
s.description = Some(#docs.to_string());
}

s.extensions = #extensions;

#example;
schema.properties.insert(#field_name.into(), s.into());

Expand All @@ -1587,9 +1628,9 @@ fn handle_field_struct(
})
} else {
quote!({
let s = #ty_ref::raw_schema();
let mut s = #ty_ref::raw_schema();
schema.properties.extend(s.properties);

s.extensions = #extensions;
if #ty_ref::required() {
schema.required.extend(s.required);
}
Expand Down
5 changes: 5 additions & 0 deletions macros/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ fn schema_fields(name: &Ident, is_ref: bool) -> proc_macro2::TokenStream {
pub required: std::collections::BTreeSet<String>,
));

gen.extend(quote!(
#[serde(skip_serializing_if = "std::collections::BTreeMap::is_empty", default = "std::collections::BTreeMap::default")]
pub extensions: std::collections::BTreeMap<String, serde_json::Value>,
));

if is_ref {
gen.extend(quote!(
#[serde(skip)]
Expand Down