Skip to content

Commit

Permalink
Implement JsonSchema on EnumSet type
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcampbell authored and GREsau committed Oct 10, 2021
1 parent 515a87a commit d059686
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
include:
- rust: 1.37.0
# exclude ui_test as the output is slightly different in rustc 1.37
test_features: "--features impl_json_schema,chrono,indexmap,either,uuid,smallvec,arrayvec"
test_features: "--features impl_json_schema,chrono,indexmap,either,uuid,smallvec,arrayvec,enumset"
allow_failure: false
- rust: stable
test_features: "--all-features"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,4 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
- [`arrayvec`](https://crates.io/crates/arrayvec) (^0.5)
- [`url`](https://crates.io/crates/url) (^2.0)
- [`bytes`](https://crates.io/crates/bytes) (^1.0)
- [`enumset`](https://crates.io/crates/enumset) (^1.0)
1 change: 1 addition & 0 deletions docs/4-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
- [`arrayvec`](https://crates.io/crates/arrayvec) (^0.5)
- [`url`](https://crates.io/crates/url) (^2.0)
- [`bytes`](https://crates.io/crates/bytes) (^1.0)
- [`enumset`](https://crates.io/crates/enumset) (^1.0)
5 changes: 5 additions & 0 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ smallvec = { version = "1.0", optional = true }
arrayvec = { version = "0.5", default-features = false, optional = true }
url = { version = "2.0", default-features = false, optional = true }
bytes = { version = "1.0", optional = true }
enumset = { version = "1.0", optional = true }

[dev-dependencies]
pretty_assertions = "0.6.1"
Expand Down Expand Up @@ -87,5 +88,9 @@ required-features = ["ui_test"]
name = "url"
required-features = ["url"]

[[test]]
name = "enumset"
required-features = ["enumset"]

[package.metadata.docs.rs]
all-features = true
6 changes: 6 additions & 0 deletions schemars/src/json_schema_impls/enumset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use enumset::{EnumSet, EnumSetType};

forward_impl!((<T> JsonSchema for EnumSet<T> where T: EnumSetType + JsonSchema) => std::collections::BTreeSet<T>);
2 changes: 2 additions & 0 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ mod chrono;
mod core;
#[cfg(feature = "either")]
mod either;
#[cfg(feature = "enumset")]
mod enumset;
mod ffi;
#[cfg(feature = "indexmap")]
mod indexmap;
Expand Down
1 change: 1 addition & 0 deletions schemars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
- [`arrayvec`](https://crates.io/crates/arrayvec) (^0.5)
- [`url`](https://crates.io/crates/url) (^2.0)
- [`bytes`](https://crates.io/crates/bytes) (^1.0)
- [`enumset`](https://crates.io/crates/enumset) (^1.0)
*/

/// The map type used by schemars types.
Expand Down
15 changes: 15 additions & 0 deletions schemars/tests/enumset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod util;
use enumset::{EnumSet, EnumSetType};
use schemars::JsonSchema;
use util::*;

#[derive(EnumSetType, JsonSchema)]
enum Foo {
Bar,
Baz,
}

#[test]
fn enumset() -> TestResult {
test_default_generated_schema::<EnumSet<Foo>>("enumset")
}
18 changes: 18 additions & 0 deletions schemars/tests/expected/enumset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Set_of_Foo",
"type": "array",
"items": {
"$ref": "#/definitions/Foo"
},
"uniqueItems": true,
"definitions": {
"Foo": {
"type": "string",
"enum": [
"Bar",
"Baz"
]
}
}
}

0 comments on commit d059686

Please sign in to comment.