Skip to content

Commit

Permalink
Ignore bound set in serde attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Oct 2, 2022
1 parent 104dccc commit 4754a13
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions schemars_derive/src/attr/schemars_to_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) static SERDE_KEYWORDS: &[&str] = &[
"flatten",
"remote",
"transparent",
// Special case - `bound` is removed from serde attrs, so is only respected when present in schemars attr.
"bound",
// Special cases - `with`/`serialize_with` are passed to serde but not copied from schemars attrs to serde attrs.
// This is because we want to preserve any serde attribute's `serialize_with` value to determine whether the field's
Expand Down Expand Up @@ -57,7 +58,7 @@ fn process_serde_field_attrs<'a>(ctxt: &Ctxt, fields: impl Iterator<Item = &'a m
}

fn process_attrs(ctxt: &Ctxt, attrs: &mut Vec<Attribute>) {
// Remove #[serde(...)] attributes (some may be re-added later)
// Remove #[serde(...)] attributes (some may be re-added later)
let (serde_attrs, other_attrs): (Vec<_>, Vec<_>) =
attrs.drain(..).partition(|at| at.path.is_ident("serde"));
*attrs = other_attrs;
Expand Down Expand Up @@ -94,7 +95,10 @@ fn process_attrs(ctxt: &Ctxt, attrs: &mut Vec<Attribute>) {
.flatten()
{
if let Ok(i) = get_meta_ident(&ctxt, &meta) {
if !schemars_meta_names.contains(&i) && SERDE_KEYWORDS.contains(&i.as_ref()) {
if !schemars_meta_names.contains(&i)
&& SERDE_KEYWORDS.contains(&i.as_ref())
&& i != "bound"
{
serde_meta.push(meta);
}
}
Expand Down Expand Up @@ -166,10 +170,10 @@ mod tests {
#[misc]
struct MyStruct {
/// blah blah blah
#[serde(skip_serializing_if = "some_fn")]
#[serde(skip_serializing_if = "some_fn", bound = "removed")]
field1: i32,
#[serde(serialize_with = "se", deserialize_with = "de")]
#[schemars(with = "with")]
#[schemars(with = "with", bound = "bound")]
field2: i32,
#[schemars(skip)]
#[serde(skip_serializing)]
Expand All @@ -184,7 +188,7 @@ mod tests {
#[doc = r" blah blah blah"]
#[serde(skip_serializing_if = "some_fn")]
field1: i32,
#[schemars(with = "with")]
#[schemars(with = "with", bound = "bound")]
#[serde(serialize_with = "se")]
field2: i32,
#[schemars(skip)]
Expand Down

0 comments on commit 4754a13

Please sign in to comment.