Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: GREsau/schemars
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.8.8
Choose a base ref
...
head repository: GREsau/schemars
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.8.9
Choose a head ref
  • 9 commits
  • 48 files changed
  • 3 contributors

Commits on May 15, 2022

  1. Support generic default values

    I'm trying to `derive(JsonSchema)` on a field with a default that relies
    on type inference to determine it's return type.  This causes compile
    errors because schemars calls the default function without providing
    any types for inference to use.
    
    This changes that - wraps the `default` in a closure with a defined
    return value that it immediately calls.  Feels a bit hacky, but I
    couldn't think of a better way to fix this.
    obmarg authored and GREsau committed May 15, 2022

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    kamilmysliwiec Kamil Mysliwiec
    Copy the full SHA
    b38a553 View commit details
  2. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    kamilmysliwiec Kamil Mysliwiec
    Copy the full SHA
    feb6c4b View commit details
  3. Copy the full SHA
    85ed613 View commit details
  4. Update changelog

    GREsau committed May 15, 2022
    Copy the full SHA
    70f4fb0 View commit details
  5. Add license notice to regex_syntax.rs

    The comment in the code says this code was copied from another source
    with an appropriate link. However just local to this file it is not
    clear what the license terms of that code are. I added the license
    notice to this file that governs the particular code that was copied
    over.
    
    I would like to use this crate in an environment where we want to be
    clear about licensing. Hopefully this is an okay change otherwise I
    would be more than happy to do this a different way as long as the
    license provenance is clear.
    rokob authored and GREsau committed May 15, 2022
    Copy the full SHA
    115a9ee View commit details
  6. Update changelog

    GREsau committed May 15, 2022
    Copy the full SHA
    29d2455 View commit details
  7. Fix build warnings from tests

    Deriving `Debug` is no longer enough to suppress warnings about unused fields
    GREsau committed May 15, 2022
    Copy the full SHA
    043d794 View commit details

Commits on May 16, 2022

  1. Copy the full SHA
    74974d3 View commit details
  2. v0.8.9

    GREsau committed May 16, 2022
    Copy the full SHA
    1610294 View commit details
