Skip to content

Commit 18826cd

Browse files
committed
Add test for changes from #452
1 parent 2ac8ff5 commit 18826cd

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

schemars/tests/integration/map.rs

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

8+
#[derive(JsonSchema, Deserialize, Serialize, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9+
struct IndirectU32(u32);
10+
11+
#[derive(JsonSchema, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
12+
enum Enum {
13+
Unit1,
14+
Unit2,
15+
#[serde(untagged)]
16+
UntaggedI8(i8),
17+
#[serde(untagged)]
18+
UntaggedIndirectU32(IndirectU32),
19+
}
20+
821
#[derive(JsonSchema, Deserialize, Serialize, Default)]
922
struct Maps {
1023
s_map: HashMap<String, bool>,
1124
i_map: BTreeMap<i8, bool>,
1225
u_map: HashMap<u64, bool>,
1326
pattern_map: BTreeMap<HexNumber, bool>,
27+
enum_map: HashMap<Enum, bool>,
1428
}
1529

1630
#[test]
@@ -22,6 +36,25 @@ fn maps() {
2236
i_map: BTreeMap::from_iter([(-123, true), (123, true)]),
2337
u_map: HashMap::from_iter([(123, true)]),
2438
pattern_map: BTreeMap::from_iter([(HexNumber("b4df00d".to_owned()), true)]),
39+
enum_map: HashMap::from_iter([(Enum::Unit1, true)]),
2540
}])
41+
.assert_allows_ser_only(
42+
// serde allow serializing untagged non-string newtype variants, but not deserializing them.
43+
[
44+
Maps {
45+
enum_map: HashMap::from_iter([(Enum::UntaggedI8(-100), true)]),
46+
..Default::default()
47+
},
48+
Maps {
49+
// It's unfortunate that this adds an unused `IndirectU32` to the schema's `$defs`,
50+
// but it's ultimately harmless.
51+
enum_map: HashMap::from_iter([(
52+
Enum::UntaggedIndirectU32(IndirectU32(1000000)),
53+
true,
54+
)]),
55+
..Default::default()
56+
},
57+
],
58+
)
2659
.assert_matches_de_roundtrip(arbitrary_values());
2760
}

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,61 @@
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"
45-
]
61+
"pattern_map",
62+
"enum_map"
63+
],
64+
"$defs": {
65+
"IndirectU32": {
66+
"type": "integer",
67+
"format": "uint32",
68+
"minimum": 0
69+
}
70+
}
4671
}

0 commit comments

Comments
 (0)