File tree Expand file tree Collapse file tree 9 files changed +138
-2
lines changed Expand file tree Collapse file tree 9 files changed +138
-2
lines changed Original file line number Diff line number Diff line change @@ -404,6 +404,35 @@ impl<T> JsonSchemaAs<T> for DisplayFromStr {
404
404
forward_schema ! ( String ) ;
405
405
}
406
406
407
+ #[ cfg( feature = "hex" ) ]
408
+ impl < T , F : formats:: Format > JsonSchemaAs < T > for hex:: Hex < F > {
409
+ fn schema_name ( ) -> String {
410
+ "Hex<F>" . into ( )
411
+ }
412
+
413
+ fn schema_id ( ) -> Cow < ' static , str > {
414
+ "serde_with::hex::Hex<F>" . into ( )
415
+ }
416
+
417
+ fn json_schema ( _: & mut SchemaGenerator ) -> Schema {
418
+ use :: schemars_0_8:: schema:: StringValidation ;
419
+
420
+ SchemaObject {
421
+ instance_type : Some ( InstanceType :: String . into ( ) ) ,
422
+ string : Some ( Box :: new ( StringValidation {
423
+ pattern : Some ( r"^(?:[0-9A-Fa-f]{2})*$" . to_owned ( ) ) ,
424
+ ..Default :: default ( )
425
+ } ) ) ,
426
+ ..Default :: default ( )
427
+ }
428
+ . into ( )
429
+ }
430
+
431
+ fn is_referenceable ( ) -> bool {
432
+ false
433
+ }
434
+ }
435
+
407
436
impl JsonSchemaAs < bool > for BoolFromInt < Strict > {
408
437
fn schema_name ( ) -> String {
409
438
"BoolFromInt<Strict>" . into ( )
Original file line number Diff line number Diff line change @@ -406,6 +406,28 @@ impl<T> JsonSchemaAs<T> for DisplayFromStr {
406
406
forward_schema ! ( String ) ;
407
407
}
408
408
409
+ #[ cfg( feature = "hex" ) ]
410
+ impl < T , F : formats:: Format > JsonSchemaAs < T > for hex:: Hex < F > {
411
+ fn schema_name ( ) -> Cow < ' static , str > {
412
+ "Hex<F>" . into ( )
413
+ }
414
+
415
+ fn schema_id ( ) -> Cow < ' static , str > {
416
+ "serde_with::hex::Hex<F>" . into ( )
417
+ }
418
+
419
+ fn json_schema ( _: & mut SchemaGenerator ) -> Schema {
420
+ json_schema ! ( {
421
+ "type" : "string" ,
422
+ "pattern" : r"^(?:[0-9A-Fa-f]{2})*$" ,
423
+ } )
424
+ }
425
+
426
+ fn inline_schema ( ) -> bool {
427
+ true
428
+ }
429
+ }
430
+
409
431
impl JsonSchemaAs < bool > for BoolFromInt < Strict > {
410
432
fn schema_name ( ) -> Cow < ' static , str > {
411
433
"BoolFromInt<Strict>" . into ( )
Original file line number Diff line number Diff line change @@ -408,6 +408,28 @@ impl<T> JsonSchemaAs<T> for DisplayFromStr {
408
408
forward_schema ! ( String ) ;
409
409
}
410
410
411
+ #[ cfg( feature = "hex" ) ]
412
+ impl < T , F : formats:: Format > JsonSchemaAs < T > for hex:: Hex < F > {
413
+ fn schema_name ( ) -> Cow < ' static , str > {
414
+ "Hex<F>" . into ( )
415
+ }
416
+
417
+ fn schema_id ( ) -> Cow < ' static , str > {
418
+ "serde_with::hex::Hex<F>" . into ( )
419
+ }
420
+
421
+ fn json_schema ( _: & mut SchemaGenerator ) -> Schema {
422
+ json_schema ! ( {
423
+ "type" : "string" ,
424
+ "pattern" : r"^(?:[0-9A-Fa-f]{2})*$" ,
425
+ } )
426
+ }
427
+
428
+ fn inline_schema ( ) -> bool {
429
+ true
430
+ }
431
+ }
432
+
411
433
impl JsonSchemaAs < bool > for BoolFromInt < Strict > {
412
434
fn schema_name ( ) -> Cow < ' static , str > {
413
435
"BoolFromInt<Strict>" . into ( )
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ use expect_test::expect_file;
5
5
use schemars:: JsonSchema ;
6
6
use serde:: Serialize ;
7
7
use serde_json:: json;
8
+ use serde_with:: hex:: * ;
8
9
use serde_with:: * ;
9
10
use std:: collections:: BTreeSet ;
10
11
@@ -90,6 +91,14 @@ fn schemars_basic() {
90
91
/// Same thing, but with a Vec this time.
91
92
#[ serde_as( as = "Vec<_>" ) ]
92
93
vec_same : Vec < u32 > ,
94
+
95
+ /// A vector of bytes that's serialized as a lowercase hex string.
96
+ #[ serde_as( as = "Hex" ) ]
97
+ lowercase_hex : Vec < u8 > ,
98
+
99
+ /// A vector of bytes that's serialized as an uppercase hex string.
100
+ #[ serde_as( as = "Hex<formats::Uppercase>" ) ]
101
+ uppercase_hex : Vec < u8 > ,
93
102
}
94
103
95
104
let schema = schemars:: schema_for!( Basic ) ;
Original file line number Diff line number Diff line change 6
6
" bare_field" ,
7
7
" box_same" ,
8
8
" display_from_str" ,
9
+ " lowercase_hex" ,
9
10
" same" ,
11
+ " uppercase_hex" ,
10
12
" vec_same"
11
13
],
12
14
"properties" : {
26
28
"description" : " Field that directly uses `DisplayFromStr`" ,
27
29
"type" : " string"
28
30
},
31
+ "lowercase_hex" : {
32
+ "description" : " A vector of bytes that's serialized as a lowercase hex string." ,
33
+ "type" : " string" ,
34
+ "pattern" : " ^(?:[0-9A-Fa-f]{2})*$"
35
+ },
29
36
"same" : {
30
37
"description" : " Same does not implement `JsonSchema` directly so this checks that the correct schemars attribute was injected." ,
31
38
"type" : " integer" ,
32
39
"format" : " uint32" ,
33
40
"minimum" : 0.0
34
41
},
42
+ "uppercase_hex" : {
43
+ "description" : " A vector of bytes that's serialized as an uppercase hex string." ,
44
+ "type" : " string" ,
45
+ "pattern" : " ^(?:[0-9A-Fa-f]{2})*$"
46
+ },
35
47
"vec_same" : {
36
48
"description" : " Same thing, but with a Vec this time." ,
37
49
"type" : " array" ,
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ use expect_test::expect_file;
6
6
use schemars:: JsonSchema ;
7
7
use serde:: Serialize ;
8
8
use serde_json:: json;
9
+ use serde_with:: hex:: * ;
9
10
use serde_with:: * ;
10
11
use std:: collections:: BTreeSet ;
11
12
@@ -91,6 +92,14 @@ fn schemars_basic() {
91
92
/// Same thing, but with a Vec this time.
92
93
#[ serde_as( as = "Vec<_>" ) ]
93
94
vec_same : Vec < u32 > ,
95
+
96
+ /// A vector of bytes that's serialized as a lowercase hex string.
97
+ #[ serde_as( as = "Hex" ) ]
98
+ lowercase_hex : Vec < u8 > ,
99
+
100
+ /// A vector of bytes that's serialized as an uppercase hex string.
101
+ #[ serde_as( as = "Hex<formats::Uppercase>" ) ]
102
+ uppercase_hex : Vec < u8 > ,
94
103
}
95
104
96
105
let schema = schemars:: schema_for!( Basic ) ;
Original file line number Diff line number Diff line change 33
33
"format" : " uint32" ,
34
34
"minimum" : 0
35
35
}
36
+ },
37
+ "lowercase_hex" : {
38
+ "description" : " A vector of bytes that's serialized as a lowercase hex string." ,
39
+ "type" : " string" ,
40
+ "pattern" : " ^(?:[0-9A-Fa-f]{2})*$"
41
+ },
42
+ "uppercase_hex" : {
43
+ "description" : " A vector of bytes that's serialized as an uppercase hex string." ,
44
+ "type" : " string" ,
45
+ "pattern" : " ^(?:[0-9A-Fa-f]{2})*$"
36
46
}
37
47
},
38
48
"required" : [
39
49
" bare_field" ,
40
50
" display_from_str" ,
41
51
" same" ,
42
52
" box_same" ,
43
- " vec_same"
53
+ " vec_same" ,
54
+ " lowercase_hex" ,
55
+ " uppercase_hex"
44
56
]
45
57
}
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ use expect_test::expect_file;
6
6
use schemars:: JsonSchema ;
7
7
use serde:: Serialize ;
8
8
use serde_json:: json;
9
+ use serde_with:: hex:: * ;
9
10
use serde_with:: * ;
10
11
use std:: collections:: BTreeSet ;
11
12
@@ -91,6 +92,14 @@ fn schemars_basic() {
91
92
/// Same thing, but with a Vec this time.
92
93
#[ serde_as( as = "Vec<_>" ) ]
93
94
vec_same : Vec < u32 > ,
95
+
96
+ /// A vector of bytes that's serialized as a lowercase hex string.
97
+ #[ serde_as( as = "Hex" ) ]
98
+ lowercase_hex : Vec < u8 > ,
99
+
100
+ /// A vector of bytes that's serialized as an uppercase hex string.
101
+ #[ serde_as( as = "Hex<formats::Uppercase>" ) ]
102
+ uppercase_hex : Vec < u8 > ,
94
103
}
95
104
96
105
let schema = schemars:: schema_for!( Basic ) ;
Original file line number Diff line number Diff line change 33
33
"format" : " uint32" ,
34
34
"minimum" : 0
35
35
}
36
+ },
37
+ "lowercase_hex" : {
38
+ "description" : " A vector of bytes that's serialized as a lowercase hex string." ,
39
+ "type" : " string" ,
40
+ "pattern" : " ^(?:[0-9A-Fa-f]{2})*$"
41
+ },
42
+ "uppercase_hex" : {
43
+ "description" : " A vector of bytes that's serialized as an uppercase hex string." ,
44
+ "type" : " string" ,
45
+ "pattern" : " ^(?:[0-9A-Fa-f]{2})*$"
36
46
}
37
47
},
38
48
"required" : [
39
49
" bare_field" ,
40
50
" display_from_str" ,
41
51
" same" ,
42
52
" box_same" ,
43
- " vec_same"
53
+ " vec_same" ,
54
+ " lowercase_hex" ,
55
+ " uppercase_hex"
44
56
]
45
57
}
You can’t perform that action at this time.
0 commit comments