Skip to content

Commit 74974d3

Browse files
authored
Support uuid v1 and arrayvec 0.7 (#142)
1 parent 043d794 commit 74974d3

File tree

15 files changed

+157
-72
lines changed

15 files changed

+157
-72
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
include:
1616
- rust: 1.37.0
1717
# exclude ui_test as the output is slightly different in rustc 1.37
18-
test_features: "--features impl_json_schema,chrono,indexmap,either,uuid,smallvec,arrayvec,enumset"
18+
test_features: "--features impl_json_schema,chrono,indexmap,either,uuid08,smallvec,arrayvec05,enumset"
1919
allow_failure: false
2020
- rust: stable
2121
test_features: "--all-features"

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
### Added:
55
- Support generic default values in `default` attributes (https://github.com/GREsau/schemars/pull/83)
66
- Add missing MIT licence text for usage of code from regex_syntax crate (https://github.com/GREsau/schemars/pull/132)
7+
- Support uuid v1 and arrayvec 0.7 via feature flags `uuid1` and `arrayvec07` (https://github.com/GREsau/schemars/pull/142)
8+
- This also adds `uuid08` and `arrayvec05` feature flags for the previously supported versions of these crates. The existing `uuid` and `arrayvec` flags are still supported for backward-compatibility, but they are **deprecated**.
9+
- Similarly, `indexmap1` feature flag is added, and `indexmap` flag is **deprecated**.
710

811
## [0.8.8] - 2021-11-25
912
### Added:

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,20 @@ println!("{}", serde_json::to_string_pretty(&schema).unwrap());
264264
- `impl_json_schema` - implements `JsonSchema` for Schemars types themselves
265265
- `preserve_order` - keep the order of struct fields in `Schema` and `SchemaObject`
266266

267-
## Optional Dependencies
268-
Schemars can implement `JsonSchema` on types from several popular crates, enabled via optional dependencies (dependency versions are shown in brackets):
269-
- [`chrono`](https://crates.io/crates/chrono) (^0.4)
270-
- [`indexmap`](https://crates.io/crates/indexmap) (^1.2)
271-
- [`either`](https://crates.io/crates/either) (^1.3)
272-
- [`uuid`](https://crates.io/crates/uuid) (^0.8)
273-
- [`smallvec`](https://crates.io/crates/smallvec) (^1.0)
274-
- [`arrayvec`](https://crates.io/crates/arrayvec) (^0.5)
275-
- [`url`](https://crates.io/crates/url) (^2.0)
276-
- [`bytes`](https://crates.io/crates/bytes) (^1.0)
277-
- [`enumset`](https://crates.io/crates/enumset) (^1.0)
278-
- [`rust_decimal`](https://crates.io/crates/rust_decimal) (^1.0)
279-
- [`bigdecimal`](https://crates.io/crates/bigdecimal) (^0.3)
267+
Schemars can implement `JsonSchema` on types from several popular crates, enabled via feature flags (dependency versions are shown in brackets):
268+
- `chrono` - [chrono](https://crates.io/crates/chrono) (^0.4)
269+
- `indexmap1` - [indexmap](https://crates.io/crates/indexmap) (^1.2)
270+
- `either` - [either](https://crates.io/crates/either) (^1.3)
271+
- `uuid08` - [uuid](https://crates.io/crates/uuid) (^0.8)
272+
- `uuid1` - [uuid](https://crates.io/crates/uuid) (^1.0)
273+
- `smallvec` - [smallvec](https://crates.io/crates/smallvec) (^1.0)
274+
- `arrayvec05` - [arrayvec](https://crates.io/crates/arrayvec) (^0.5)
275+
- `arrayvec07` - [arrayvec](https://crates.io/crates/arrayvec) (^0.7)
276+
- `url` - [url](https://crates.io/crates/url) (^2.0)
277+
- `bytes` - [bytes](https://crates.io/crates/bytes) (^1.0)
278+
- `enumset` - [enumset](https://crates.io/crates/enumset) (^1.0)
279+
- `rust_decimal` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
280+
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)
280281

281282
For example, to implement `JsonSchema` on types from `chrono`, enable it as a feature in the `schemars` dependency in your `Cargo.toml` like so:
282283

docs/4-features.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ permalink: /features/
66
---
77

88
# Feature Flags and Optional Dependencies
9-
10-
Some functionality can be selectively enabled/disabled via [Cargo features](https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section). These can be enabled when you add Schemars to your crate's cargo.toml, e.g.
11-
```toml
12-
[dependencies]
13-
schemars = { version = "0.6", features = ["chrono"] }
14-
```
15-
16-
## Feature Flags
179
- `derive` (enabled by default) - provides `#[derive(JsonSchema)]` macro
1810
- `impl_json_schema` - implements `JsonSchema` for Schemars types themselves
1911
- `preserve_order` - keep the order of struct fields in `Schema` and `SchemaObject`
2012

21-
## Optional Dependencies
22-
Schemars can implement `JsonSchema` on types from several popular crates, enabled via optional dependencies (dependency versions are shown in brackets):
23-
- [`chrono`](https://crates.io/crates/chrono) (^0.4)
24-
- [`indexmap`](https://crates.io/crates/indexmap) (^1.2)
25-
- [`either`](https://crates.io/crates/either) (^1.3)
26-
- [`uuid`](https://crates.io/crates/uuid) (^0.8)
27-
- [`smallvec`](https://crates.io/crates/smallvec) (^1.0)
28-
- [`arrayvec`](https://crates.io/crates/arrayvec) (^0.5)
29-
- [`url`](https://crates.io/crates/url) (^2.0)
30-
- [`bytes`](https://crates.io/crates/bytes) (^1.0)
31-
- [`enumset`](https://crates.io/crates/enumset) (^1.0)
32-
- [`rust_decimal`](https://crates.io/crates/rust_decimal) (^1.0)
33-
- [`bigdecimal`](https://crates.io/crates/bigdecimal) (^0.3)
13+
Schemars can implement `JsonSchema` on types from several popular crates, enabled via feature flags (dependency versions are shown in brackets):
14+
- `chrono` - [chrono](https://crates.io/crates/chrono) (^0.4)
15+
- `indexmap1` - [indexmap](https://crates.io/crates/indexmap) (^1.2)
16+
- `either` - [either](https://crates.io/crates/either) (^1.3)
17+
- `uuid08` - [uuid](https://crates.io/crates/uuid) (^0.8)
18+
- `uuid1` - [uuid](https://crates.io/crates/uuid) (^1.0)
19+
- `smallvec` - [smallvec](https://crates.io/crates/smallvec) (^1.0)
20+
- `arrayvec05` - [arrayvec](https://crates.io/crates/arrayvec) (^0.5)
21+
- `arrayvec07` - [arrayvec](https://crates.io/crates/arrayvec) (^0.7)
22+
- `url` - [url](https://crates.io/crates/url) (^2.0)
23+
- `bytes` - [bytes](https://crates.io/crates/bytes) (^1.0)
24+
- `enumset` - [enumset](https://crates.io/crates/enumset) (^1.0)
25+
- `rust_decimal` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
26+
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)
27+
28+
For example, to implement `JsonSchema` on types from `chrono`, enable it as a feature in the `schemars` dependency in your `Cargo.toml` like so:
29+
30+
```toml
31+
[dependencies]
32+
schemars = { version = "0.8", features = ["chrono"] }
33+
```

schemars/Cargo.toml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ serde_json = "1.0"
1919
dyn-clone = "1.0"
2020

2121
chrono = { version = "0.4", default-features = false, optional = true }
22-
indexmap = { version = "1.2", features=["serde-1"], optional = true }
22+
indexmap = { version = "1.2", features = ["serde-1"], optional = true }
2323
either = { version = "1.3", default-features = false, optional = true }
24-
uuid = { version = "0.8", default-features = false, optional = true }
24+
uuid08 = { version = "0.8", default-features = false, optional = true, package = "uuid" }
25+
uuid1 = { version = "1.0", default-features = false, optional = true, package = "uuid" }
2526
smallvec = { version = "1.0", optional = true }
26-
arrayvec = { version = "0.5", default-features = false, optional = true }
27+
arrayvec05 = { version = "0.5", default-features = false, optional = true, package = "arrayvec" }
28+
arrayvec07 = { version = "0.7", default-features = false, optional = true, package = "arrayvec" }
2729
url = { version = "2.0", default-features = false, optional = true }
2830
bytes = { version = "1.0", optional = true }
2931
rust_decimal = { version = "1", default-features = false, optional = true }
3032
bigdecimal = { version = "0.3", default-features = false, optional = true }
3133
enumset = { version = "1.0", optional = true }
3234

3335
[dev-dependencies]
34-
pretty_assertions = "0.6.1"
36+
pretty_assertions = "1.2.1"
3537
trybuild = "1.0"
3638

3739
[features]
@@ -48,6 +50,12 @@ impl_json_schema = ["derive"]
4850
# derive_json_schema will be removed in a later version
4951
derive_json_schema = ["impl_json_schema"]
5052

53+
# `uuid` feature contains `uuid08` only for back-compat - will be changed to include uuid 1.0 instead in a later version
54+
uuid = ["uuid08"]
55+
# `arrayvec` feature without version suffix is included only for back-compat - will be removed in a later version
56+
arrayvec = ["arrayvec05"]
57+
indexmap1 = ["indexmap"]
58+
5159
ui_test = []
5260

5361
[[test]]
@@ -64,7 +72,7 @@ required-features = ["either"]
6472

6573
[[test]]
6674
name = "uuid"
67-
required-features = ["uuid"]
75+
required-features = ["uuid08", "uuid1"]
6876

6977
[[test]]
7078
name = "smallvec"
@@ -76,7 +84,7 @@ required-features = ["bytes"]
7684

7785
[[test]]
7886
name = "arrayvec"
79-
required-features = ["arrayvec"]
87+
required-features = ["arrayvec05", "arrayvec07"]
8088

8189
[[test]]
8290
name = "schema_for_schema"

schemars/src/json_schema_impls/arrayvec.rs renamed to schemars/src/json_schema_impls/arrayvec05.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::gen::SchemaGenerator;
22
use crate::schema::*;
33
use crate::JsonSchema;
4-
use arrayvec::{Array, ArrayString, ArrayVec};
4+
use arrayvec05::{Array, ArrayString, ArrayVec};
55
use std::convert::TryInto;
66

77
// Do not set maxLength on the schema as that describes length in characters, but we only
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use crate::gen::SchemaGenerator;
2+
use crate::schema::*;
3+
use crate::JsonSchema;
4+
use arrayvec07::{ArrayString, ArrayVec};
5+
use std::convert::TryInto;
6+
7+
// Do not set maxLength on the schema as that describes length in characters, but we only
8+
// know max length in bytes.
9+
forward_impl!((<const CAP: usize> JsonSchema for ArrayString<CAP>) => String);
10+
11+
impl<T, const CAP: usize> JsonSchema for ArrayVec<T, CAP>
12+
where
13+
T: JsonSchema,
14+
{
15+
no_ref_schema!();
16+
17+
fn schema_name() -> String {
18+
format!("Array_up_to_size_{}_of_{}", CAP, T::schema_name())
19+
}
20+
21+
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
22+
SchemaObject {
23+
instance_type: Some(InstanceType::Array.into()),
24+
array: Some(Box::new(ArrayValidation {
25+
items: Some(gen.subschema_for::<T>().into()),
26+
max_items: CAP.try_into().ok(),
27+
..Default::default()
28+
})),
29+
..Default::default()
30+
}
31+
.into()
32+
}
33+
}

schemars/src/json_schema_impls/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,18 @@ macro_rules! forward_impl {
3636
}
3737

3838
mod array;
39-
#[cfg(feature = "arrayvec")]
40-
mod arrayvec;
39+
#[cfg(feature = "arrayvec05")]
40+
mod arrayvec05;
41+
#[cfg(feature = "arrayvec07")]
42+
mod arrayvec07;
4143
#[cfg(std_atomic)]
4244
mod atomic;
4345
#[cfg(feature = "bytes")]
4446
mod bytes;
4547
#[cfg(feature = "chrono")]
4648
mod chrono;
4749
mod core;
48-
#[cfg(any(feature = "rust_decimal", feature="bigdecimal"))]
50+
#[cfg(any(feature = "rust_decimal", feature = "bigdecimal"))]
4951
mod decimal;
5052
#[cfg(feature = "either")]
5153
mod either;
@@ -66,6 +68,8 @@ mod time;
6668
mod tuple;
6769
#[cfg(feature = "url")]
6870
mod url;
69-
#[cfg(feature = "uuid")]
70-
mod uuid;
71+
#[cfg(feature = "uuid08")]
72+
mod uuid08;
73+
#[cfg(feature = "uuid1")]
74+
mod uuid1;
7175
mod wrapper;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::gen::SchemaGenerator;
2+
use crate::schema::*;
3+
use crate::JsonSchema;
4+
use uuid08::Uuid;
5+
6+
impl JsonSchema for Uuid {
7+
no_ref_schema!();
8+
9+
fn schema_name() -> String {
10+
"Uuid".to_string()
11+
}
12+
13+
fn json_schema(_: &mut SchemaGenerator) -> Schema {
14+
SchemaObject {
15+
instance_type: Some(InstanceType::String.into()),
16+
format: Some("uuid".to_string()),
17+
..Default::default()
18+
}
19+
.into()
20+
}
21+
}

schemars/src/json_schema_impls/uuid.rs renamed to schemars/src/json_schema_impls/uuid1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::gen::SchemaGenerator;
22
use crate::schema::*;
33
use crate::JsonSchema;
4-
use uuid::Uuid;
4+
use uuid1::Uuid;
55

66
impl JsonSchema for Uuid {
77
no_ref_schema!();

0 commit comments

Comments
 (0)