Debian Codegraph @

tests/integration/schema_with.rs

raw symbols SCIP
1 use crate::prelude::*;
2 use schemars::generate::SchemaSettings;
3
4 #[derive(JsonSchema, Deserialize, Serialize, Default)]
5 struct Struct<T: Default + Serialize> {
6 #[serde(with = "int_as_str")]
7 #[schemars(schema_with = "int_as_str::json_schema")]
8 x: i64,
9 #[schemars(schema_with = "from_serialize_default::<T>")]
10 t: T,
11 }
12
13 mod int_as_str {
14 pub(super) fn serialize<S, T>(value: &T, ser: S) -> Result<S::Ok, S::Error>
15 where
16 S: serde::Serializer,
17 T: std::fmt::Display,
18 {
19 ser.collect_str(value)
20 }
21
22 pub(super) fn deserialize<'de, D, T>(deser: D) -> Result<T, D::Error>
23 where
24 D: serde::Deserializer<'de>,
25 T: std::str::FromStr<Err = std::num::ParseIntError>,
26 {
27 <&str as serde::Deserialize>::deserialize(deser)?
28 .parse()
29 .map_err(serde::de::Error::custom)
30 }
31
32 pub(super) fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
33 schemars::json_schema!({
34 "type": "string",
35 "pattern": r"^-?\d+$"
36 })
37 }
38 }
39
40 fn from_serialize_default<T: Default + Serialize>(
41 generator: &mut schemars::SchemaGenerator,
42 ) -> schemars::Schema {
43 generator
44 .settings()
45 .clone()
46 .into_generator()
47 .into_root_schema_for_value(&T::default())
48 .unwrap()
49 }
50
51 #[test]
52 fn field_schema_with() {
53 test!(Struct<String>)
54 .assert_snapshot()
55 .assert_allows_ser_roundtrip_default()
56 .assert_matches_de_roundtrip(arbitrary_values_except(
57 Value::is_array,
58 "structs with `#derive(Deserialize)` can technically be deserialized from sequences, but that's not intended to be used via JSON, so schemars ignores it",
59 ));
60 }
61
62 struct NotJsonSchema;
63
64 #[derive(JsonSchema)]
65 #[schemars(with = "Struct<String>")]
66 struct Struct2 {
67 _x: NotJsonSchema,
68 }
69
70 #[test]
71 fn container_with() {
72 test!(Struct2).assert_identical::<Struct<String>>();
73 }
74
75 #[derive(JsonSchema)]
76 #[schemars(with = "Struct<String>", description = "Testing...")]
77 struct Struct3 {
78 _x: NotJsonSchema,
79 }
80
81 #[test]
82 fn container_with_metadata() {
83 test!(Struct3).assert_snapshot().custom(|schema, _| {
84 assert_eq!(schema.get("description"), Some(&"Testing...".into()));
85 });
86
87 assert_ne!(<Struct2>::schema_id(), <Struct3>::schema_id());
88 }
89
90 #[derive(JsonSchema)]
91 #[schemars(schema_with = "int_as_str::json_schema")]
92 struct IntAsString {
93 _x: NotJsonSchema,
94 }
95
96 #[test]
97 fn container_schema_with() {
98 test!(IntAsString)
99 .assert_snapshot()
100 .custom(|schema, contract| {
101 let mut generator = SchemaSettings::default()
102 .with(|s| s.contract = contract)
103 .into_generator();
104 let expected = int_as_str::json_schema(&mut generator);
105
106 // `title` and `$schema` are added for root schemas - ignore for comparison purposes
107 let mut schema = schema.clone();
108 schema.remove("title");
109 schema.remove("$schema");
110
111 assert_eq!(schema, expected);
112 });
113 }