From b0a2f9994ba2a6d19fdf991351b2fcfde3b3e681 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Thu, 4 Jun 2020 19:39:57 +0100 Subject: [PATCH] Error on unknown schemars attr items --- CHANGELOG.md | 3 ++ schemars_derive/src/attr/mod.rs | 46 +++++++++---------- schemars_derive/src/attr/schemars_to_serde.rs | 2 +- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a8e4d9..0b38814 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/schemars_derive/src/attr/mod.rs b/schemars_derive/src/attr/mod.rs index 9bf221c..37919b3 100644 --- a/schemars_derive/src/attr/mod.rs +++ b/schemars_derive/src/attr/mod.rs @@ -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", + ); } } } diff --git a/schemars_derive/src/attr/schemars_to_serde.rs b/schemars_derive/src/attr/schemars_to_serde.rs index 380a276..bf8d389 100644 --- a/schemars_derive/src/attr/schemars_to_serde.rs +++ b/schemars_derive/src/attr/schemars_to_serde.rs @@ -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",