Debian Codegraph @

tests/integration/enums_deny_unknown_fields.rs

raw symbols SCIP
1 use crate::prelude::*;
2 use std::collections::BTreeMap;
3
4 macro_rules! fn_values {
5 () => {
6 fn values() -> impl IntoIterator<Item = Self> {
7 [
8 Self::Unit,
9 Self::StringMap(
10 [("hello".to_owned(), "world".to_owned())]
11 .into_iter()
12 .collect(),
13 ),
14 Self::StructNewType(Struct {
15 foo: 123,
16 bar: true,
17 }),
18 Self::StructDenyUnknownFieldsNewType(StructDenyUnknownFields {
19 baz: 123,
20 foobar: true,
21 }),
22 Self::Struct {
23 foo: 123,
24 bar: true,
25 },
26 ]
27 }
28 };
29 }
30
31 #[derive(JsonSchema, Deserialize, Serialize, Default)]
32 struct Struct {
33 foo: i32,
34 bar: bool,
35 }
36
37 #[derive(JsonSchema, Deserialize, Serialize, Default)]
38 #[serde(deny_unknown_fields)]
39 struct StructDenyUnknownFields {
40 baz: i32,
41 foobar: bool,
42 }
43
44 #[derive(JsonSchema, Deserialize, Serialize)]
45 #[serde(deny_unknown_fields)]
46 enum External {
47 Unit,
48 StringMap(BTreeMap<String, String>),
49 StructNewType(Struct),
50 StructDenyUnknownFieldsNewType(StructDenyUnknownFields),
51 Struct { foo: i32, bar: bool },
52 }
53
54 impl External {
55 fn_values!();
56 }
57
58 #[derive(JsonSchema, Deserialize, Serialize)]
59 #[serde(tag = "tag", deny_unknown_fields)]
60 enum Internal {
61 Unit,
62 StringMap(BTreeMap<String, String>),
63 StructNewType(Struct),
64 StructDenyUnknownFieldsNewType(StructDenyUnknownFields),
65 Struct { foo: i32, bar: bool },
66 }
67
68 impl Internal {
69 fn_values!();
70 }
71
72 #[derive(JsonSchema, Deserialize, Serialize)]
73 #[serde(tag = "tag", content = "content", deny_unknown_fields)]
74 enum Adjacent {
75 Unit,
76 StringMap(BTreeMap<String, String>),
77 StructNewType(Struct),
78 StructDenyUnknownFieldsNewType(StructDenyUnknownFields),
79 Struct { foo: i32, bar: bool },
80 }
81
82 impl Adjacent {
83 fn_values!();
84 }
85
86 #[derive(JsonSchema, Deserialize, Serialize)]
87 #[serde(untagged, deny_unknown_fields)]
88 enum Untagged {
89 Unit,
90 StringMap(BTreeMap<String, String>),
91 StructNewType(Struct),
92 StructDenyUnknownFieldsNewType(StructDenyUnknownFields),
93 Struct { foo: i32, bar: bool },
94 }
95
96 impl Untagged {
97 fn_values!();
98 }
99
100 #[test]
101 fn externally_tagged_enum() {
102 test!(External)
103 .assert_snapshot()
104 .assert_allows_ser_roundtrip(External::values())
105 .assert_matches_de_roundtrip(arbitrary_values())
106 .assert_rejects_de([
107 json!({
108 "Struct": {
109 "foo": 123,
110 "bar": true,
111 "extra": null
112 }
113 }),
114 json!({
115 "StructDenyUnknownFieldsNewType": {
116 "baz": 123,
117 "foobar": true,
118 "extra": null
119 }
120 }),
121 ])
122 .assert_allows_de_roundtrip([json!({
123 "StructNewType": {
124 "foo": 123,
125 "bar": true,
126 "extra": null
127 }
128 })]);
129 }
130
131 #[test]
132 fn internally_tagged_enum() {
133 test!(Internal)
134 .assert_snapshot()
135 .assert_allows_ser_roundtrip(Internal::values())
136 .assert_matches_de_roundtrip(arbitrary_values())
137 .assert_rejects_de([
138 json!({
139 "tag": "Struct",
140 "foo": 123,
141 "bar": true,
142 "extra": null
143 }),
144 json!({
145 "tag": "StructDenyUnknownFieldsNewType",
146 "baz": 123,
147 "foobar": true,
148 "extra": null
149 }),
150 ])
151 .assert_allows_de_roundtrip([json!({
152 "tag": "StructNewType",
153 "foo": 123,
154 "bar": true,
155 "extra": null
156 })]);
157 }
158
159 #[test]
160 fn adjacently_tagged_enum() {
161 test!(Adjacent)
162 .assert_snapshot()
163 .assert_allows_ser_roundtrip(Adjacent::values())
164 .assert_matches_de_roundtrip(arbitrary_values())
165 .assert_rejects_de([
166 json!({
167 "tag": "Struct",
168 "content": {
169 "foo": 123,
170 "bar": true,
171 "extra": null
172 }
173 }),
174 json!({
175 "tag": "StructDenyUnknownFieldsNewType",
176 "content": {
177 "baz": 123,
178 "foobar": true,
179 "extra": null
180 }
181 }),
182 ])
183 .assert_allows_de_roundtrip([json!({
184 "tag": "StructNewType",
185 "content": {
186 "foo": 123,
187 "bar": true,
188 "extra": null
189 }
190 })]);
191 }
192
193 #[test]
194 fn untagged_enum() {
195 test!(Untagged)
196 .assert_snapshot()
197 .assert_allows_ser_roundtrip(Untagged::values())
198 .assert_matches_de_roundtrip(arbitrary_values())
199 .assert_rejects_de([json!({
200 "baz": 123,
201 "foobar": true,
202 "extra": null
203 })])
204 .assert_allows_de_roundtrip([json!({
205 "foo": 123,
206 "bar": true,
207 "extra": null
208 })]);
209 }