Skip to content

Commit b0a2f99

Browse files
committed
Error on unknown schemars attr items
1 parent a829267 commit b0a2f99

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
- `SchemaSettings::bool_schemas` - this has been superseded by the `ReplaceBoolSchemas` visitor
1010
- `SchemaSettings::allow_ref_siblings` - this has been superseded by the `RemoveRefSiblings` visitor
1111

12+
### Fixed:
13+
- **BREAKING CHANGE** unknown items in `#[schemars(...)]` attributes now cause a compilation error (https://github.com/GREsau/schemars/issues/18)
14+
1215
### Deprecated:
1316
- `make_extensible`, `schema_for_any`, and `schema_for_none` methods on `SchemaGenerator`
1417

schemars_derive/src/attr/mod.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod schemars_to_serde;
44
pub use schemars_to_serde::process_serde_attrs;
55

66
use proc_macro2::{Group, Span, TokenStream, TokenTree};
7+
use quote::ToTokens;
78
use serde_derive_internals::Ctxt;
89
use syn::parse::{self, Parse};
910
use syn::Meta::{List, NameValue};
@@ -117,31 +118,30 @@ impl Attrs {
117118
}
118119
}
119120

120-
Meta(_meta_item) => {
121-
// TODO uncomment this for 0.8.0 (breaking change)
122-
// https://github.com/GREsau/schemars/issues/18
123-
// if !ignore_errors {
124-
// let path = meta_item
125-
// .path()
126-
// .into_token_stream()
127-
// .to_string()
128-
// .replace(' ', "");
129-
// errors.error_spanned_by(
130-
// meta_item.path(),
131-
// format!("unknown schemars container attribute `{}`", path),
132-
// );
133-
// }
121+
_ if ignore_errors => {}
122+
123+
Meta(meta_item) => {
124+
let is_known_serde_keyword = schemars_to_serde::SERDE_KEYWORDS
125+
.iter()
126+
.any(|k| meta_item.path().is_ident(k));
127+
if !is_known_serde_keyword {
128+
let path = meta_item
129+
.path()
130+
.into_token_stream()
131+
.to_string()
132+
.replace(' ', "");
133+
errors.error_spanned_by(
134+
meta_item.path(),
135+
format!("unknown schemars container attribute `{}`", path),
136+
);
137+
}
134138
}
135139

136-
Lit(_lit) => {
137-
// TODO uncomment this for 0.8.0 (breaking change)
138-
// https://github.com/GREsau/schemars/issues/18
139-
// if !ignore_errors {
140-
// errors.error_spanned_by(
141-
// lit,
142-
// "unexpected literal in schemars container attribute",
143-
// );
144-
// }
140+
Lit(lit) => {
141+
errors.error_spanned_by(
142+
lit,
143+
"unexpected literal in schemars container attribute",
144+
);
145145
}
146146
}
147147
}

schemars_derive/src/attr/schemars_to_serde.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syn::parse::Parser;
55
use syn::{Attribute, Data, Field, Meta, NestedMeta, Variant};
66

77
// List of keywords that can appear in #[serde(...)]/#[schemars(...)] attributes which we want serde_derive_internals to parse for us.
8-
static SERDE_KEYWORDS: &[&str] = &[
8+
pub(crate) static SERDE_KEYWORDS: &[&str] = &[
99
"rename",
1010
"rename_all",
1111
"deny_unknown_fields",

0 commit comments

Comments
 (0)