-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add schemars
to ZeroVec
#4792
base: main
Are you sure you want to change the base?
add schemars
to ZeroVec
#4792
Changes from all commits
9179c9b
4e06ff3
6472c50
dc97369
395401f
5350349
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ all-features = true | |
|
||
[dependencies] | ||
zerofrom = { workspace = true } | ||
serde_json = "1.0" | ||
|
||
zerovec-derive = { workspace = true, optional = true} | ||
|
||
|
@@ -37,6 +38,7 @@ serde = { version = "1.0", default-features = false, features = ["alloc"], optio | |
# and all 0.7 versions, but not further. | ||
yoke = { version = ">=0.6.0, <0.8.0", path = "../yoke", optional = true } | ||
twox-hash = { version = "1.6", default-features = false, optional = true } | ||
schemars = "0.8.16" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: this must be an optional dependency There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, and please add it to the top-level Cargo.toml (ideally wait until #4844 is merged) |
||
|
||
[dev-dependencies] | ||
bincode = "1.3" | ||
|
@@ -48,7 +50,6 @@ rand = "0.8" | |
rand_distr = "0.4" | ||
rand_pcg = "0.3" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
yoke = { path = "../../utils/yoke", features = ["derive"] } | ||
zerofrom = { path = "../../utils/zerofrom", features = ["derive"] } | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,19 @@ | |
use core::any; | ||
use core::fmt; | ||
|
||
use crate::__zerovec_internal_reexport::boxed::Box; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: just |
||
use alloc::borrow::ToOwned; | ||
use alloc::format; | ||
use alloc::string::String; | ||
use alloc::vec; | ||
use schemars::gen::SchemaGenerator; | ||
use schemars::schema::InstanceType; | ||
use schemars::schema::Schema; | ||
use schemars::schema::SchemaObject; | ||
use schemars::JsonSchema; | ||
|
||
use serde_json::Value; | ||
|
||
/// A generic error type to be used for decoding slices of ULE types | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
|
@@ -33,6 +46,27 @@ impl fmt::Display for ZeroVecError { | |
} | ||
} | ||
} | ||
impl JsonSchema for ZeroVecError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: I don't think you should need this impl. It should go away when you fix the ZeroSlice impl. |
||
fn schema_name() -> String { | ||
format!("ZeroVecError") | ||
} | ||
|
||
fn json_schema(_gen: &mut SchemaGenerator) -> Schema { | ||
Schema::Object(SchemaObject { | ||
instance_type: Some(InstanceType::String.into()), | ||
enum_values: Some(vec![ | ||
Value::String("InvalidLength".to_owned()), | ||
Value::String("ParseError".to_owned()), | ||
Value::String("VarZeroVecFormatError".to_owned()), | ||
]), | ||
metadata: Some(Box::new(schemars::schema::Metadata { | ||
description: Some("ZeroVecError is an enum representing errors that can occur during the decoding of slices of ULE".into()), | ||
..Default::default() | ||
})), | ||
..Default::default() | ||
}) | ||
} | ||
} | ||
|
||
impl ZeroVecError { | ||
/// Construct a parse error for the given type | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -523,6 +523,53 @@ pub use zerovec_derive::make_varule; | |
mod tests { | ||
use super::*; | ||
use core::mem::size_of; | ||
use schemars::{gen::SchemaGenerator, JsonSchema}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
|
||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[derive(JsonSchema)] | ||
pub struct DataStruct<'data> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: for in-file unit tests, make one test for each of these. For example, #[test]
fn test_schema_zerovec_u32() {
let gen = SchemaGenerator::default();
let schema = gen.into_root_schema_for::<ZeroVec<u32>>();
// ...
} As suggested below, use insta for the more complex tests. |
||
#[cfg_attr(feature = "serde", serde(borrow))] | ||
nums: ZeroVec<'data, u32>, | ||
|
||
#[cfg_attr(feature = "serde", serde(borrow))] | ||
chars: ZeroVec<'data, char>, | ||
|
||
#[cfg_attr(feature = "serde", serde(borrow))] | ||
strs: VarZeroVec<'data, str>, | ||
|
||
#[cfg_attr(feature = "serde", serde(borrow))] | ||
nested_numbers: VarZeroVec<'data, ZeroSlice<u32>>, | ||
} | ||
|
||
#[test] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: use insta to help generate test cases and check-in the JSON Schema output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, will look into it 👍 |
||
fn check_schema() { | ||
let gen = SchemaGenerator::default(); | ||
let schema = gen.into_root_schema_for::<DataStruct>(); | ||
let schema_json = | ||
serde_json::to_string_pretty(&schema).expect("Failed to serialize schema"); | ||
println!("{}", schema_json); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Please include the generated JSON. |
||
let parsed_schema: Value = | ||
serde_json::from_str(&schema_json).expect("Failed to parse schema JSON"); | ||
|
||
// Check for the existence of "ZeroVec<Character>" and "ZeroVec<uint32>" in `definitions` | ||
let definitions = parsed_schema | ||
.get("definitions") | ||
.expect("No definitions found in schema"); | ||
assert!( | ||
definitions.get("ZeroVec<Character>").is_some(), | ||
"Definition for ZeroVec<Character> not found" | ||
); | ||
assert!( | ||
definitions.get("ZeroVec<uint32>").is_some(), | ||
"Definition for ZeroVec<uint32> not found" | ||
); | ||
assert!( | ||
definitions.get("VarZeroVec<String>").is_some(), | ||
"Definition for VarZeroVec<String> not found" | ||
); | ||
} | ||
|
||
/// Checks that the size of the type is one of the given sizes. | ||
/// The size might differ across Rust versions or channels. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
||
use super::*; | ||
use crate::alloc::string::ToString; | ||
use ::serde::Serialize; | ||
use alloc::boxed::Box; | ||
use core::cmp::Ordering; | ||
use core::ops::Range; | ||
|
@@ -543,6 +545,41 @@ impl<T: AsULE + Ord> Ord for ZeroSlice<T> { | |
} | ||
} | ||
|
||
impl<T> JsonSchema for ZeroSlice<T> | ||
where | ||
T: AsULE + JsonSchema + Serialize, // Ensure T is serializable for accurate schema representation | ||
{ | ||
fn schema_name() -> String { | ||
format!("ZeroSlice<{}>", T::schema_name()) | ||
} | ||
|
||
fn schema_id() -> Cow<'static, str> { | ||
Cow::Owned(format!("zerovec::ZeroSlice<{}>", T::schema_id())) | ||
} | ||
|
||
fn json_schema(gen: &mut SchemaGenerator) -> Schema { | ||
// Instead of generating a subschema for T, we generate a schema representing the byte array | ||
let byte_schema = gen.subschema_for::<Vec<u8>>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: why? ZeroSlice is logically an array of items There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, ZeroVec and ZeroSlice should both become arrays of T. Maybe they can resolve to the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, expected interaction should be as array not bytes. Lets fallback to implementing as arrays of T. |
||
|
||
SchemaObject { | ||
instance_type: Some(InstanceType::Object.into()), | ||
object: Some(Box::new({ | ||
let mut object_validation = schemars::schema::ObjectValidation::default(); | ||
object_validation | ||
.properties | ||
.insert("data".to_string(), byte_schema); | ||
object_validation | ||
.properties | ||
.insert("error".to_string(), gen.subschema_for::<ZeroVecError>()); | ||
object_validation.required.insert("data".to_string()); | ||
object_validation | ||
})), | ||
..Default::default() | ||
} | ||
.into() | ||
} | ||
} | ||
Comment on lines
+560
to
+581
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ maintainers I am not certain if this is the right way to move forward |
||
|
||
impl<T: AsULE> AsRef<ZeroSlice<T>> for Vec<T::ULE> { | ||
fn as_ref(&self) -> &ZeroSlice<T> { | ||
ZeroSlice::<T>::from_ule_slice(self) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue: Use the
{ workspace = true }
version of the dependency.