Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify the code generated for unit, externally tagged and internally tagged enums #266

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 116 additions & 1 deletion schemars/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ JSON Schema types.

#[cfg(feature = "impl_json_schema")]
use crate as schemars;
#[cfg(feature = "impl_json_schema")]
use crate::JsonSchema;
use crate::{Map, Set};
use serde::{Deserialize, Serialize};
Expand All @@ -26,6 +25,32 @@ pub enum Schema {
Object(SchemaObject),
}

macro_rules! with_metadata_fn {
($method:ident, $name:ident, $ty:ty) => {
with_metadata_fn!(
concat!(
"Returns the schema with the ", stringify!($name), " metadata field set."
),
$method,
$name,
$ty
);
};
($doc:expr, $method:ident, $name:ident, $ty:ty) => {
#[doc = $doc]
pub fn $method(self, $name: impl Into<$ty>) -> Self {
let value = $name.into();
if value == <$ty>::default() {
self
} else {
let mut schema_obj = self.into_object();
schema_obj.metadata().$name = value.into();
Schema::Object(schema_obj)
}
}
};
}

impl Schema {
/// Creates a new `$ref` schema.
///
Expand Down Expand Up @@ -71,6 +96,86 @@ impl Schema {
},
}
}

with_metadata_fn!(with_description, description, String);
with_metadata_fn!(with_id, id, String);
with_metadata_fn!(with_title, title, String);
with_metadata_fn!(with_deprecated, deprecated, bool);
with_metadata_fn!(with_read_only, read_only, bool);
with_metadata_fn!(with_write_only, write_only, bool);
with_metadata_fn!(with_default, default, Value);

pub fn with_examples<I: IntoIterator<Item = Value>>(self, examples: I) -> Self {
let mut schema_obj = self.into_object();
schema_obj.metadata().examples.extend(examples);
Schema::Object(schema_obj)
}

/// Create a schema for a unit enum
pub fn new_unit_enum(variant: &str) -> Self {
Self::Object(SchemaObject {
instance_type: Some(InstanceType::String.into()),
enum_values: Some(vec![variant.into()]),
..SchemaObject::default()
})
}

/// Create a schema for an externally tagged enum
pub fn new_externally_tagged_enum(variant: &str, sub_schema: Schema) -> Self {
Self::Object(SchemaObject {
instance_type: Some(InstanceType::Object.into()),
object: Some(Box::new(ObjectValidation {
properties: {
let mut props = Map::new();
props.insert(variant.to_owned(), sub_schema);
props
},
required: {
let mut required = Set::new();
required.insert(variant.to_owned());
required
},
// Externally tagged variants must prohibit additional
// properties irrespective of the disposition of
// `deny_unknown_fields`. If additional properties were allowed
// one could easily construct an object that validated against
// multiple variants since here it's the properties rather than
// the values of a property that distingish between variants.
additional_properties: Some(Box::new(false.into())),
..Default::default()
})),
..SchemaObject::default()
})
}

/// Create a schema for an internally tagged enum
pub fn new_internally_tagged_enum(tag_name: &str, variant: &str, deny_unknown_fields: bool) -> Self {
let tag_schema = Schema::Object(SchemaObject {
instance_type: Some(
InstanceType::String.into(),
),
enum_values: Some(vec![variant.into()]),
..Default::default()
});
Self::Object(SchemaObject {
instance_type: Some(InstanceType::Object.into()),
object: Some(Box::new(ObjectValidation {
properties: {
let mut props = Map::new();
props.insert(tag_name.to_owned(), tag_schema);
props
},
required: {
let mut required = Set::new();
required.insert(tag_name.to_owned());
required
},
additional_properties: deny_unknown_fields.then(|| Box::new(false.into())),
..Default::default()
})),
..SchemaObject::default()
})
}
}