Showing with 402 additions and 209 deletions.
  1. +1 −1 .github/workflows/ci.yml
  2. +8 −0 CHANGELOG.md
  3. +14 −13 README.md
  4. +21 −21 docs/4-features.md
  5. +16 −8 schemars/Cargo.toml
  6. +1 −1 schemars/src/json_schema_impls/{arrayvec.rs → arrayvec05.rs}
  7. +33 −0 schemars/src/json_schema_impls/arrayvec07.rs
  8. +9 −5 schemars/src/json_schema_impls/mod.rs
  9. +21 −0 schemars/src/json_schema_impls/uuid08.rs
  10. +1 −1 schemars/src/json_schema_impls/{uuid.rs → uuid1.rs}
  11. +16 −13 schemars/src/lib.rs
  12. +14 −5 schemars/tests/arrayvec.rs
  13. +2 −1 schemars/tests/chrono.rs
  14. +3 −2 schemars/tests/crate_alias.rs
  15. +27 −13 schemars/tests/default.rs
  16. +6 −4 schemars/tests/deprecated.rs
  17. +3 −2 schemars/tests/dereference.rs
  18. +16 −13 schemars/tests/docs.rs
  19. +20 −14 schemars/tests/enum.rs
  20. +20 −14 schemars/tests/enum_deny_unknown_fields.rs
  21. +2 −2 schemars/tests/examples.rs
  22. +14 −0 schemars/tests/expected/generic_default.json
  23. +2 −1 schemars/tests/ffi.rs
  24. +10 −6 schemars/tests/flatten.rs
  25. +2 −1 schemars/tests/indexmap.rs
  26. +17 −13 schemars/tests/inline_subschemas.rs
  27. +4 −3 schemars/tests/macro.rs
  28. +2 −1 schemars/tests/nonzero_ints.rs
  29. +2 −1 schemars/tests/property_name.rs
  30. +2 −1 schemars/tests/range.rs
  31. +3 −3 schemars/tests/remote_derive.rs
  32. +1 −2 schemars/tests/remote_derive_generic.rs
  33. +4 −2 schemars/tests/result.rs
  34. +8 −4 schemars/tests/schema_name.rs
  35. +4 −4 schemars/tests/schema_with_enum.rs
  36. +6 −6 schemars/tests/schema_with_struct.rs
  37. +4 −3 schemars/tests/skip.rs
  38. +6 −5 schemars/tests/struct.rs
  39. +2 −1 schemars/tests/struct_additional_properties.rs
  40. +2 −1 schemars/tests/time.rs
  41. +6 −4 schemars/tests/transparent.rs
  42. +2 −1 schemars/tests/url.rs
  43. +0 −1 schemars/tests/util/mod.rs
  44. +7 −3 schemars/tests/uuid.rs
  45. +8 −5 schemars/tests/validate.rs
  46. +3 −3 schemars_derive/Cargo.toml
  47. +26 −0 schemars_derive/src/regex_syntax.rs
  48. +1 −1 schemars_derive/src/schema_exprs.rs
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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,enumset"
test_features: "--features impl_json_schema,chrono,indexmap,either,uuid08,smallvec,arrayvec05,enumset"
allow_failure: false
- rust: stable
test_features: "--all-features"
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [0.8.9] - 2022-05-16
### Added:
- Support generic default values in `default` attributes (https://github.com/GREsau/schemars/pull/83)
- Add missing MIT licence text for usage of code from regex_syntax crate (https://github.com/GREsau/schemars/pull/132)
- Support uuid v1 and arrayvec 0.7 via feature flags `uuid1` and `arrayvec07` (https://github.com/GREsau/schemars/pull/142)
- 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**.
- Similarly, `indexmap1` feature flag is added, and `indexmap` flag is **deprecated**.

## [0.8.8] - 2021-11-25
### Added:
- Implement `JsonSchema` for types from `rust_decimal` and `bigdecimal` crates (https://github.com/GREsau/schemars/pull/101)
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -264,19 +264,20 @@ println!("{}", serde_json::to_string_pretty(&schema).unwrap());
- `impl_json_schema` - implements `JsonSchema` for Schemars types themselves
- `preserve_order` - keep the order of struct fields in `Schema` and `SchemaObject`

## Optional Dependencies
Schemars can implement `JsonSchema` on types from several popular crates, enabled via optional dependencies (dependency versions are shown in brackets):
- [`chrono`](https://crates.io/crates/chrono) (^0.4)
- [`indexmap`](https://crates.io/crates/indexmap) (^1.2)
- [`either`](https://crates.io/crates/either) (^1.3)
- [`uuid`](https://crates.io/crates/uuid) (^0.8)
- [`smallvec`](https://crates.io/crates/smallvec) (^1.0)
- [`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)
- [`rust_decimal`](https://crates.io/crates/rust_decimal) (^1.0)
- [`bigdecimal`](https://crates.io/crates/bigdecimal) (^0.3)
Schemars can implement `JsonSchema` on types from several popular crates, enabled via feature flags (dependency versions are shown in brackets):
- `chrono` - [chrono](https://crates.io/crates/chrono) (^0.4)
- `indexmap1` - [indexmap](https://crates.io/crates/indexmap) (^1.2)
- `either` - [either](https://crates.io/crates/either) (^1.3)
- `uuid08` - [uuid](https://crates.io/crates/uuid) (^0.8)
- `uuid1` - [uuid](https://crates.io/crates/uuid) (^1.0)
- `smallvec` - [smallvec](https://crates.io/crates/smallvec) (^1.0)
- `arrayvec05` - [arrayvec](https://crates.io/crates/arrayvec) (^0.5)
- `arrayvec07` - [arrayvec](https://crates.io/crates/arrayvec) (^0.7)
- `url` - [url](https://crates.io/crates/url) (^2.0)
- `bytes` - [bytes](https://crates.io/crates/bytes) (^1.0)
- `enumset` - [enumset](https://crates.io/crates/enumset) (^1.0)
- `rust_decimal` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)

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

42 changes: 21 additions & 21 deletions docs/4-features.md
Original file line number Diff line number Diff line change
@@ -6,28 +6,28 @@ permalink: /features/
---

# Feature Flags and Optional Dependencies

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.
```toml
[dependencies]
schemars = { version = "0.6", features = ["chrono"] }
```

## Feature Flags
- `derive` (enabled by default) - provides `#[derive(JsonSchema)]` macro
- `impl_json_schema` - implements `JsonSchema` for Schemars types themselves
- `preserve_order` - keep the order of struct fields in `Schema` and `SchemaObject`

## Optional Dependencies
Schemars can implement `JsonSchema` on types from several popular crates, enabled via optional dependencies (dependency versions are shown in brackets):
- [`chrono`](https://crates.io/crates/chrono) (^0.4)
- [`indexmap`](https://crates.io/crates/indexmap) (^1.2)
- [`either`](https://crates.io/crates/either) (^1.3)
- [`uuid`](https://crates.io/crates/uuid) (^0.8)
- [`smallvec`](https://crates.io/crates/smallvec) (^1.0)
- [`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)
- [`rust_decimal`](https://crates.io/crates/rust_decimal) (^1.0)
- [`bigdecimal`](https://crates.io/crates/bigdecimal) (^0.3)
Schemars can implement `JsonSchema` on types from several popular crates, enabled via feature flags (dependency versions are shown in brackets):
- `chrono` - [chrono](https://crates.io/crates/chrono) (^0.4)
- `indexmap1` - [indexmap](https://crates.io/crates/indexmap) (^1.2)
- `either` - [either](https://crates.io/crates/either) (^1.3)
- `uuid08` - [uuid](https://crates.io/crates/uuid) (^0.8)
- `uuid1` - [uuid](https://crates.io/crates/uuid) (^1.0)
- `smallvec` - [smallvec](https://crates.io/crates/smallvec) (^1.0)
- `arrayvec05` - [arrayvec](https://crates.io/crates/arrayvec) (^0.5)
- `arrayvec07` - [arrayvec](https://crates.io/crates/arrayvec) (^0.7)
- `url` - [url](https://crates.io/crates/url) (^2.0)
- `bytes` - [bytes](https://crates.io/crates/bytes) (^1.0)
- `enumset` - [enumset](https://crates.io/crates/enumset) (^1.0)
- `rust_decimal` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)

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

```toml
[dependencies]
schemars = { version = "0.8", features = ["chrono"] }
```
24 changes: 16 additions & 8 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ name = "schemars"
description = "Generate JSON Schemas from Rust code"
homepage = "https://graham.cool/schemars/"
repository = "https://github.com/GREsau/schemars"
version = "0.8.8"
version = "0.8.9"
authors = ["Graham Esau <gesau@hotmail.co.uk>"]
edition = "2018"
license = "MIT"
@@ -13,25 +13,27 @@ categories = ["encoding"]
build = "build.rs"

[dependencies]
schemars_derive = { version = "=0.8.8", optional = true, path = "../schemars_derive" }
schemars_derive = { version = "=0.8.9", optional = true, path = "../schemars_derive" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
dyn-clone = "1.0"

chrono = { version = "0.4", default-features = false, optional = true }
indexmap = { version = "1.2", features=["serde-1"], optional = true }
indexmap = { version = "1.2", features = ["serde-1"], optional = true }
either = { version = "1.3", default-features = false, optional = true }
uuid = { version = "0.8", default-features = false, optional = true }
uuid08 = { version = "0.8", default-features = false, optional = true, package = "uuid" }
uuid1 = { version = "1.0", default-features = false, optional = true, package = "uuid" }
smallvec = { version = "1.0", optional = true }
arrayvec = { version = "0.5", default-features = false, optional = true }
arrayvec05 = { version = "0.5", default-features = false, optional = true, package = "arrayvec" }
arrayvec07 = { version = "0.7", default-features = false, optional = true, package = "arrayvec" }
url = { version = "2.0", default-features = false, optional = true }
bytes = { version = "1.0", optional = true }
rust_decimal = { version = "1", default-features = false, optional = true }
bigdecimal = { version = "0.3", default-features = false, optional = true }
enumset = { version = "1.0", optional = true }

[dev-dependencies]
pretty_assertions = "0.6.1"
pretty_assertions = "1.2.1"
trybuild = "1.0"

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

# `uuid` feature contains `uuid08` only for back-compat - will be changed to include uuid 1.0 instead in a later version
uuid = ["uuid08"]
# `arrayvec` feature without version suffix is included only for back-compat - will be removed in a later version
arrayvec = ["arrayvec05"]
indexmap1 = ["indexmap"]

ui_test = []

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

[[test]]
name = "uuid"
required-features = ["uuid"]
required-features = ["uuid08", "uuid1"]

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

[[test]]
name = "arrayvec"
required-features = ["arrayvec"]
required-features = ["arrayvec05", "arrayvec07"]

[[test]]
name = "schema_for_schema"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use arrayvec::{Array, ArrayString, ArrayVec};
use arrayvec05::{Array, ArrayString, ArrayVec};
use std::convert::TryInto;

// Do not set maxLength on the schema as that describes length in characters, but we only
33 changes: 33 additions & 0 deletions schemars/src/json_schema_impls/arrayvec07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use arrayvec07::{ArrayString, ArrayVec};
use std::convert::TryInto;

// Do not set maxLength on the schema as that describes length in characters, but we only
// know max length in bytes.
forward_impl!((<const CAP: usize> JsonSchema for ArrayString<CAP>) => String);

impl<T, const CAP: usize> JsonSchema for ArrayVec<T, CAP>
where
T: JsonSchema,
{
no_ref_schema!();

fn schema_name() -> String {
format!("Array_up_to_size_{}_of_{}", CAP, T::schema_name())
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Array.into()),
array: Some(Box::new(ArrayValidation {
items: Some(gen.subschema_for::<T>().into()),
max_items: CAP.try_into().ok(),
..Default::default()
})),
..Default::default()
}
.into()
}
}
14 changes: 9 additions & 5 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
@@ -36,16 +36,18 @@ macro_rules! forward_impl {
}

mod array;
#[cfg(feature = "arrayvec")]
mod arrayvec;
#[cfg(feature = "arrayvec05")]
mod arrayvec05;
#[cfg(feature = "arrayvec07")]
mod arrayvec07;
#[cfg(std_atomic)]
mod atomic;
#[cfg(feature = "bytes")]
mod bytes;
#[cfg(feature = "chrono")]
mod chrono;
mod core;
#[cfg(any(feature = "rust_decimal", feature="bigdecimal"))]
#[cfg(any(feature = "rust_decimal", feature = "bigdecimal"))]
mod decimal;
#[cfg(feature = "either")]
mod either;
@@ -66,6 +68,8 @@ mod time;
mod tuple;
#[cfg(feature = "url")]
mod url;
#[cfg(feature = "uuid")]
mod uuid;
#[cfg(feature = "uuid08")]
mod uuid08;
#[cfg(feature = "uuid1")]
mod uuid1;
mod wrapper;
21 changes: 21 additions & 0 deletions schemars/src/json_schema_impls/uuid08.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use uuid08::Uuid;

impl JsonSchema for Uuid {
no_ref_schema!();

fn schema_name() -> String {
"Uuid".to_string()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
format: Some("uuid".to_string()),
..Default::default()
}
.into()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use uuid::Uuid;
use uuid1::Uuid;

impl JsonSchema for Uuid {
no_ref_schema!();
29 changes: 16 additions & 13 deletions schemars/src/lib.rs
Original file line number Diff line number Diff line change
@@ -259,25 +259,28 @@ println!("{}", serde_json::to_string_pretty(&schema).unwrap());
- `impl_json_schema` - implements `JsonSchema` for Schemars types themselves
- `preserve_order` - keep the order of struct fields in `Schema` and `SchemaObject`
## Optional Dependencies
Schemars can implement `JsonSchema` on types from several popular crates, enabled via optional dependencies (dependency versions are shown in brackets):
- [`chrono`](https://crates.io/crates/chrono) (^0.4)
- [`indexmap`](https://crates.io/crates/indexmap) (^1.2)
- [`either`](https://crates.io/crates/either) (^1.3)
- [`uuid`](https://crates.io/crates/uuid) (^0.8)
- [`smallvec`](https://crates.io/crates/smallvec) (^1.0)
- [`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)
- [`rust_decimal`](https://crates.io/crates/rust_decimal) (^1.0)
- [`bigdecimal`](https://crates.io/crates/bigdecimal) (^0.3)
Schemars can implement `JsonSchema` on types from several popular crates, enabled via feature flags (dependency versions are shown in brackets):
- `chrono` - [chrono](https://crates.io/crates/chrono) (^0.4)
- `indexmap1` - [indexmap](https://crates.io/crates/indexmap) (^1.2)
- `either` - [either](https://crates.io/crates/either) (^1.3)
- `uuid08` - [uuid](https://crates.io/crates/uuid) (^0.8)
- `uuid1` - [uuid](https://crates.io/crates/uuid) (^1.0)
- `smallvec` - [smallvec](https://crates.io/crates/smallvec) (^1.0)
- `arrayvec05` - [arrayvec](https://crates.io/crates/arrayvec) (^0.5)
- `arrayvec07` - [arrayvec](https://crates.io/crates/arrayvec) (^0.7)
- `url` - [url](https://crates.io/crates/url) (^2.0)
- `bytes` - [bytes](https://crates.io/crates/bytes) (^1.0)
- `enumset` - [enumset](https://crates.io/crates/enumset) (^1.0)
- `rust_decimal` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)
For example, to implement `JsonSchema` on types from `chrono`, enable it as a feature in the `schemars` dependency in your `Cargo.toml` like so:
```toml
[dependencies]
schemars = { version = "0.8", features = ["chrono"] }
```
```
*/

19 changes: 14 additions & 5 deletions schemars/tests/arrayvec.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
mod util;
use arrayvec::{ArrayString, ArrayVec};
use util::*;

#[test]
fn arrayvec() -> TestResult {
test_default_generated_schema::<ArrayVec<[i32; 16]>>("arrayvec")
fn arrayvec05() -> TestResult {
test_default_generated_schema::<arrayvec05::ArrayVec<[i32; 16]>>("arrayvec")
}

#[test]
fn arrayvec_string() -> TestResult {
test_default_generated_schema::<ArrayString<[u8; 16]>>("arrayvec_string")
fn arrayvec05_string() -> TestResult {
test_default_generated_schema::<arrayvec05::ArrayString<[u8; 16]>>("arrayvec_string")
}

#[test]
fn arrayvec07() -> TestResult {
test_default_generated_schema::<arrayvec07::ArrayVec<i32, 16>>("arrayvec")
}

#[test]
fn arrayvec07_string() -> TestResult {
test_default_generated_schema::<arrayvec07::ArrayString<16>>("arrayvec_string")
}
3 changes: 2 additions & 1 deletion schemars/tests/chrono.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@ use chrono::prelude::*;
use schemars::JsonSchema;
use util::*;

#[derive(Debug, JsonSchema)]
#[allow(dead_code)]
#[derive(JsonSchema)]
struct ChronoTypes {
weekday: Weekday,
date_time: DateTime<Utc>,
5 changes: 3 additions & 2 deletions schemars/tests/crate_alias.rs
Original file line number Diff line number Diff line change
@@ -5,9 +5,10 @@ use util::*;
#[allow(unused_imports)]
use std as schemars;

#[derive(Debug, not_schemars::JsonSchema)]
#[allow(dead_code)]
#[derive(not_schemars::JsonSchema)]
#[schemars(crate = "not_schemars")]
pub struct Struct {
struct Struct {
/// This is a document
foo: i32,
bar: bool,
Loading