Skip to content

Commit 32cff93

Browse files
committed
Add test
1 parent ab4ba87 commit 32cff93

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

Cargo.lock

Lines changed: 24 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemars/tests/integration/map.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@ use std::collections::{BTreeMap, HashMap};
55
#[schemars(extend("pattern" = "^[0-9a-f]*$"))]
66
struct HexNumber(String);
77

8+
#[derive(JsonSchema, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
9+
enum Enum {
10+
Unit1,
11+
Unit2,
12+
#[serde(untagged)]
13+
UntaggedI8(i8),
14+
#[serde(untagged)]
15+
UntaggedU32(u32),
16+
}
17+
818
#[derive(JsonSchema, Deserialize, Serialize, Default)]
919
struct Maps {
1020
s_map: HashMap<String, bool>,
1121
i_map: BTreeMap<i8, bool>,
1222
u_map: HashMap<u64, bool>,
1323
pattern_map: BTreeMap<HexNumber, bool>,
24+
enum_map: HashMap<Enum, bool>,
1425
}
1526

1627
#[test]
@@ -22,6 +33,20 @@ fn maps() {
2233
i_map: BTreeMap::from_iter([(-123, true), (123, true)]),
2334
u_map: HashMap::from_iter([(123, true)]),
2435
pattern_map: BTreeMap::from_iter([(HexNumber("b4df00d".to_owned()), true)]),
36+
enum_map: HashMap::from_iter([(Enum::Unit1, true)]),
2537
}])
38+
.assert_allows_ser_only(
39+
// serde allow serializing untagged non-string newtype variants, but not deserializing them.
40+
[
41+
Maps {
42+
enum_map: HashMap::from_iter([(Enum::UntaggedI8(-100), true)]),
43+
..Default::default()
44+
},
45+
Maps {
46+
enum_map: HashMap::from_iter([(Enum::UntaggedU32(1000000), true)]),
47+
..Default::default()
48+
},
49+
],
50+
)
2651
.assert_matches_de_roundtrip(arbitrary_values());
2752
}

schemars/tests/integration/snapshots/schemars/tests/integration/map.rs~maps.json

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,54 @@
1111
},
1212
"i_map": {
1313
"type": "object",
14+
"additionalProperties": false,
1415
"patternProperties": {
1516
"^-?\\d+$": {
1617
"type": "boolean"
1718
}
18-
},
19-
"additionalProperties": false
19+
}
2020
},
2121
"u_map": {
2222
"type": "object",
23+
"additionalProperties": false,
2324
"patternProperties": {
2425
"^\\d+$": {
2526
"type": "boolean"
2627
}
27-
},
28-
"additionalProperties": false
28+
}
2929
},
3030
"pattern_map": {
3131
"type": "object",
32+
"additionalProperties": false,
3233
"patternProperties": {
3334
"^[0-9a-f]*$": {
3435
"type": "boolean"
3536
}
37+
}
38+
},
39+
"enum_map": {
40+
"type": "object",
41+
"properties": {
42+
"Unit1": {
43+
"type": "boolean"
44+
},
45+
"Unit2": {
46+
"type": "boolean"
47+
}
3648
},
37-
"additionalProperties": false
49+
"additionalProperties": false,
50+
"patternProperties": {
51+
"^-?\\d+$": {
52+
"type": "boolean"
53+
}
54+
}
3855
}
3956
},
4057
"required": [
4158
"s_map",
4259
"i_map",
4360
"u_map",
44-
"pattern_map"
61+
"pattern_map",
62+
"enum_map"
4563
]
4664
}

0 commit comments

Comments
 (0)