Skip to content

Commit c08551a

Browse files
committed
primitive-types: Fix json schema
Apparently the latest version broke it and thus, CI is failing for other prs.
1 parent f0dade1 commit c08551a

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

primitive-types/src/json_schema.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
use super::*;
22
#[cfg(not(feature = "std"))]
33
use alloc::{
4-
borrow::ToOwned,
4+
borrow::{Cow, ToOwned},
55
string::{String, ToString},
66
};
7+
#[cfg(feature = "std")]
8+
use std::borrow::Cow;
79

8-
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
10+
use schemars::{json_schema, JsonSchema, Schema, SchemaGenerator};
911

1012
impl JsonSchema for H160 {
11-
fn schema_name() -> String {
12-
"HexEncoded20Bytes".to_owned()
13+
fn schema_name() -> Cow<'static, str> {
14+
"HexEncoded20Bytes".into()
1315
}
1416

15-
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
16-
let mut schema = gen.subschema_for::<String>().into_object();
17-
schema.metadata().description = Some("Hex encoded 20 bytes".to_string());
18-
schema.string().pattern = Some("^0(x|X)[a-fA-F0-9]{40}$".to_string());
19-
schema.into()
17+
fn json_schema(_: &mut SchemaGenerator) -> Schema {
18+
json_schema!({
19+
"description": "Hex encoded 20 bytes",
20+
"pattern": "^0(x|X)[a-fA-F0-9]{40}$",
21+
})
2022
}
2123
}
2224

2325
impl JsonSchema for U256 {
24-
fn schema_name() -> String {
25-
"U256String".to_string()
26+
fn schema_name() -> Cow<'static, str> {
27+
"U256String".into()
2628
}
2729

28-
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
29-
let mut schema = gen.subschema_for::<String>().into_object();
30-
schema.metadata().description = Some("256-bit Unsigned Integer".to_string());
31-
schema.string().pattern = Some("^(0|[1-9][0-9]{0,77})$".to_string());
32-
schema.into()
30+
fn json_schema(_: &mut SchemaGenerator) -> Schema {
31+
json_schema!({
32+
"description": "256-bit Unsigned Integer",
33+
"pattern": "^(0|[1-9][0-9]{0,77})$",
34+
})
3335
}
3436
}
3537

@@ -44,14 +46,14 @@ mod tests {
4446

4547
#[test]
4648
fn hex_encoded_20_bytes() {
47-
let schema = H160::json_schema(&mut schemars::gen::SchemaGenerator::default());
49+
let schema = H160::json_schema(&mut schemars::SchemaGenerator::default());
4850
let schema_json = serde_json::to_value(&schema).unwrap();
4951
let schema = jsonschema::Validator::options()
5052
.with_draft(Draft::Draft7)
5153
.build(&schema_json)
5254
.unwrap();
5355
let value = serde_json::to_value("0x55086adeca661185c437d92b9818e6eda6d0d047").unwrap();
54-
assert!(schema.validate(&value).is_ok());
56+
schema.validate(&value).map_err(|e| e.into_iter().map(|e| e.to_string()).collect::<Vec<_>>()).unwrap();
5557
let value = serde_json::to_value("0X0E9C8DA9FD4BDD3281879D9E328D8D74D02558CC").unwrap();
5658
assert!(schema.validate(&value).is_ok());
5759

@@ -61,7 +63,7 @@ mod tests {
6163

6264
#[test]
6365
fn u256() {
64-
let schema = U256::json_schema(&mut schemars::gen::SchemaGenerator::default());
66+
let schema = U256::json_schema(&mut schemars::SchemaGenerator::default());
6567
let schema_json = serde_json::to_value(&schema).unwrap();
6668
let schema = jsonschema::Validator::options()
6769
.with_draft(Draft::Draft7)

0 commit comments

Comments
 (0)