impl From<SchemaObject> for Schema {
Expand Down Expand Up @@ -486,6 +591,16 @@ pub struct ObjectValidation {
pub property_names: Option<Box<Schema>>,
}

impl ObjectValidation {
pub fn insert_property<T: ?Sized + JsonSchema>(&mut self, key: &str, mut has_default: bool, required: bool, schema: Schema) {
self.properties.insert(key.to_owned(), schema);
has_default |= T::_schemars_private_is_option();
if required || !has_default {
self.required.insert(key.to_owned());
}
}
}

/// The possible types of values in JSON Schema documents.
///
/// See [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).
Expand Down
19 changes: 8 additions & 11 deletions schemars_derive/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ impl<'a> SchemaMetadata<'a> {
if !setters.is_empty() {
*schema_expr = quote! {{
let schema = #schema_expr;
schemars::_private::apply_metadata(schema, schemars::schema::Metadata {
#(#setters)*
..Default::default()
})
schema #(#setters)*
}}
}
}
Expand All @@ -30,29 +27,29 @@ impl<'a> SchemaMetadata<'a> {

if let Some(title) = &self.title {
setters.push(quote! {
title: Some(#title.to_owned()),
.with_title(#title)
});
}
if let Some(description) = &self.description {
setters.push(quote! {
description: Some(#description.to_owned()),
.with_description(#description)
});
}

if self.deprecated {
setters.push(quote! {
deprecated: true,
.with_deprecated(true)
});
}

if self.read_only {
setters.push(quote! {
read_only: true,
.with_read_only(true)
});
}
if self.write_only {
setters.push(quote! {
write_only: true,
.with_write_only(true)
});
}

Expand All @@ -63,13 +60,13 @@ impl<'a> SchemaMetadata<'a> {
}
});
setters.push(quote! {
examples: vec![#(#examples),*].into_iter().flatten().collect(),
.with_examples([#(#examples),*].into_iter().flatten())
});
}

if let Some(default) = &self.default {
setters.push(quote! {
default: #default.and_then(|d| schemars::_schemars_maybe_to_value!(d)),
.with_default(#default.and_then(|d| schemars::_schemars_maybe_to_value!(d)))
});
}

Expand Down
86 changes: 13 additions & 73 deletions schemars_derive/src/schema_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn expr_for_external_tagged_enum<'a>(
let unit_names = unit_variants.iter().map(|v| v.name());
let unit_schema = schema_object(quote! {
instance_type: Some(schemars::schema::InstanceType::String.into()),
enum_values: Some(vec![#(#unit_names.into()),*]),
enum_values: Some([#(#unit_names),*].into_iter().map(|v| v.into()).collect()),
});

if complex_variants.is_empty() {
Expand All @@ -178,35 +178,14 @@ fn expr_for_external_tagged_enum<'a>(
let name = variant.name();

let mut schema_expr = if variant.is_unit() && variant.attrs.with.is_none() {
schema_object(quote! {
instance_type: Some(schemars::schema::InstanceType::String.into()),
enum_values: Some(vec![#name.into()]),
})
quote! {
schemars::schema::Schema::new_unit_enum(#name)
}
} else {
let sub_schema = expr_for_untagged_enum_variant(variant, deny_unknown_fields);
schema_object(quote! {
instance_type: Some(schemars::schema::InstanceType::Object.into()),
object: Some(Box::new(schemars::schema::ObjectValidation {
properties: {
let mut props = schemars::Map::new();
props.insert(#name.to_owned(), #sub_schema);
props
},
required: {
let mut required = schemars::Set::new();
required.insert(#name.to_owned());
required
},
// Externally tagged variants must prohibit additional
// properties irrespective of the disposition of
// `deny_unknown_fields`. If additional properties were allowed
// one could easily construct an object that validated against
// multiple variants since here it's the properties rather than
// the values of a property that distingish between variants.
additional_properties: Some(Box::new(false.into())),
..Default::default()
})),
})
quote! {
schemars::schema::Schema::new_externally_tagged_enum(#name, #sub_schema)
}
};

variant
Expand All @@ -227,43 +206,16 @@ fn expr_for_internal_tagged_enum<'a>(
) -> TokenStream {
let mut unique_names = HashSet::new();
let mut count = 0;
let set_additional_properties = if deny_unknown_fields {
quote! {
additional_properties: Some(Box::new(false.into())),
}
} else {
TokenStream::new()
};
let variant_schemas = variants
.map(|variant| {
unique_names.insert(variant.name());
count += 1;

let name = variant.name();
let type_schema = schema_object(quote! {
instance_type: Some(schemars::schema::InstanceType::String.into()),
enum_values: Some(vec![#name.into()]),
});

let mut tag_schema = schema_object(quote! {
instance_type: Some(schemars::schema::InstanceType::Object.into()),
object: Some(Box::new(schemars::schema::ObjectValidation {
properties: {
let mut props = schemars::Map::new();
props.insert(#tag_name.to_owned(), #type_schema);
props
},
required: {
let mut required = schemars::Set::new();
required.insert(#tag_name.to_owned());
required
},
// As we're creating a "wrapper" object, we can honor the
// disposition of deny_unknown_fields.
#set_additional_properties
..Default::default()
})),
});
let mut tag_schema = quote! {
schemars::schema::Schema::new_internally_tagged_enum(#tag_name, #name, #deny_unknown_fields)
};

variant.attrs.as_metadata().apply_to_schema(&mut tag_schema);

Expand Down Expand Up @@ -498,19 +450,8 @@ fn expr_for_struct(

let (ty, type_def) = type_for_field_schema(field);

let maybe_insert_required = match (&default, field.validation_attrs.required()) {
(Some(_), _) => TokenStream::new(),
(None, false) => {
quote! {
if !<#ty as schemars::JsonSchema>::_schemars_private_is_option() {
object_validation.required.insert(#name.to_owned());
}
}
}
(None, true) => quote! {
object_validation.required.insert(#name.to_owned());
},
};
let has_default = default.is_some();
let required = field.validation_attrs.required();

let metadata = SchemaMetadata {
read_only: field.serde_attrs.skip_deserializing(),
Expand All @@ -536,8 +477,7 @@ fn expr_for_struct(
quote! {
{
#type_def
object_validation.properties.insert(#name.to_owned(), #schema_expr);
#maybe_insert_required
object_validation.insert_property::<#ty>(#name, #has_default, #required, #schema_expr);
}
}
})
Expand Down
Loading