Error on unknown schemars attr items

This commit is contained in:
Graham Esau 2020-06-04 19:39:57 +01:00
parent a829267111
commit b0a2f9994b
3 changed files with 27 additions and 24 deletions

View file

@ -9,6 +9,9 @@
- `SchemaSettings::bool_schemas` - this has been superseded by the `ReplaceBoolSchemas` visitor
- `SchemaSettings::allow_ref_siblings` - this has been superseded by the `RemoveRefSiblings` visitor
### Fixed:
- **BREAKING CHANGE** unknown items in `#[schemars(...)]` attributes now cause a compilation error (https://github.com/GREsau/schemars/issues/18)
### Deprecated:
- `make_extensible`, `schema_for_any`, and `schema_for_none` methods on `SchemaGenerator`

View file

@ -4,6 +4,7 @@ mod schemars_to_serde;
pub use schemars_to_serde::process_serde_attrs;
use proc_macro2::{Group, Span, TokenStream, TokenTree};
use quote::ToTokens;
use serde_derive_internals::Ctxt;
use syn::parse::{self, Parse};
use syn::Meta::{List, NameValue};
@ -117,31 +118,30 @@ impl Attrs {
}
}
Meta(_meta_item) => {
// TODO uncomment this for 0.8.0 (breaking change)
// https://github.com/GREsau/schemars/issues/18
// if !ignore_errors {
// let path = meta_item
// .path()
// .into_token_stream()
// .to_string()
// .replace(' ', "");
// errors.error_spanned_by(
// meta_item.path(),
// format!("unknown schemars container attribute `{}`", path),
// );
// }
_ if ignore_errors => {}
Meta(meta_item) => {
let is_known_serde_keyword = schemars_to_serde::SERDE_KEYWORDS
.iter()
.any(|k| meta_item.path().is_ident(k));
if !is_known_serde_keyword {
let path = meta_item
.path()
.into_token_stream()
.to_string()
.replace(' ', "");
errors.error_spanned_by(
meta_item.path(),
format!("unknown schemars container attribute `{}`", path),
);
}
}
Lit(_lit) => {
// TODO uncomment this for 0.8.0 (breaking change)
// https://github.com/GREsau/schemars/issues/18
// if !ignore_errors {
// errors.error_spanned_by(
// lit,
// "unexpected literal in schemars container attribute",
// );
// }
Lit(lit) => {
errors.error_spanned_by(
lit,
"unexpected literal in schemars container attribute",
);
}
}
}

View file

@ -5,7 +5,7 @@ use syn::parse::Parser;
use syn::{Attribute, Data, Field, Meta, NestedMeta, Variant};
// List of keywords that can appear in #[serde(...)]/#[schemars(...)] attributes which we want serde_derive_internals to parse for us.
static SERDE_KEYWORDS: &[&str] = &[
pub(crate) static SERDE_KEYWORDS: &[&str] = &[
"rename",
"rename_all",
"deny_unknown_fields",