Check for #[deprecated] attributes
This commit is contained in:
parent
bb8c93ddc1
commit
509a1c3b7b
7 changed files with 124 additions and 7 deletions
|
@ -1,4 +1,8 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
## In-dev - version TBC
|
||||||
|
### Added:
|
||||||
|
- Setting `#[deprecated]` attribute will now cause generated schemas to have the `deprecated` property set to `true`
|
||||||
|
|
||||||
## [0.7.4] - 2020-05-16
|
## [0.7.4] - 2020-05-16
|
||||||
### Added:
|
### Added:
|
||||||
- If a struct is annotated with `#[serde(deny_unknown_fields)]`, generated schema will have `additionalProperties` set to `false` (https://github.com/GREsau/schemars/pull/30)
|
- If a struct is annotated with `#[serde(deny_unknown_fields)]`, generated schema will have `additionalProperties` set to `false` (https://github.com/GREsau/schemars/pull/30)
|
||||||
|
|
|
@ -144,6 +144,13 @@ Serde docs: [container](https://serde.rs/container-attrs.html#deny_unknown_field
|
||||||
|
|
||||||
Set on a variant or field to generate this field's schema using the given function. This function must be callable as `fn(&mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema`.
|
Set on a variant or field to generate this field's schema using the given function. This function must be callable as `fn(&mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema`.
|
||||||
|
|
||||||
|
<h3 id="deprecated">
|
||||||
|
|
||||||
|
`#[deprecated]`
|
||||||
|
</h3>
|
||||||
|
|
||||||
|
Set the Rust build-in [`deprecated`](https://doc.rust-lang.org/edition-guide/rust-2018/the-compiler/an-attribute-for-deprecation.html) attribute on a struct, enum, field or variant to set the generated schema's `deprecated` keyword to `true`.
|
||||||
|
|
||||||
<h3 id="doc">
|
<h3 id="doc">
|
||||||
|
|
||||||
Doc Comments (`#[doc = "..."]`)
|
Doc Comments (`#[doc = "..."]`)
|
||||||
|
|
37
schemars/tests/deprecated.rs
Normal file
37
schemars/tests/deprecated.rs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#![allow(deprecated)]
|
||||||
|
|
||||||
|
mod util;
|
||||||
|
use schemars::JsonSchema;
|
||||||
|
use util::*;
|
||||||
|
|
||||||
|
#[derive(Debug, JsonSchema)]
|
||||||
|
#[deprecated]
|
||||||
|
pub struct DeprecatedStruct {
|
||||||
|
foo: i32,
|
||||||
|
#[deprecated]
|
||||||
|
deprecated_field: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deprecated_struct() -> TestResult {
|
||||||
|
test_default_generated_schema::<DeprecatedStruct>("deprecated-struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, JsonSchema)]
|
||||||
|
#[deprecated]
|
||||||
|
pub enum DeprecatedEnum {
|
||||||
|
Unit,
|
||||||
|
#[deprecated]
|
||||||
|
DeprecatedUnitVariant,
|
||||||
|
#[deprecated]
|
||||||
|
DeprecatedStructVariant {
|
||||||
|
foo: i32,
|
||||||
|
#[deprecated]
|
||||||
|
deprecated_field: bool,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deprecated_enum() -> TestResult {
|
||||||
|
test_default_generated_schema::<DeprecatedEnum>("deprecated-enum")
|
||||||
|
}
|
40
schemars/tests/expected/deprecated-enum.json
Normal file
40
schemars/tests/expected/deprecated-enum.json
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "DeprecatedEnum",
|
||||||
|
"deprecated": true,
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"enum": [
|
||||||
|
"Unit",
|
||||||
|
"DeprecatedUnitVariant"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"deprecated": true,
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"DeprecatedStructVariant"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"DeprecatedStructVariant": {
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"deprecated_field",
|
||||||
|
"foo"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"deprecated_field": {
|
||||||
|
"deprecated": true,
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"foo": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
20
schemars/tests/expected/deprecated-struct.json
Normal file
20
schemars/tests/expected/deprecated-struct.json
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "DeprecatedStruct",
|
||||||
|
"deprecated": true,
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"deprecated_field",
|
||||||
|
"foo"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"deprecated_field": {
|
||||||
|
"deprecated": true,
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"foo": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ use syn::Attribute;
|
||||||
pub struct SchemaMetadata {
|
pub struct SchemaMetadata {
|
||||||
pub title: Option<String>,
|
pub title: Option<String>,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
pub deprecated: bool,
|
||||||
pub read_only: bool,
|
pub read_only: bool,
|
||||||
pub write_only: bool,
|
pub write_only: bool,
|
||||||
pub default: Option<TokenStream>,
|
pub default: Option<TokenStream>,
|
||||||
|
@ -30,11 +31,13 @@ impl ToTokens for SchemaMetadata {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SchemaMetadata {
|
impl SchemaMetadata {
|
||||||
pub fn from_doc_attrs(attrs: &[Attribute]) -> SchemaMetadata {
|
pub fn from_attrs(attrs: &[Attribute]) -> SchemaMetadata {
|
||||||
let (title, description) = attr::get_title_and_desc_from_doc(attrs);
|
let (title, description) = attr::get_title_and_desc_from_doc(attrs);
|
||||||
|
let deprecated = attrs.iter().any(|a| a.path.is_ident("deprecated"));
|
||||||
SchemaMetadata {
|
SchemaMetadata {
|
||||||
title,
|
title,
|
||||||
description,
|
description,
|
||||||
|
deprecated,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,6 +65,12 @@ impl SchemaMetadata {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.deprecated {
|
||||||
|
setters.push(quote! {
|
||||||
|
metadata.deprecated = true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if self.read_only {
|
if self.read_only {
|
||||||
setters.push(quote! {
|
setters.push(quote! {
|
||||||
metadata.read_only = true;
|
metadata.read_only = true;
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub fn expr_for_container(cont: &Container) -> TokenStream {
|
||||||
Data::Enum(variants) => expr_for_enum(variants, &cont.serde_attrs),
|
Data::Enum(variants) => expr_for_enum(variants, &cont.serde_attrs),
|
||||||
};
|
};
|
||||||
|
|
||||||
let doc_metadata = SchemaMetadata::from_doc_attrs(&cont.original.attrs);
|
let doc_metadata = SchemaMetadata::from_attrs(&cont.original.attrs);
|
||||||
doc_metadata.apply_to_schema(schema_expr)
|
doc_metadata.apply_to_schema(schema_expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ fn expr_for_external_tagged_enum<'a>(
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})),
|
})),
|
||||||
});
|
});
|
||||||
let doc_metadata = SchemaMetadata::from_doc_attrs(&variant.original.attrs);
|
let doc_metadata = SchemaMetadata::from_attrs(&variant.original.attrs);
|
||||||
doc_metadata.apply_to_schema(schema_expr)
|
doc_metadata.apply_to_schema(schema_expr)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -160,7 +160,7 @@ fn expr_for_internal_tagged_enum<'a>(
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})),
|
})),
|
||||||
});
|
});
|
||||||
let doc_metadata = SchemaMetadata::from_doc_attrs(&variant.original.attrs);
|
let doc_metadata = SchemaMetadata::from_attrs(&variant.original.attrs);
|
||||||
let tag_schema = doc_metadata.apply_to_schema(tag_schema);
|
let tag_schema = doc_metadata.apply_to_schema(tag_schema);
|
||||||
|
|
||||||
match expr_for_untagged_enum_variant_for_flatten(&variant) {
|
match expr_for_untagged_enum_variant_for_flatten(&variant) {
|
||||||
|
@ -182,7 +182,7 @@ fn expr_for_internal_tagged_enum<'a>(
|
||||||
fn expr_for_untagged_enum<'a>(variants: impl Iterator<Item = &'a Variant<'a>>) -> TokenStream {
|
fn expr_for_untagged_enum<'a>(variants: impl Iterator<Item = &'a Variant<'a>>) -> TokenStream {
|
||||||
let schemas = variants.map(|variant| {
|
let schemas = variants.map(|variant| {
|
||||||
let schema_expr = expr_for_untagged_enum_variant(variant);
|
let schema_expr = expr_for_untagged_enum_variant(variant);
|
||||||
let doc_metadata = SchemaMetadata::from_doc_attrs(&variant.original.attrs);
|
let doc_metadata = SchemaMetadata::from_attrs(&variant.original.attrs);
|
||||||
doc_metadata.apply_to_schema(schema_expr)
|
doc_metadata.apply_to_schema(schema_expr)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -240,7 +240,7 @@ fn expr_for_adjacent_tagged_enum<'a>(
|
||||||
})),
|
})),
|
||||||
});
|
});
|
||||||
|
|
||||||
let doc_metadata = SchemaMetadata::from_doc_attrs(&variant.original.attrs);
|
let doc_metadata = SchemaMetadata::from_attrs(&variant.original.attrs);
|
||||||
doc_metadata.apply_to_schema(outer_schema)
|
doc_metadata.apply_to_schema(outer_schema)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -334,7 +334,7 @@ fn expr_for_struct(fields: &[Field], cattrs: Option<&serde_attr::Container>) ->
|
||||||
read_only: field.serde_attrs.skip_deserializing(),
|
read_only: field.serde_attrs.skip_deserializing(),
|
||||||
write_only: field.serde_attrs.skip_serializing(),
|
write_only: field.serde_attrs.skip_serializing(),
|
||||||
default,
|
default,
|
||||||
..SchemaMetadata::from_doc_attrs(&field.original.attrs)
|
..SchemaMetadata::from_attrs(&field.original.attrs)
|
||||||
};
|
};
|
||||||
|
|
||||||
let (ty, type_def) = type_for_schema(field, type_defs.len());
|
let (ty, type_def) = type_for_schema(field, type_defs.len());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue