Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test-serde.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ jobs:
cargo test --no-default-features --features=serde
cargo test --no-default-features --features=serde,std
cargo test --no-default-features --features=schemars
cargo test --no-default-features --features=schemars1
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Added optional `bytecheck` support. Enable using the `bytecheck` feature.
- Added optional `schemars` v1 support. Enable using the `schemars1` feature.

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde = ["dep:serde"]
borsh = ["dep:borsh"]

schemars = ["dep:schemars", "std"]
schemars1 = ["dep:schemars1", "std"]

# Derive/implement traits for bytecheck
bytecheck = ["dep:bytecheck"]
Expand All @@ -53,6 +54,7 @@ defmt = { version = "1", optional = true }
serde = { version = "1.0", optional = true, default-features = false }
borsh = { version = "1.5.1", optional = true, features = ["unstable__schema"], default-features = false }
schemars = { version = "0.8.21", optional = true, features = ["derive"], default-features = false }
schemars1 = { package = "schemars", version = "1", optional = true, default-features = false }
bytemuck = { version = "1", optional = true, default-features = false }
bin-proto = { version = "0.12.2", optional = true, default-features = false }
bytecheck = { version = "0.8.2", optional = true, default-features = false }
Expand All @@ -62,3 +64,4 @@ quickcheck = { version = "1", optional = true, default-features = false }

[dev-dependencies]
serde_test = "1.0"
serde_json = "1.0"
39 changes: 39 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,45 @@ macro_rules! impl_schemars {
Schema::Object(schema_object)
}
}

#[cfg(feature = "schemars1")]
impl<T: BuiltinInteger + $trait, const BITS: usize> schemars1::JsonSchema for $type<T, BITS>
where
Self: Integer,
{
fn inline_schema() -> bool {
true
}

fn schema_name() -> alloc::borrow::Cow<'static, str> {
use alloc::string::ToString;
[$str_prefix, &BITS.to_string()].concat().into()
}

fn json_schema(_gen: &mut schemars1::SchemaGenerator) -> schemars1::Schema {
// The `schemars` crate only provides the `minimum` and `maximum` fields for "small"
// integers (i8/u8/i16/u16).
//
// The rationale for this choice is explained in this issue:
// https://github.com/GREsau/schemars/issues/298
//
// We mimic that behavior and only provide the `minimum` and `maximum` fields for
// integers smaller than 32 bits.
if BITS < 32 {
schemars1::json_schema!({
"type": "integer",
"format": Self::schema_name(),
"minimum": Self::MIN.as_i64(),
"maximum": Self::MAX.as_i64(),
})
} else {
schemars1::json_schema!({
"type": "integer",
"format": Self::schema_name(),
})
}
}
}
};
}

Expand Down
106 changes: 106 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4278,6 +4278,112 @@ fn schemars_signed() {
assert_eq!(i8, i9);
}

#[cfg(feature = "schemars1")]
#[test]
fn schemars1_unsigned() {
use schemars1::{generate::SchemaSettings, schema_for};
use serde_json::json;

let meta = SchemaSettings::default().meta_schema.unwrap();

assert_eq!(
schema_for!(u1).to_value(),
json!({
"$schema": meta,
"type": "integer",
"format": "uint1",
"title": "uint1",
"minimum": 0,
"maximum": 1,
})
);

assert_eq!(
schema_for!(u31).to_value(),
json!({
"$schema": meta,
"type": "integer",
"format": "uint31",
"title": "uint31",
"minimum": 0,
"maximum": 0x7fffffff,
})
);

assert_eq!(
schema_for!(u33).to_value(),
json!({
"$schema": meta,
"type": "integer",
"format": "uint33",
"title": "uint33"
})
);

assert_eq!(
schema_for!(u127).to_value(),
json!({
"$schema": meta,
"type": "integer",
"format": "uint127",
"title": "uint127"
})
);
}

#[cfg(feature = "schemars1")]
#[test]
fn schemars1_signed() {
use schemars1::{generate::SchemaSettings, schema_for};
use serde_json::json;

let meta = SchemaSettings::default().meta_schema.unwrap();

assert_eq!(
schema_for!(i1).to_value(),
json!({
"$schema": meta,
"type": "integer",
"format": "int1",
"title": "int1",
"minimum": -1,
"maximum": 0,
})
);

assert_eq!(
schema_for!(i31).to_value(),
json!({
"$schema": meta,
"type": "integer",
"format": "int31",
"title": "int31",
"minimum": -0x40000000,
"maximum": 0x3fffffff,
})
);

assert_eq!(
schema_for!(i33).to_value(),
json!({
"$schema": meta,
"type": "integer",
"format": "int33",
"title": "int33"
})
);

assert_eq!(
schema_for!(i127).to_value(),
json!({
"$schema": meta,
"type": "integer",
"format": "int127",
"title": "int127"
})
);
}

#[cfg(feature = "bytemuck")]
mod bytemuck {
use arbitrary_int::prelude::*;
Expand Down
Loading