From 3b3a6aefe0ae00d46c3444b326baed066bbd9e69 Mon Sep 17 00:00:00 2001 From: matt rice Date: Mon, 30 Oct 2023 12:32:55 -0700 Subject: [PATCH 1/3] add feature for serde_bytes::ByteBuf --- Cargo.lock | 10 ++++++++++ schemars/Cargo.toml | 7 +++++++ schemars/src/json_schema_impls/mod.rs | 2 ++ schemars/src/json_schema_impls/serde_bytes.rs | 11 +++++++++++ schemars/tests/serde_bytes.rs | 8 ++++++++ 5 files changed, 38 insertions(+) create mode 100644 schemars/src/json_schema_impls/serde_bytes.rs create mode 100644 schemars/tests/serde_bytes.rs diff --git a/Cargo.lock b/Cargo.lock index 0f076208..43608fe6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -789,6 +789,7 @@ dependencies = [ "schemars_derive", "semver", "serde", + "serde_bytes", "serde_json", "serde_repr", "smallvec", @@ -835,6 +836,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "387cc504cb06bb40a96c8e04e951fe01854cf6bc921053c954e4a606d9675c6a" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.210" diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index 384f3eae..744d90aa 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -32,6 +32,7 @@ smallvec1 = { version = "1.0", default-features = false, optional = true, packag smol_str02 = { version = "0.2.1", default-features = false, optional = true, package = "smol_str" } url2 = { version = "2.0", default-features = false, optional = true, package = "url" } uuid1 = { version = "1.0", default-features = false, optional = true, package = "uuid" } +serde_bytes = { version = "0.11.12", optional = true } [dev-dependencies] pretty_assertions = "1.2.1" @@ -76,6 +77,8 @@ preserve_order = ["serde_json/preserve_order"] # Implements `JsonSchema` on `serde_json::value::RawValue` raw_value = ["serde_json/raw_value"] +serde_bytes = ["dep:serde_bytes"] + # For internal/CI use only _ui_test = [] @@ -83,6 +86,10 @@ _ui_test = [] name = "ui" required-features = ["_ui_test"] +[[test]] +name = "serde_bytes" +required-features = ["serde_bytes"] + [package.metadata.docs.rs] all-features = true rustdoc-args = ["--extend-css", "docs-rs-custom.css"] diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index cbe1a075..2018117b 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -45,6 +45,8 @@ mod maps; mod nonzero; mod primitives; mod sequences; +#[cfg(feature = "serde_bytes")] +mod serde_bytes; mod serdejson; mod std_time; mod tuple; diff --git a/schemars/src/json_schema_impls/serde_bytes.rs b/schemars/src/json_schema_impls/serde_bytes.rs new file mode 100644 index 00000000..9bbe96fb --- /dev/null +++ b/schemars/src/json_schema_impls/serde_bytes.rs @@ -0,0 +1,11 @@ +use crate::gen::SchemaGenerator; +use crate::schema::*; +use crate::JsonSchema; +use serde_bytes::ByteBuf; + +forward_impl!((JsonSchema for ByteBuf) => Vec); +// Because Bytes is a wrapper around [u8] which is not `Sized` +// I couldn't get it through the testsuite to check if this actually works. +// +// use serde_bytes::Bytes; +// forward_impl!((JsonSchema for Bytes) => Vec); diff --git a/schemars/tests/serde_bytes.rs b/schemars/tests/serde_bytes.rs new file mode 100644 index 00000000..44703bff --- /dev/null +++ b/schemars/tests/serde_bytes.rs @@ -0,0 +1,8 @@ +mod util; +use serde_bytes::ByteBuf; +use util::*; + +#[test] +fn bytes() -> TestResult { + test_default_generated_schema::<(ByteBuf, ByteBuf)>("bytes") +} From a9d3f65ad8ac89f9dd431d7bd4155ae33eaa3505 Mon Sep 17 00:00:00 2001 From: matt rice Date: Thu, 7 Nov 2024 14:24:03 -0800 Subject: [PATCH 2/3] Use alloc crate --- schemars/src/json_schema_impls/serde_bytes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemars/src/json_schema_impls/serde_bytes.rs b/schemars/src/json_schema_impls/serde_bytes.rs index 9bbe96fb..715b4edc 100644 --- a/schemars/src/json_schema_impls/serde_bytes.rs +++ b/schemars/src/json_schema_impls/serde_bytes.rs @@ -3,7 +3,7 @@ use crate::schema::*; use crate::JsonSchema; use serde_bytes::ByteBuf; -forward_impl!((JsonSchema for ByteBuf) => Vec); +forward_impl!((JsonSchema for ByteBuf) => alloc::vec::Vec); // Because Bytes is a wrapper around [u8] which is not `Sized` // I couldn't get it through the testsuite to check if this actually works. // From 6e22cfb0db5dff5c8f52a712a8cb3a2ef6fa6ba2 Mon Sep 17 00:00:00 2001 From: matt rice Date: Thu, 7 Nov 2024 14:27:20 -0800 Subject: [PATCH 3/3] try and update imports --- schemars/src/json_schema_impls/serde_bytes.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/schemars/src/json_schema_impls/serde_bytes.rs b/schemars/src/json_schema_impls/serde_bytes.rs index 715b4edc..59d68fae 100644 --- a/schemars/src/json_schema_impls/serde_bytes.rs +++ b/schemars/src/json_schema_impls/serde_bytes.rs @@ -1,5 +1,3 @@ -use crate::gen::SchemaGenerator; -use crate::schema::*; use crate::JsonSchema; use serde_bytes::ByteBuf;