Make HashSet and BTreeSet use the uniqueItems keyword in their schema (#64)

Co-authored-by: Graham Esau <gesau@hotmail.co.uk>
This commit is contained in:
Alastair Feille 2021-03-21 09:43:58 -05:00 committed by GitHub
parent c4ef7bad22
commit addac6d386
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 10 deletions

View file

@ -7,6 +7,7 @@
### Changed: ### Changed:
- Minimum supported rust version is now 1.37.0 - Minimum supported rust version is now 1.37.0
- Deriving JsonSchema on enums now sets `additionalProperties` to false on generated schemas wherever serde doesn't accept unknown properties. This includes non-unit variants of externally tagged enums, and struct-style variants of all enums that have the `deny_unknown_fields` attribute. - Deriving JsonSchema on enums now sets `additionalProperties` to false on generated schemas wherever serde doesn't accept unknown properties. This includes non-unit variants of externally tagged enums, and struct-style variants of all enums that have the `deny_unknown_fields` attribute.
- Schemas for HashSet and BTreeSet now have `uniqueItems` set to true (https://github.com/GREsau/schemars/pull/64)
### Fixed ### Fixed
- Fix use of `#[serde(transparent)]` in combination with `#[schemars(with = ...)]` (https://github.com/GREsau/schemars/pull/67) - Fix use of `#[serde(transparent)]` in combination with `#[schemars(with = ...)]` (https://github.com/GREsau/schemars/pull/67)

View file

@ -29,9 +29,38 @@ macro_rules! seq_impl {
}; };
} }
macro_rules! set_impl {
($($desc:tt)+) => {
impl $($desc)+
where
T: JsonSchema,
{
no_ref_schema!();
fn schema_name() -> String {
format!("Set_of_{}", T::schema_name())
}
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Array.into()),
array: Some(Box::new(ArrayValidation {
unique_items: Some(true),
items: Some(gen.subschema_for::<T>().into()),
..Default::default()
})),
..Default::default()
}
.into()
}
}
};
}
seq_impl!(<T> JsonSchema for std::collections::BinaryHeap<T>); seq_impl!(<T> JsonSchema for std::collections::BinaryHeap<T>);
seq_impl!(<T> JsonSchema for std::collections::BTreeSet<T>); set_impl!(<T> JsonSchema for std::collections::BTreeSet<T>);
seq_impl!(<T, H> JsonSchema for std::collections::HashSet<T, H>); set_impl!(<T, H> JsonSchema for std::collections::HashSet<T, H>);
seq_impl!(<T> JsonSchema for std::collections::LinkedList<T>); seq_impl!(<T> JsonSchema for std::collections::LinkedList<T>);
seq_impl!(<T> JsonSchema for Vec<T>); seq_impl!(<T> JsonSchema for Vec<T>);
seq_impl!(<T> JsonSchema for std::collections::VecDeque<T>); seq_impl!(<T> JsonSchema for std::collections::VecDeque<T>);

View file

@ -18,7 +18,8 @@
"items": { "items": {
"type": "integer", "type": "integer",
"format": "int" "format": "int"
} },
"uniqueItems": true
} }
} }
} }

View file

@ -24,7 +24,8 @@
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"uniqueItems": true
} }
} }
}, },

View file

@ -315,7 +315,8 @@
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"uniqueItems": true
}, },
"properties": { "properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).", "description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",
@ -670,7 +671,8 @@
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"uniqueItems": true
}, },
"properties": { "properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).", "description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",

View file

@ -255,7 +255,8 @@
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"uniqueItems": true
}, },
"properties": { "properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).", "description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",
@ -552,7 +553,8 @@
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"uniqueItems": true
}, },
"properties": { "properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).", "description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",

View file

@ -315,7 +315,8 @@
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"uniqueItems": true
}, },
"properties": { "properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).", "description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",
@ -674,7 +675,8 @@
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"uniqueItems": true
}, },
"properties": { "properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).", "description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",