openapiv3_1 0.1.3

OpenAPI 3.1.x bindings for rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
//! Implements [OpenAPI Schema Object][schema] types which can be
//! used to define field properties, enum values, array or object types.
//!
//! [schema]: https://spec.openapis.org/oas/latest.html#schema-object
use indexmap::IndexMap;
use ordered_float::OrderedFloat;
use serde_derive::{Deserialize, Serialize};

use super::extensions::Extensions;
use super::security::SecurityScheme;
use super::{RefOr, Response};

/// Create an _`empty`_ [`Schema`] that serializes to _`null`_.
///
/// Can be used in places where an item can be serialized as `null`. This is used with unit type
/// enum variants and tuple unit types.
pub fn empty() -> Schema {
    Schema::object(Object::builder().default(serde_json::Value::Null).build())
}

/// Implements [OpenAPI Components Object][components] which holds supported
/// reusable objects.
///
/// Components can hold either reusable types themselves or references to other reusable
/// types.
///
/// [components]: https://spec.openapis.org/oas/latest.html#components-object
#[non_exhaustive]
#[derive(Serialize, Deserialize, Default, Clone, PartialEq, bon::Builder)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[serde(rename_all = "camelCase")]
#[builder(on(_, into))]
pub struct Components {
    /// Map of reusable [OpenAPI Schema Object][schema]s.
    ///
    /// [schema]: https://spec.openapis.org/oas/latest.html#schema-object
    #[serde(skip_serializing_if = "IndexMap::is_empty", default)]
    #[builder(field)]
    pub schemas: IndexMap<String, Schema>,

    /// Map of reusable response name, to [OpenAPI Response Object][response]s or [OpenAPI
    /// Reference][reference]s to [OpenAPI Response Object][response]s.
    ///
    /// [response]: https://spec.openapis.org/oas/latest.html#response-object
    /// [reference]: https://spec.openapis.org/oas/latest.html#reference-object
    #[serde(skip_serializing_if = "IndexMap::is_empty", default)]
    #[builder(field)]
    pub responses: IndexMap<String, RefOr<Response>>,

    /// Map of reusable [OpenAPI Security Scheme Object][security_scheme]s.
    ///
    /// [security_scheme]: https://spec.openapis.org/oas/latest.html#security-scheme-object
    #[serde(skip_serializing_if = "IndexMap::is_empty", default)]
    #[builder(field)]
    pub security_schemes: IndexMap<String, SecurityScheme>,

    /// Optional extensions "x-something".
    #[serde(skip_serializing_if = "Option::is_none", default, flatten)]
    pub extensions: Option<Extensions>,
}

impl Components {
    /// Construct a new [`Components`].
    pub fn new() -> Self {
        Self { ..Default::default() }
    }

    /// Add [`SecurityScheme`] to [`Components`].
    ///
    /// Accepts two arguments where first is the name of the [`SecurityScheme`]. This is later when
    /// referenced by [`SecurityRequirement`][requirement]s. Second parameter is the [`SecurityScheme`].
    ///
    /// [requirement]: ../security/struct.SecurityRequirement.html
    pub fn add_security_scheme<N: Into<String>, S: Into<SecurityScheme>>(&mut self, name: N, security_scheme: S) {
        self.security_schemes.insert(name.into(), security_scheme.into());
    }

    /// Add iterator of [`SecurityScheme`]s to [`Components`].
    ///
    /// Accepts two arguments where first is the name of the [`SecurityScheme`]. This is later when
    /// referenced by [`SecurityRequirement.requirement`]s. Second parameter is the [`SecurityScheme`].
    pub fn add_security_schemes_from_iter<N: Into<String>, S: Into<SecurityScheme>>(
        &mut self,
        schemas: impl IntoIterator<Item = (N, S)>,
    ) {
        self.security_schemes
            .extend(schemas.into_iter().map(|(name, item)| (name.into(), item.into())));
    }

    /// Add [`Schema`] to [`Components`].
    ///
    /// Accepts two arguments where first is the name of the [`Schema`]. This is later when
    /// referenced by [`Ref.ref_location`]s. Second parameter is the [`Schema`].
    pub fn add_schema<N: Into<String>, S: Into<Schema>>(&mut self, name: N, scheme: S) {
        self.schemas.insert(name.into(), scheme.into());
    }

    /// Add iterator of [`Schema`]s to [`Components`].
    ///
    /// Accepts two arguments where first is the name of the [`Schema`]. This is later when
    /// referenced by [`Ref.ref_location`]s. Second parameter is the [`Schema`].
    ///
    /// [requirement]: ../security/struct.SecurityRequirement.html
    pub fn add_schemas_from_iter<N: Into<String>, S: Into<Schema>>(&mut self, schemas: impl IntoIterator<Item = (N, S)>) {
        self.schemas
            .extend(schemas.into_iter().map(|(name, item)| (name.into(), item.into())));
    }
}

impl<S: components_builder::State> ComponentsBuilder<S> {
    /// Add [`Schema`] to [`Components`].
    ///
    /// Accepts two arguments where first is name of the schema and second is the schema itself.
    pub fn schema(mut self, name: impl Into<String>, schema: impl Into<Schema>) -> Self {
        self.schemas.insert(name.into(), schema.into());
        self
    }

    /// Add [`Schema`]s from iterator.
    ///
    /// # Examples
    /// ```rust
    /// # use openapiv3_1::schema::{Components, Object, Type, Schema};
    /// Components::builder().schemas_from_iter([(
    ///     "Pet",
    ///     Schema::from(
    ///         Object::builder()
    ///             .property(
    ///                 "name",
    ///                 Object::builder().schema_type(Type::String),
    ///             )
    ///             .required(["name"])
    ///     ),
    /// )]);
    /// ```
    pub fn schemas_from_iter<I: IntoIterator<Item = (S2, C)>, C: Into<Schema>, S2: Into<String>>(
        mut self,
        schemas: I,
    ) -> Self {
        self.schemas
            .extend(schemas.into_iter().map(|(name, schema)| (name.into(), schema.into())));

        self
    }

    /// Add [`struct@Response`] to [`Components`].
    ///
    /// Method accepts tow arguments; `name` of the reusable response and `response` which is the
    /// reusable response itself.
    pub fn response<S2: Into<String>, R: Into<RefOr<Response>>>(mut self, name: S2, response: R) -> Self {
        self.responses.insert(name.into(), response.into());
        self
    }

    /// Add multiple [`struct@Response`]s to [`Components`] from iterator.
    ///
    /// Like the [`ComponentsBuilder::schemas_from_iter`] this allows adding multiple responses by
    /// any iterator what returns tuples of (name, response) values.
    pub fn responses_from_iter<I: IntoIterator<Item = (S2, R)>, S2: Into<String>, R: Into<RefOr<Response>>>(
        mut self,
        responses: I,
    ) -> Self {
        self.responses
            .extend(responses.into_iter().map(|(name, response)| (name.into(), response.into())));

        self
    }

    /// Add [`SecurityScheme`] to [`Components`].
    ///
    /// Accepts two arguments where first is the name of the [`SecurityScheme`]. This is later when
    /// referenced by [`SecurityRequirement`][requirement]s. Second parameter is the [`SecurityScheme`].
    ///
    /// [requirement]: ../security/struct.SecurityRequirement.html
    pub fn security_scheme<N: Into<String>, S2: Into<SecurityScheme>>(mut self, name: N, security_scheme: S2) -> Self {
        self.security_schemes.insert(name.into(), security_scheme.into());

        self
    }
}

impl<S: components_builder::IsComplete> From<ComponentsBuilder<S>> for Components {
    fn from(value: ComponentsBuilder<S>) -> Self {
        value.build()
    }
}

impl Default for Schema {
    fn default() -> Self {
        Schema::Bool(true)
    }
}

/// OpenAPI [Discriminator][discriminator] object which can be optionally used together with
/// [`Object`] composite object.
///
/// [discriminator]: https://spec.openapis.org/oas/latest.html#discriminator-object
#[derive(Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Discriminator {
    /// Defines a discriminator property name which must be found within all composite
    /// objects.
    pub property_name: String,

    /// An object to hold mappings between payload values and schema names or references.
    /// This field can only be populated manually. There is no macro support and no
    /// validation.
    #[serde(skip_serializing_if = "IndexMap::is_empty", default)]
    pub mapping: IndexMap<String, String>,

    /// Optional extensions "x-something".
    #[serde(skip_serializing_if = "Option::is_none", flatten)]
    pub extensions: Option<Extensions>,
}

impl Discriminator {
    /// Construct a new [`Discriminator`] object with property name.
    ///
    /// # Examples
    ///
    /// Create a new [`Discriminator`] object for `pet_type` property.
    /// ```rust
    /// # use openapiv3_1::schema::Discriminator;
    /// let discriminator = Discriminator::new("pet_type");
    /// ```
    pub fn new<I: Into<String>>(property_name: I) -> Self {
        Self {
            property_name: property_name.into(),
            mapping: IndexMap::new(),
            ..Default::default()
        }
    }

    /// Construct a new [`Discriminator`] object with property name and mappings.
    ///
    ///
    /// Method accepts two arguments. First _`property_name`_ to use as `discriminator` and
    /// _`mapping`_ for custom property name mappings.
    ///
    /// # Examples
    ///
    /// _**Construct an ew [`Discriminator`] with custom mapping.**_
    ///
    /// ```rust
    /// # use openapiv3_1::schema::Discriminator;
    /// let discriminator = Discriminator::with_mapping("pet_type", [
    ///     ("cat","#/components/schemas/Cat")
    /// ]);
    /// ```
    pub fn with_mapping<P: Into<String>, M: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>>(
        property_name: P,
        mapping: M,
    ) -> Self {
        Self {
            property_name: property_name.into(),
            mapping: IndexMap::from_iter(mapping.into_iter().map(|(key, val)| (key.into(), val.into()))),
            ..Default::default()
        }
    }
}

/// Implements [OpenAPI Reference Object][reference] that can be used to reference
/// reusable components such as [`Schema`]s or [`Response`]s.
///
/// [reference]: https://spec.openapis.org/oas/latest.html#reference-object
#[non_exhaustive]
#[derive(Serialize, Deserialize, Default, Clone, PartialEq, Eq, bon::Builder)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[builder(on(_, into))]
pub struct Ref {
    /// Reference location of the actual component.
    #[serde(rename = "$ref")]
    pub ref_location: String,

    /// A description which by default should override that of the referenced component.
    /// Description supports markdown syntax. If referenced object type does not support
    /// description this field does not have effect.
    #[serde(skip_serializing_if = "String::is_empty", default)]
    #[builder(default)]
    pub description: String,

    /// A short summary which by default should override that of the referenced component. If
    /// referenced component does not support summary field this does not have effect.
    #[serde(skip_serializing_if = "String::is_empty", default)]
    #[builder(default)]
    pub summary: String,
}

impl Ref {
    /// Construct a new [`Ref`] with custom ref location. In most cases this is not necessary
    /// and [`Ref::from_schema_name`] could be used instead.
    pub fn new<I: Into<String>>(ref_location: I) -> Self {
        Self {
            ref_location: ref_location.into(),
            ..Default::default()
        }
    }

    /// Construct a new [`Ref`] from provided schema name. This will create a [`Ref`] that
    /// references the the reusable schemas.
    pub fn from_schema_name<I: Into<String>>(schema_name: I) -> Self {
        Self::new(format!("#/components/schemas/{}", schema_name.into()))
    }

    /// Construct a new [`Ref`] from provided response name. This will create a [`Ref`] that
    /// references the reusable response.
    pub fn from_response_name<I: Into<String>>(response_name: I) -> Self {
        Self::new(format!("#/components/responses/{}", response_name.into()))
    }
}

impl<S: ref_builder::IsComplete> From<RefBuilder<S>> for Schema {
    fn from(builder: RefBuilder<S>) -> Self {
        Self::from(builder.build())
    }
}

impl From<Ref> for Schema {
    fn from(r: Ref) -> Self {
        Self::object(
            Object::builder()
                .reference(r.ref_location)
                .description(r.description)
                .summary(r.summary)
                .build(),
        )
    }
}

impl<T> From<T> for RefOr<T> {
    fn from(t: T) -> Self {
        Self::T(t)
    }
}

/// JSON Schema Type
/// <https://www.learnjsonschema.com/2020-12/validation/type>
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Copy)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[non_exhaustive]
pub enum Type {
    /// JSON array
    #[serde(rename = "array")]
    Array,
    /// The JSON true or false constants
    #[serde(rename = "boolean")]
    Boolean,
    /// A JSON number that represents an integer
    #[serde(rename = "integer")]
    Integer,
    /// The JSON null constant
    #[serde(rename = "null")]
    Null,
    /// A JSON number
    #[serde(rename = "number")]
    Number,
    /// A JSON object
    #[serde(rename = "object")]
    Object,
    /// A JSON string
    #[serde(rename = "string")]
    String,
}

/// JSON Schema Type
///
/// `type` can either be a singular type or an array of types.
///
/// <https://www.learnjsonschema.com/2020-12/validation/type>
#[derive(Serialize, Deserialize, Clone, PartialEq)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[serde(untagged)]
pub enum Types {
    /// A singular type
    Single(Type),
    /// Multiple types
    Multi(Vec<Type>),
}

impl From<Type> for Types {
    fn from(value: Type) -> Self {
        Self::Single(value)
    }
}

impl From<Vec<Type>> for Types {
    fn from(mut value: Vec<Type>) -> Self {
        if value.len() == 1 {
            Self::Single(value.remove(0))
        } else {
            Self::Multi(value)
        }
    }
}

trait IsEmpty {
    fn is_empty(&self) -> bool;
}

impl<T> IsEmpty for Option<T> {
    fn is_empty(&self) -> bool {
        self.is_none()
    }
}

impl<T> IsEmpty for Vec<T> {
    fn is_empty(&self) -> bool {
        Vec::is_empty(self)
    }
}

impl<K, V> IsEmpty for IndexMap<K, V> {
    fn is_empty(&self) -> bool {
        IndexMap::is_empty(self)
    }
}

impl IsEmpty for String {
    fn is_empty(&self) -> bool {
        self.is_empty()
    }
}

/// A JSON Schema Object as per JSON Schema specification.
/// <https://www.learnjsonschema.com/2020-12/>
#[derive(serde_derive::Serialize, serde_derive::Deserialize, Clone, PartialEq, Default, bon::Builder)]
#[serde(default, deny_unknown_fields)]
#[builder(on(_, into))]
#[cfg_attr(feature = "debug", derive(Debug))]
#[non_exhaustive]
pub struct Object {
    /// The `properties` keyword restricts object properties to the given subschemas.
    /// Collected annotations report which properties were evaluated.
    /// <https://www.learnjsonschema.com/2020-12/applicator/properties/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(field)]
    pub properties: IndexMap<String, Schema>,
    /// The `examples` keyword provides example instances for documentation.
    /// Does not affect validation.
    /// <https://www.learnjsonschema.com/2020-12/meta-data/examples/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(field)]
    pub examples: Vec<serde_json::Value>,
    /// The `prefixItems` keyword validates the first items of an array against a sequence of subschemas.
    /// Remaining items fall back to `items`, if present.
    /// <https://www.learnjsonschema.com/2020-12/applicator/prefixitems/>
    #[serde(rename = "prefixItems", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(field)]
    pub prefix_items: Option<Vec<Schema>>,
    /// The `enum` keyword restricts instances to a finite set of values.
    /// <https://www.learnjsonschema.com/2020-12/validation/enum/>
    #[serde(rename = "enum", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(field)]
    pub enum_values: Option<Vec<serde_json::Value>>,
    /// The `required` keyword lists property names that must be present in an object.
    /// <https://www.learnjsonschema.com/2020-12/applicator/required/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(field)]
    pub required: Vec<String>,
    /// The `allOf` keyword requires instance validation against all subschemas.
    /// <https://www.learnjsonschema.com/2020-12/validation/allof/>
    #[serde(rename = "allOf", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(field)]
    pub all_of: Vec<Schema>,
    /// The `anyOf` keyword requires validation against at least one subschema.
    /// <https://www.learnjsonschema.com/2020-12/validation/anyof/>
    #[serde(rename = "anyOf", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(field)]
    pub any_of: Option<Vec<Schema>>,
    /// The `oneOf` keyword requires validation against exactly one subschema.
    /// <https://www.learnjsonschema.com/2020-12/validation/oneof/>
    #[serde(rename = "oneOf", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(field)]
    pub one_of: Option<Vec<Schema>>,
    /// The `$id` keyword defines a unique identifier for the schema.
    /// <https://www.learnjsonschema.com/2020-12/meta-data/id/>
    #[serde(rename = "$id", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub id: String,
    /// The `$schema` keyword declares the JSON Schema version.
    /// <https://www.learnjsonschema.com/2020-12/meta-data/schema/>
    #[serde(rename = "$schema", skip_serializing_if = "IsEmpty::is_empty")]
    pub schema: Option<Schema>,
    /// The `$ref` keyword references an external or internal schema by URI.
    /// <https://www.learnjsonschema.com/2020-12/structure/$ref/>
    #[serde(rename = "$ref", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default, name = "reference")]
    pub reference: String,
    /// The `$comment` keyword provides annotations for documentation.
    /// <https://www.learnjsonschema.com/2020-12/meta-data/comment/>
    #[serde(rename = "$comment", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub comment: String,
    /// The `title` keyword provides a short descriptive title.
    /// <https://www.learnjsonschema.com/2020-12/meta-data/title/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub title: String,
    /// The `description` keyword provides a detailed description.
    /// <https://www.learnjsonschema.com/2020-12/meta-data/description/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub description: String,
    /// The `summary` keyword offers a brief summary for documentation.
    /// <https://www.learnjsonschema.com/2020-12/meta-data/summary/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub summary: String,
    /// The `default` keyword provides a default instance value.
    /// <https://www.learnjsonschema.com/2020-12/validation/default/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    pub default: Option<serde_json::Value>,
    /// The `readOnly` keyword marks a property as read-only.
    /// <https://www.learnjsonschema.com/2020-12/validation/readOnly/>
    #[serde(rename = "readOnly", skip_serializing_if = "IsEmpty::is_empty")]
    pub read_only: Option<bool>,
    /// The `deprecated` keyword marks a schema as deprecated.
    /// <https://www.learnjsonschema.com/2020-12/meta-data/deprecated/>
    #[serde(rename = "deprecated", skip_serializing_if = "IsEmpty::is_empty")]
    pub deprecated: Option<bool>,
    /// The `writeOnly` keyword marks a property as write-only.
    /// <https://www.learnjsonschema.com/2020-12/validation/writeOnly/>
    #[serde(rename = "writeOnly", skip_serializing_if = "IsEmpty::is_empty")]
    pub write_only: Option<bool>,
    /// The `multipleOf` keyword ensures the number is a multiple of this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/multipleOf/>
    #[serde(rename = "multipleOf", skip_serializing_if = "IsEmpty::is_empty")]
    pub multiple_of: Option<OrderedFloat<f64>>,
    /// The `maximum` keyword defines the maximum numeric value.
    /// <https://www.learnjsonschema.com/2020-12/validation/maximum/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    pub maximum: Option<OrderedFloat<f64>>,
    /// The `exclusiveMaximum` keyword requires the number to be less than this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/exclusiveMaximum/>
    #[serde(rename = "exclusiveMaximum", skip_serializing_if = "IsEmpty::is_empty")]
    pub exclusive_maximum: Option<OrderedFloat<f64>>,
    /// The `minimum` keyword defines the minimum numeric value.
    /// <https://www.learnjsonschema.com/2020-12/validation/minimum/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    pub minimum: Option<OrderedFloat<f64>>,
    /// The `exclusiveMinimum` keyword requires the number to be greater than this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/exclusiveMinimum/>
    #[serde(rename = "exclusiveMinimum", skip_serializing_if = "IsEmpty::is_empty")]
    pub exclusive_minimum: Option<OrderedFloat<f64>>,
    /// The `maxLength` keyword restricts string length to at most this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/maxLength/>
    #[serde(rename = "maxLength", skip_serializing_if = "IsEmpty::is_empty")]
    pub max_length: Option<u64>,
    /// The `minLength` keyword restricts string length to at least this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/minLength/>
    #[serde(rename = "minLength", skip_serializing_if = "IsEmpty::is_empty")]
    pub min_length: Option<u64>,
    /// The `pattern` keyword restricts strings to those matching this regular expression.
    /// <https://www.learnjsonschema.com/2020-12/validation/pattern/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    pub pattern: Option<String>,
    /// The `additionalItems` keyword defines the schema for array elements beyond those covered by a tuple definition.
    /// <https://www.learnjsonschema.com/2020-12/applicator/additionalItems/>
    #[serde(rename = "additionalItems", skip_serializing_if = "IsEmpty::is_empty")]
    pub additional_items: Option<Schema>,
    /// The `items` keyword restricts all elements in an array to this schema, or provides a tuple of schemas for positional validation.
    /// <https://www.learnjsonschema.com/2020-12/applicator/items/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    pub items: Option<Schema>,
    /// The `maxItems` keyword restricts the number of elements in an array to at most this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/maxItems/>
    #[serde(rename = "maxItems", skip_serializing_if = "IsEmpty::is_empty")]
    pub max_items: Option<u64>,
    /// The `minItems` keyword restricts the number of elements in an array to at least this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/minItems/>
    #[serde(rename = "minItems", skip_serializing_if = "IsEmpty::is_empty")]
    pub min_items: Option<u64>,
    /// The `uniqueItems` keyword ensures that all elements in an array are unique.
    /// <https://www.learnjsonschema.com/2020-12/validation/uniqueItems/>
    #[serde(rename = "uniqueItems", skip_serializing_if = "IsEmpty::is_empty")]
    pub unique_items: Option<bool>,
    /// The `contains` keyword ensures that at least one element in the array matches the specified schema.
    /// <https://www.learnjsonschema.com/2020-12/applicator/contains/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    pub contains: Option<Schema>,
    /// The `maxProperties` keyword restricts the number of properties in an object to at most this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/maxProperties/>
    #[serde(rename = "maxProperties", skip_serializing_if = "IsEmpty::is_empty")]
    pub max_properties: Option<u64>,
    /// The `minProperties` keyword restricts the number of properties in an object to at least this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/minProperties/>
    #[serde(rename = "minProperties", skip_serializing_if = "IsEmpty::is_empty")]
    pub min_properties: Option<u64>,
    /// The `maxContains` keyword limits how many items matching `contains` may appear in an array.
    /// <https://www.learnjsonschema.com/2020-12/applicator/maxContains/>
    #[serde(rename = "maxContains", skip_serializing_if = "IsEmpty::is_empty")]
    pub max_contains: Option<u64>,
    /// The `minContains` keyword requires at least this many items matching `contains` in an array.
    /// <https://www.learnjsonschema.com/2020-12/applicator/minContains/>
    #[serde(rename = "minContains", skip_serializing_if = "IsEmpty::is_empty")]
    pub min_contains: Option<u64>,
    /// The `additionalProperties` keyword defines the schema for object properties not explicitly listed.
    /// <https://www.learnjsonschema.com/2020-12/applicator/additionalProperties/>
    #[serde(rename = "additionalProperties", skip_serializing_if = "IsEmpty::is_empty")]
    pub additional_properties: Option<Schema>,
    /// The `definitions` section holds reusable schema definitions for reference.
    /// <https://www.learnjsonschema.com/2020-12/meta-data/definitions/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub definitions: IndexMap<String, Schema>,
    /// The `patternProperties` keyword maps regex patterns to schemas for matching property names.
    /// <https://www.learnjsonschema.com/2020-12/applicator/patternProperties/>
    #[serde(rename = "patternProperties", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub pattern_properties: IndexMap<String, Schema>,
    /// The `dependencies` keyword specifies schema or property dependencies for an object.
    /// <https://www.learnjsonschema.com/2020-12/applicator/dependencies/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub dependencies: IndexMap<String, Schema>,
    /// The `propertyNames` keyword restricts all property names in an object to match this schema.
    /// <https://www.learnjsonschema.com/2020-12/applicator/propertyNames/>
    #[serde(rename = "propertyNames", skip_serializing_if = "IsEmpty::is_empty")]
    pub property_names: Option<Schema>,
    /// The `const` keyword requires the instance to be exactly this value.
    /// <https://www.learnjsonschema.com/2020-12/validation/const/>
    #[serde(rename = "const", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(name = "const_value")]
    pub const_value: Option<serde_json::Value>,
    /// The `type` keyword restricts the instance to the specified JSON types.
    /// <https://www.learnjsonschema.com/2020-12/validation/type/>
    #[serde(rename = "type", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(name = "schema_type")]
    pub schema_type: Option<Types>,
    /// The `format` keyword provides semantic validation hints, such as "email" or "date-time".
    /// <https://www.learnjsonschema.com/2020-12/meta-data/format/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub format: String,
    /// The `contentMediaType` annotation describes the media type for string content.
    /// <https://www.learnjsonschema.com/2020-12/annotations/contentMediaType/>
    #[serde(rename = "contentMediaType", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub content_media_type: String,
    /// The `contentEncoding` annotation describes the encoding (e.g., "base64") for string content.
    /// <https://www.learnjsonschema.com/2020-12/annotations/contentEncoding/>
    #[serde(rename = "contentEncoding", skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(default)]
    pub content_encoding: String,
    /// The `contentSchema` annotation defines a schema for binary media represented as a string.
    /// <https://www.learnjsonschema.com/2020-12/applicator/contentSchema/>
    #[serde(rename = "contentSchema", skip_serializing_if = "IsEmpty::is_empty")]
    pub content_schema: Option<Schema>,
    /// The `if` keyword applies conditional schema validation when this subschema is valid.
    /// <https://www.learnjsonschema.com/2020-12/applicator/if/>
    #[serde(rename = "if", skip_serializing_if = "IsEmpty::is_empty")]
    pub if_cond: Option<Schema>,
    /// The `then` keyword applies this subschema when the `if` condition is met.
    /// <https://www.learnjsonschema.com/2020-12/applicator/then/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    #[builder(name = "then_cond")]
    pub then: Option<Schema>,
    /// The `else` keyword applies this subschema when the `if` condition is not met.
    /// <https://www.learnjsonschema.com/2020-12/applicator/else/>
    #[serde(rename = "else", skip_serializing_if = "IsEmpty::is_empty")]
    pub else_cond: Option<Schema>,
    /// The `not` keyword ensures the instance does *not* match this subschema.
    /// <https://www.learnjsonschema.com/2020-12/applicator/not/>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    pub not: Option<Schema>,
    /// The `unevaluatedItems` keyword applies schemas to items not covered by `items` or `contains`.
    /// <https://www.learnjsonschema.com/2020-12/applicator/unevaluatedItems/>
    #[serde(rename = "unevaluatedItems", skip_serializing_if = "IsEmpty::is_empty")]
    pub unevaluated_items: Option<Schema>,
    /// The `unevaluatedProperties` keyword applies schemas to properties not covered by `properties` or pattern-based keywords.
    /// <https://www.learnjsonschema.com/2020-12/applicator/unevaluatedProperties/>
    #[serde(rename = "unevaluatedProperties", skip_serializing_if = "IsEmpty::is_empty")]
    pub unevaluated_properties: Option<Schema>,
    /// The `discriminator` keyword provides object property-based type differentiation (OpenAPI).
    /// <https://spec.openapis.org/oas/v3.1.0#discriminator-object>
    #[serde(skip_serializing_if = "IsEmpty::is_empty")]
    pub discriminator: Option<Discriminator>,
    /// All additional, unrecognized fields are stored here as extensions.
    #[serde(flatten)]
    pub extensions: Option<Extensions>,
}

impl From<Ref> for Object {
    fn from(value: Ref) -> Self {
        Self::builder()
            .reference(value.ref_location)
            .description(value.description)
            .summary(value.summary)
            .build()
    }
}

impl<S: object_builder::State> ObjectBuilder<S> {
    /// Extend the properties using the iterator of `(name, schema)`
    pub fn properties<P: Into<String>, C: Into<Schema>>(mut self, properties: impl IntoIterator<Item = (P, C)>) -> Self {
        self.properties
            .extend(properties.into_iter().map(|(p, s)| (p.into(), s.into())));
        self
    }

    /// Add a singular property
    pub fn property(mut self, name: impl Into<String>, schema: impl Into<Schema>) -> Self {
        self.properties.insert(name.into(), schema.into());
        self
    }

    /// Add a singular schema into the `allOf` array
    pub fn all_of(mut self, all_of: impl Into<Schema>) -> Self {
        self.all_of.push(all_of.into());
        self
    }

    /// Extend the `allOf` array using the iterator of schemas
    pub fn all_ofs<C: Into<Schema>>(mut self, all_ofs: impl IntoIterator<Item = C>) -> Self {
        self.all_of.extend(all_ofs.into_iter().map(|s| s.into()));
        self
    }

    /// Extend the `anyOf` array using the iterator of schemas
    pub fn any_ofs<C: Into<Schema>>(self, any_ofs: impl IntoIterator<Item = C>) -> Self {
        any_ofs.into_iter().fold(self, |this, c| this.any_of(c))
    }

    /// Add a singular schema into the `anyOf` array
    pub fn any_of(mut self, any_of: impl Into<Schema>) -> Self {
        self.any_of.get_or_insert_default().push(any_of.into());
        self
    }

    /// Extend the `oneOfs` array using the iterator of schemas
    pub fn one_ofs<C: Into<Schema>>(self, one_ofs: impl IntoIterator<Item = C>) -> Self {
        one_ofs.into_iter().fold(self, |this, c| this.one_of(c))
    }

    /// Add a singular schema into the `oneOf` array
    pub fn one_of(mut self, one_of: impl Into<Schema>) -> Self {
        self.one_of.get_or_insert_default().push(one_of.into());
        self
    }

    /// Add a singular item into the `enum` array
    pub fn enum_value(mut self, enum_value: impl Into<serde_json::Value>) -> Self {
        self.enum_values.get_or_insert_default().push(enum_value.into());
        self
    }

    /// Extend the `enum` array using an iterator of items
    pub fn enum_values<E: Into<serde_json::Value>>(self, enum_values: impl IntoIterator<Item = E>) -> Self {
        enum_values.into_iter().fold(self, |this, e| this.enum_value(e))
    }

    /// Add a single field into the `required` array
    pub fn require(mut self, require: impl Into<String>) -> Self {
        self.required.push(require.into());
        self
    }

    /// Extend the `required` array from the iterator of fields.
    pub fn required<R: Into<String>>(self, required: impl IntoIterator<Item = R>) -> Self {
        required.into_iter().fold(self, |this, e| this.require(e))
    }

    /// Add a single example to the `examples` array
    pub fn example(mut self, example: impl Into<serde_json::Value>) -> Self {
        self.examples.push(example.into());
        self
    }

    /// Extend the `examples` array using an iterator of examples.
    pub fn examples<E: Into<serde_json::Value>>(self, examples: impl IntoIterator<Item = E>) -> Self {
        examples.into_iter().fold(self, |this, e| this.example(e))
    }
}

impl<S: object_builder::IsComplete> ObjectBuilder<S> {
    /// Convert the object into an array of this type
    pub fn to_array(self) -> ObjectBuilder<object_builder::SetItems<object_builder::SetSchemaType>> {
        Object::builder().schema_type(Type::Array).items(self)
    }
}

impl<S: object_builder::IsComplete> From<ObjectBuilder<S>> for Object {
    fn from(value: ObjectBuilder<S>) -> Self {
        value.build()
    }
}

impl<S: object_builder::IsComplete> From<ObjectBuilder<S>> for Schema {
    fn from(value: ObjectBuilder<S>) -> Self {
        value.build().into()
    }
}

impl Object {
    /// Create a new object builder with the schema type.
    /// Short hand for
    /// ```rust
    /// # use openapiv3_1::{Object, schema::Type};
    /// # let ty = Type::Null;
    /// # let _ = {
    /// Object::builder().schema_type(ty)
    /// # };
    /// ```
    pub fn with_type(ty: impl Into<Types>) -> ObjectBuilder<object_builder::SetSchemaType> {
        Object::builder().schema_type(ty)
    }

    /// An object that represents an [`i32`]
    pub fn int32() -> Object {
        Object::builder()
            .schema_type(Type::Integer)
            .maximum(i32::MAX as f64)
            .minimum(i32::MIN as f64)
            .build()
    }

    /// An object that represents an [`i64`]
    pub fn int64() -> Object {
        Object::builder()
            .schema_type(Type::Integer)
            .maximum(i64::MAX as f64)
            .minimum(i64::MIN as f64)
            .build()
    }

    /// An object that represents an [`u32`]
    pub fn uint32() -> Object {
        Object::builder()
            .schema_type(Type::Integer)
            .maximum(u32::MAX as f64)
            .minimum(u32::MIN as f64)
            .build()
    }

    /// An object that represents an [`u64`]
    pub fn uint64() -> Object {
        Object::builder()
            .schema_type(Type::Integer)
            .maximum(u64::MAX as f64)
            .minimum(u64::MIN as f64)
            .build()
    }

    /// Convert the object into an array of that type.
    pub fn to_array(self) -> Self {
        Self::builder().schema_type(Type::Array).items(self).build()
    }

    /// Builds a new object where its an aggregate of all the objects in the iterator.
    /// Short hand for
    /// ```rust
    /// # use openapiv3_1::{Object, schema::Type};
    /// # let all_ofs = [true];
    /// # let _ = {
    /// Object::builder().all_ofs(all_ofs).build()
    /// # };
    /// ```
    pub fn all_ofs<S: Into<Schema>>(all_ofs: impl IntoIterator<Item = S>) -> Object {
        Object::builder().all_ofs(all_ofs).build()
    }
}

macro_rules! iter_chain {
    ($($item:expr),*$(,)?) => {
        std::iter::empty()
            $(.chain($item))*
    };
}

macro_rules! merge_item {
    ([$self:ident, $other:ident] => { $($item:ident => $merge_behaviour:expr),*$(,)? }) => {$({
        let self_item = &mut $self.$item;
        let other_item = &mut $other.$item;
        if IsEmpty::is_empty(self_item) {
            *self_item = std::mem::take(other_item);
        } else if self_item == other_item {
            std::mem::take(other_item);
        } else if !IsEmpty::is_empty(other_item) {
            $merge_behaviour(self_item, other_item);
        }
    })*};
}

fn dedupe_array<T: PartialEq>(items: &mut Vec<T>) {
    let mut dedupe = Vec::new();
    for item in items.drain(..) {
        if !dedupe.contains(&item) {
            dedupe.push(item);
        }
    }

    *items = dedupe;
}

impl Object {
    /// Optimize the openapi schema
    /// This will compress nested `allOfs` and try merge things together.
    pub fn optimize(&mut self) {
        // Collect allofs.
        let mut all_ofs = Vec::new();
        self.take_all_ofs(&mut all_ofs);

        all_ofs
            .iter_mut()
            .filter_map(|schema| schema.as_object_mut())
            .for_each(|schema| self.merge(schema));

        // recursively call optimize
        let sub_schemas = iter_chain!(
            self.schema.iter_mut(),
            self.additional_items.iter_mut(),
            self.contains.iter_mut(),
            self.additional_properties.iter_mut(),
            self.items.iter_mut(),
            self.prefix_items.iter_mut().flatten(),
            self.definitions.values_mut(),
            self.properties.values_mut(),
            self.pattern_properties.values_mut(),
            self.dependencies.values_mut(),
            self.property_names.iter_mut(),
            self.if_cond.iter_mut(),
            self.then.iter_mut(),
            self.else_cond.iter_mut(),
            self.any_of.iter_mut().flatten(),
            self.one_of.iter_mut().flatten(),
            self.not.iter_mut(),
            self.unevaluated_items.iter_mut(),
            self.unevaluated_properties.iter_mut(),
            self.content_schema.iter_mut(),
        );

        for schema in sub_schemas {
            schema.optimize();
        }

        self.all_of = all_ofs.into_iter().filter(|schema| !schema.is_empty()).collect();
        dedupe_array(&mut self.examples);
        dedupe_array(&mut self.required);
        if let Some(_enum) = &mut self.enum_values {
            dedupe_array(_enum);
        }
        dedupe_array(&mut self.all_of);
        if let Some(any_of) = &mut self.any_of {
            dedupe_array(any_of);
        }
        if let Some(one_of) = &mut self.one_of {
            dedupe_array(one_of);
        }
    }

    /// Convert the value into an optimized version of itself.
    pub fn into_optimized(mut self) -> Self {
        self.optimize();
        self
    }

    /// Returns true if the object is in the default state.
    pub fn is_empty(&self) -> bool {
        static DEFAULT: std::sync::LazyLock<Object> = std::sync::LazyLock::new(Object::default);

        self == &*DEFAULT
    }

    fn take_all_ofs(&mut self, collection: &mut Vec<Schema>) {
        for mut schema in self.all_of.drain(..) {
            schema.take_all_ofs(collection);
            collection.push(schema);
        }
    }

    fn merge(&mut self, other: &mut Self) {
        merge_item!(
            [self, other] => {
                id => merge_skip,
                schema => merge_sub_schema,
                reference => merge_skip,
                comment => merge_drop_second,
                title => merge_drop_second,
                description => merge_drop_second,
                summary => merge_drop_second,
                default => merge_drop_second,
                read_only => merge_set_true,
                examples => merge_array_combine,
                multiple_of => merge_multiple_of,
                maximum => merge_min,
                exclusive_maximum => merge_min,
                minimum => merge_max,
                exclusive_minimum => merge_min,
                max_length => merge_min,
                min_length => merge_max,
                pattern => merge_skip,
                additional_items => merge_sub_schema,
                items => merge_sub_schema,
                prefix_items => merge_prefix_items,
                max_items => merge_min,
                min_items => merge_max,
                unique_items => merge_set_true,
                contains => merge_sub_schema,
                max_properties => merge_min,
                min_properties => merge_max,
                max_contains => merge_min,
                min_contains => merge_max,
                required => merge_array_combine,
                additional_properties => merge_sub_schema,
                definitions => merge_schema_map,
                properties => merge_schema_map,
                pattern_properties => merge_schema_map,
                dependencies => merge_schema_map,
                property_names => merge_sub_schema,
                const_value => merge_skip,
                enum_values => merge_array_union_optional,
                schema_type => merge_type,
                format => merge_skip,
                content_media_type => merge_skip,
                content_encoding => merge_skip,
                // _if
                // then
                // _else
                any_of => merge_array_combine_optional,
                one_of => merge_array_combine_optional,
                not => merge_sub_schema,
                unevaluated_items => merge_sub_schema,
                unevaluated_properties => merge_sub_schema,
                deprecated => merge_set_true,
                write_only => merge_set_true,
                content_schema => merge_sub_schema,
            }
        );
    }
}

fn merge_skip<T>(_: &mut T, _: &mut T) {}

fn merge_drop_second<T: Default>(_: &mut T, other: &mut T) {
    std::mem::take(other);
}

fn merge_min<T: Ord + Copy>(value: &mut Option<T>, other: &mut Option<T>) {
    let value = value.as_mut().unwrap();
    let other = other.take().unwrap();
    *value = (*value).min(other);
}

fn merge_max<T: Ord + Copy>(value: &mut Option<T>, other: &mut Option<T>) {
    let value = value.as_mut().unwrap();
    let other = other.take().unwrap();
    *value = (*value).max(other);
}

fn merge_set_true(value: &mut Option<bool>, other: &mut Option<bool>) {
    other.take();
    value.replace(true);
}

fn merge_sub_schema(value: &mut Option<Schema>, other_opt: &mut Option<Schema>) {
    let value = value.as_mut().unwrap();
    let mut other = other_opt.take().unwrap();
    value.merge(&mut other);
    if !other.is_empty() {
        other_opt.replace(other);
    }
}

fn merge_array_combine<T: PartialEq>(value: &mut Vec<T>, other: &mut Vec<T>) {
    value.append(other);
}

fn merge_array_union<T: PartialEq>(value: &mut Vec<T>, other: &mut Vec<T>) {
    let other = std::mem::take(other);
    value.retain(|v| other.contains(v));
}

fn merge_array_union_optional<T: PartialEq>(value: &mut Option<Vec<T>>, other: &mut Option<Vec<T>>) {
    merge_array_union(value.as_mut().unwrap(), other.as_mut().unwrap());
    if other.as_ref().is_some_and(|o| o.is_empty()) {
        other.take();
    }
}

fn merge_array_combine_optional<T: PartialEq>(value: &mut Option<Vec<T>>, other: &mut Option<Vec<T>>) {
    merge_array_combine(value.as_mut().unwrap(), other.as_mut().unwrap());
    if other.as_ref().is_some_and(|o| o.is_empty()) {
        other.take();
    }
}

fn merge_schema_map(value: &mut IndexMap<String, Schema>, other: &mut IndexMap<String, Schema>) {
    for (key, mut other) in other.drain(..) {
        match value.entry(key) {
            indexmap::map::Entry::Occupied(mut value) => {
                value.get_mut().merge(&mut other);
                if !other.is_empty() {
                    if let Some(obj) = value.get_mut().as_object_mut() {
                        obj.all_of.push(other);
                    }
                }
            }
            indexmap::map::Entry::Vacant(v) => {
                v.insert(other);
            }
        }
    }
}

fn merge_type(value: &mut Option<Types>, other: &mut Option<Types>) {
    match (value.as_mut().unwrap(), other.take().unwrap()) {
        (Types::Single(s), Types::Single(ref o)) if s != o => {
            value.replace(Types::Multi(Vec::new()));
        }
        (Types::Single(_), Types::Single(_)) => {}
        (Types::Multi(s), Types::Multi(ref mut o)) => {
            merge_array_union(s, o);
        }
        (&mut Types::Single(s), Types::Multi(ref o)) | (&mut Types::Multi(ref o), Types::Single(s)) => {
            if o.contains(&s) {
                value.replace(Types::Single(s));
            } else {
                value.replace(Types::Multi(Vec::new()));
            }
        }
    }
}

fn merge_prefix_items(value: &mut Option<Vec<Schema>>, other: &mut Option<Vec<Schema>>) {
    let mut other = other.take().unwrap_or_default();
    let value = value.as_mut().unwrap();
    value.extend(other.drain(value.len()..));
    for (value, mut other) in value.iter_mut().zip(other) {
        value.merge(&mut other);
        if !other.is_empty() {
            if let Some(obj) = value.as_object_mut() {
                obj.all_of.push(other);
            }
        }
    }
}

fn merge_multiple_of(value: &mut Option<OrderedFloat<f64>>, other: &mut Option<OrderedFloat<f64>>) {
    let value = value.as_mut().unwrap().as_mut();
    let other = other.take().unwrap().into_inner();

    fn gcd_f64(mut a: f64, mut b: f64) -> f64 {
        a = a.abs();
        b = b.abs();
        // if either is zero, gcd is the other
        if a == 0.0 {
            return b;
        }
        if b == 0.0 {
            return a;
        }
        // Euclid’s algorithm via remainer
        while b > 0.0 {
            let r = a % b;
            a = b;
            b = r;
        }
        a
    }

    /// lcm(a, b) = |a * b| / gcd(a, b)
    fn lcm_f64(a: f64, b: f64) -> f64 {
        if a == 0.0 || b == 0.0 {
            return 0.0;
        }
        let g = gcd_f64(a, b);
        // (a / g) * b is a bit safer against overflow than a * (b / g)
        (a / g * b).abs()
    }

    *value = lcm_f64(*value, other);
}

/// A JSON Schema can either be the [`Object`] or a [`bool`]
#[derive(serde_derive::Serialize, serde_derive::Deserialize, Clone, PartialEq)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[serde(untagged)]
#[non_exhaustive]
pub enum Schema {
    /// A json schema object
    Object(Box<Object>),
    /// A singular boolean value
    Bool(bool),
}

impl From<Object> for Schema {
    fn from(value: Object) -> Self {
        Self::object(value)
    }
}

impl From<bool> for Schema {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

impl Schema {
    /// Converts the schema into an array of this type.
    pub fn to_array(self) -> Self {
        Self::object(Object::builder().schema_type(Type::Array).items(self))
    }

    /// Optimizes the schema
    pub fn optimize(&mut self) {
        match self {
            Self::Bool(_) => {}
            Self::Object(obj) => obj.optimize(),
        }
    }

    /// Converts the schema into an optimized version
    pub fn into_optimized(mut self) -> Self {
        match &mut self {
            Self::Bool(_) => {}
            Self::Object(obj) => obj.optimize(),
        }
        self
    }

    /// Make a schema from an object
    pub fn object(value: impl Into<Object>) -> Self {
        Self::Object(value.into().into())
    }

    fn take_all_ofs(&mut self, collection: &mut Vec<Schema>) {
        match self {
            Self::Bool(_) => {}
            Self::Object(obj) => obj.take_all_ofs(collection),
        }
    }

    /// Returns true if the object is in its default state.
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Bool(result) => *result,
            Self::Object(obj) => obj.is_empty(),
        }
    }

    fn as_object_mut(&mut self) -> Option<&mut Object> {
        match self {
            Self::Bool(_) => None,
            Self::Object(obj) => Some(obj.as_mut()),
        }
    }

    fn merge(&mut self, other: &mut Self) {
        match (self, other) {
            (this @ Schema::Bool(false), _) | (this, Schema::Bool(false)) => {
                *this = Schema::Bool(false);
            }
            (this @ Schema::Bool(true), other) => {
                std::mem::swap(this, other);
            }
            (_, Schema::Bool(true)) => {}
            (Schema::Object(value), Schema::Object(other)) => {
                value.merge(other.as_mut());
            }
        }
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use insta::assert_json_snapshot;
    use serde_json::{Value, json};

    use super::*;
    use crate::*;

    #[test]
    fn create_schema_serializes_json() -> Result<(), serde_json::Error> {
        let openapi = OpenApi::builder()
            .info(Info::new("My api", "1.0.0"))
            .paths(Paths::new())
            .components(
                Components::builder()
                    .schema("Person", Ref::new("#/components/PersonModel"))
                    .schema(
                        "Credential",
                        Schema::from(
                            Object::builder()
                                .property(
                                    "id",
                                    Object::builder()
                                        .schema_type(Type::Integer)
                                        .format("int32")
                                        .description("Id of credential")
                                        .default(1i32),
                                )
                                .property(
                                    "name",
                                    Object::builder().schema_type(Type::String).description("Name of credential"),
                                )
                                .property(
                                    "status",
                                    Object::builder()
                                        .schema_type(Type::String)
                                        .default("Active")
                                        .description("Credential status")
                                        .enum_values(["Active", "NotActive", "Locked", "Expired"]),
                                )
                                .property("history", Schema::from(Ref::from_schema_name("UpdateHistory")).to_array())
                                .property("tags", Object::builder().schema_type(Type::String).build().to_array()),
                        ),
                    )
                    .build(),
            )
            .build();

        let serialized = serde_json::to_string_pretty(&openapi)?;
        println!("serialized json:\n {serialized}");

        let value = serde_json::to_value(&openapi)?;
        let credential = get_json_path(&value, "components.schemas.Credential.properties");
        let person = get_json_path(&value, "components.schemas.Person");

        assert!(
            credential.get("id").is_some(),
            "could not find path: components.schemas.Credential.properties.id"
        );
        assert!(
            credential.get("status").is_some(),
            "could not find path: components.schemas.Credential.properties.status"
        );
        assert!(
            credential.get("name").is_some(),
            "could not find path: components.schemas.Credential.properties.name"
        );
        assert!(
            credential.get("history").is_some(),
            "could not find path: components.schemas.Credential.properties.history"
        );
        assert_eq!(
            credential.get("id").unwrap_or(&serde_json::value::Value::Null).to_string(),
            r#"{"default":1,"description":"Id of credential","format":"int32","type":"integer"}"#,
            "components.schemas.Credential.properties.id did not match"
        );
        assert_eq!(
            credential.get("name").unwrap_or(&serde_json::value::Value::Null).to_string(),
            r#"{"description":"Name of credential","type":"string"}"#,
            "components.schemas.Credential.properties.name did not match"
        );
        assert_eq!(
            credential
                .get("status")
                .unwrap_or(&serde_json::value::Value::Null)
                .to_string(),
            r#"{"default":"Active","description":"Credential status","enum":["Active","NotActive","Locked","Expired"],"type":"string"}"#,
            "components.schemas.Credential.properties.status did not match"
        );
        assert_eq!(
            credential
                .get("history")
                .unwrap_or(&serde_json::value::Value::Null)
                .to_string(),
            r###"{"items":{"$ref":"#/components/schemas/UpdateHistory"},"type":"array"}"###,
            "components.schemas.Credential.properties.history did not match"
        );
        assert_eq!(
            person.to_string(),
            r###"{"$ref":"#/components/PersonModel"}"###,
            "components.schemas.Person.ref did not match"
        );

        Ok(())
    }

    // Examples taken from https://spec.openapis.org/oas/latest.html#model-with-map-dictionary-properties
    #[test]
    fn test_property_order() {
        let json_value = Object::builder()
            .property(
                "id",
                Object::builder()
                    .schema_type(Type::Integer)
                    .format("int32")
                    .description("Id of credential")
                    .default(1i32),
            )
            .property(
                "name",
                Object::builder().schema_type(Type::String).description("Name of credential"),
            )
            .property(
                "status",
                Object::builder()
                    .schema_type(Type::String)
                    .default("Active")
                    .description("Credential status")
                    .enum_values(["Active", "NotActive", "Locked", "Expired"]),
            )
            .property("history", Schema::from(Ref::from_schema_name("UpdateHistory")).to_array())
            .property("tags", Object::builder().schema_type(Type::String).to_array())
            .build();

        assert_eq!(
            json_value.properties.keys().collect::<Vec<_>>(),
            vec!["id", "name", "status", "history", "tags"]
        );
    }

    // Examples taken from https://spec.openapis.org/oas/latest.html#model-with-map-dictionary-properties
    #[test]
    fn test_additional_properties() {
        let json_value = Object::builder()
            .schema_type(Type::Object)
            .additional_properties(Object::builder().schema_type(Type::String))
            .build();
        assert_json_snapshot!(json_value, @r#"
        {
          "additionalProperties": {
            "type": "string"
          },
          "type": "object"
        }
        "#);

        let json_value = Object::builder()
            .schema_type(Type::Object)
            .additional_properties(Object::builder().schema_type(Type::Number).to_array())
            .build();

        assert_json_snapshot!(json_value, @r#"
        {
          "additionalProperties": {
            "items": {
              "type": "number"
            },
            "type": "array"
          },
          "type": "object"
        }
        "#);

        let json_value = Object::builder()
            .schema_type(Type::Object)
            .additional_properties(Ref::from_schema_name("ComplexModel"))
            .build();
        assert_json_snapshot!(json_value, @r##"
        {
          "additionalProperties": {
            "$ref": "#/components/schemas/ComplexModel"
          },
          "type": "object"
        }
        "##);
    }

    #[test]
    fn test_object_with_title() {
        let json_value = Object::builder().schema_type(Type::Object).title("SomeName").build();
        assert_json_snapshot!(json_value, @r#"
        {
          "title": "SomeName",
          "type": "object"
        }
        "#);
    }

    #[test]
    fn derive_object_with_examples() {
        let json_value = Object::builder()
            .schema_type(Type::Object)
            .examples([json!({"age": 20, "name": "bob the cat"})])
            .build();
        assert_json_snapshot!(json_value, @r#"
        {
          "examples": [
            {
              "age": 20,
              "name": "bob the cat"
            }
          ],
          "type": "object"
        }
        "#);
    }

    fn get_json_path<'a>(value: &'a Value, path: &str) -> &'a Value {
        path.split('.').fold(value, |acc, fragment| {
            acc.get(fragment).unwrap_or(&serde_json::value::Value::Null)
        })
    }

    #[test]
    fn test_array_new() {
        let array = Object::builder()
            .property(
                "id",
                Object::builder()
                    .schema_type(Type::Integer)
                    .format("int32")
                    .description("Id of credential")
                    .default(json!(1i32)),
            )
            .to_array()
            .build();

        assert!(matches!(array.schema_type, Some(Types::Single(Type::Array))));
    }

    #[test]
    fn test_array_builder() {
        let array = Object::builder()
            .schema_type(Type::Array)
            .items(
                Object::builder().property(
                    "id",
                    Object::builder()
                        .schema_type(Type::Integer)
                        .format("int32")
                        .description("Id of credential")
                        .default(1i32),
                ),
            )
            .build();

        assert!(matches!(array.schema_type, Some(Types::Single(Type::Array))));
    }

    #[test]
    fn reserialize_deserialized_schema_components() {
        let components = Components::builder()
            .schemas_from_iter([(
                "Comp",
                Schema::from(
                    Object::builder()
                        .property("name", Object::builder().schema_type(Type::String))
                        .required(["name"]),
                ),
            )])
            .responses_from_iter(vec![("200", Response::builder().description("Okay").build())])
            .security_scheme(
                "TLS",
                SecurityScheme::MutualTls {
                    description: None,
                    extensions: None,
                },
            )
            .build();

        let serialized_components = serde_json::to_string(&components).unwrap();

        let deserialized_components: Components = serde_json::from_str(serialized_components.as_str()).unwrap();

        assert_eq!(
            serialized_components,
            serde_json::to_string(&deserialized_components).unwrap()
        )
    }

    #[test]
    fn reserialize_deserialized_object_component() {
        let prop = Object::builder()
            .property("name", Object::builder().schema_type(Type::String))
            .required(["name"])
            .build();

        let serialized_components = serde_json::to_string(&prop).unwrap();
        let deserialized_components: Object = serde_json::from_str(serialized_components.as_str()).unwrap();

        assert_eq!(
            serialized_components,
            serde_json::to_string(&deserialized_components).unwrap()
        )
    }

    #[test]
    fn reserialize_deserialized_property() {
        let prop = Object::builder().schema_type(Type::String).build();

        let serialized_components = serde_json::to_string(&prop).unwrap();
        let deserialized_components: Object = serde_json::from_str(serialized_components.as_str()).unwrap();

        assert_eq!(
            serialized_components,
            serde_json::to_string(&deserialized_components).unwrap()
        )
    }

    #[test]
    fn deserialize_reserialize_one_of_default_type() {
        let a = Object::builder()
            .one_ofs([
                Object::builder().property("element", Ref::new("#/test")),
                Object::builder().property("foobar", Ref::new("#/foobar")),
            ])
            .build();

        let serialized_json = serde_json::to_string(&a).expect("should serialize to json");
        let b: Object = serde_json::from_str(&serialized_json).expect("should deserialize OneOf");
        let reserialized_json = serde_json::to_string(&b).expect("reserialized json");

        println!("{serialized_json}");
        println!("{reserialized_json}",);
        assert_eq!(serialized_json, reserialized_json);
    }

    #[test]
    fn serialize_deserialize_any_of_of_within_ref_or_t_object_builder() {
        let ref_or_schema = Object::builder()
            .property(
                "test",
                Object::builder()
                    .any_ofs([
                        Object::builder().property("element", Ref::new("#/test")).build().to_array(),
                        Object::builder().property("foobar", Ref::new("#/foobar")).build(),
                    ])
                    .build(),
            )
            .build();

        let json_str = serde_json::to_string(&ref_or_schema).expect("");
        println!("----------------------------");
        println!("{json_str}");

        let deserialized: RefOr<Schema> = serde_json::from_str(&json_str).expect("");

        let json_de_str = serde_json::to_string(&deserialized).expect("");
        println!("----------------------------");
        println!("{json_de_str}");
        assert!(json_str.contains("\"anyOf\""));
        assert_eq!(json_str, json_de_str);
    }

    #[test]
    fn serialize_deserialize_schema_array_ref_or_t() {
        let ref_or_schema = Object::builder()
            .property("element", Ref::new("#/test"))
            .to_array()
            .to_array()
            .build();

        let json_str = serde_json::to_string(&ref_or_schema).expect("");
        println!("----------------------------");
        println!("{json_str}");

        let deserialized: RefOr<Schema> = serde_json::from_str(&json_str).expect("");

        let json_de_str = serde_json::to_string(&deserialized).expect("");
        println!("----------------------------");
        println!("{json_de_str}");

        assert_eq!(json_str, json_de_str);
    }

    #[test]
    fn serialize_deserialize_schema_array_builder() {
        let ref_or_schema = Object::builder().property("element", Ref::new("#/test")).build().to_array();

        let json_str = serde_json::to_string(&ref_or_schema).expect("");
        println!("----------------------------");
        println!("{json_str}");

        let deserialized: RefOr<Schema> = serde_json::from_str(&json_str).expect("");

        let json_de_str = serde_json::to_string(&deserialized).expect("");
        println!("----------------------------");
        println!("{json_de_str}");

        assert_eq!(json_str, json_de_str);
    }

    #[test]
    fn serialize_deserialize_schema_with_additional_properties() {
        let schema = Object::builder()
            .property("map", Object::builder().additional_properties(true))
            .build();

        let json_str = serde_json::to_string(&schema).unwrap();
        println!("----------------------------");
        println!("{json_str}");

        let deserialized: RefOr<Schema> = serde_json::from_str(&json_str).unwrap();

        let json_de_str = serde_json::to_string(&deserialized).unwrap();
        println!("----------------------------");
        println!("{json_de_str}");

        assert_eq!(json_str, json_de_str);
    }

    #[test]
    fn serialize_deserialize_schema_with_additional_properties_object() {
        let schema = Object::builder()
            .property(
                "map",
                Object::builder()
                    .additional_properties(Object::builder().property("name", Object::builder().schema_type(Type::String))),
            )
            .build();

        let json_str = serde_json::to_string(&schema).unwrap();
        println!("----------------------------");
        println!("{json_str}");

        let deserialized: RefOr<Schema> = serde_json::from_str(&json_str).unwrap();

        let json_de_str = serde_json::to_string(&deserialized).unwrap();
        println!("----------------------------");
        println!("{json_de_str}");

        assert_eq!(json_str, json_de_str);
    }

    #[test]
    fn serialize_discriminator_with_mapping() {
        let mut discriminator = Discriminator::new("type");
        discriminator.mapping = [("int".to_string(), "#/components/schemas/MyInt".to_string())]
            .into_iter()
            .collect::<IndexMap<_, _>>();
        let one_of = Object::builder()
            .one_of(Ref::from_schema_name("MyInt"))
            .discriminator(discriminator)
            .build();
        assert_json_snapshot!(one_of, @r##"
        {
          "oneOf": [
            {
              "$ref": "#/components/schemas/MyInt"
            }
          ],
          "discriminator": {
            "propertyName": "type",
            "mapping": {
              "int": "#/components/schemas/MyInt"
            }
          }
        }
        "##);
    }

    #[test]
    fn serialize_deserialize_object_with_multiple_schema_types() {
        let object = Object::builder().schema_type(vec![Type::Object, Type::Null]).build();

        let json_str = serde_json::to_string(&object).unwrap();
        println!("----------------------------");
        println!("{json_str}");

        let deserialized: Object = serde_json::from_str(&json_str).unwrap();

        let json_de_str = serde_json::to_string(&deserialized).unwrap();
        println!("----------------------------");
        println!("{json_de_str}");

        assert_eq!(json_str, json_de_str);
    }

    #[test]
    fn object_with_extensions() {
        let expected = json!("value");
        let extensions = extensions::Extensions::default().add("x-some-extension", expected.clone());
        let json_value = Object::builder().extensions(extensions).build();

        let value = serde_json::to_value(&json_value).unwrap();
        assert_eq!(value.get("x-some-extension"), Some(&expected));
    }

    #[test]
    fn array_with_extensions() {
        let expected = json!("value");
        let extensions = extensions::Extensions::default().add("x-some-extension", expected.clone());
        let json_value = Object::builder().extensions(extensions).to_array().build();

        let value = serde_json::to_value(&json_value).unwrap();
        assert_eq!(value["items"].get("x-some-extension"), Some(&expected));
    }

    #[test]
    fn oneof_with_extensions() {
        let expected = json!("value");
        let extensions = extensions::Extensions::default().add("x-some-extension", expected.clone());
        let json_value = Object::builder()
            .one_of(Object::builder().extensions(extensions).build())
            .build();

        let value = serde_json::to_value(&json_value).unwrap();
        assert_eq!(value["oneOf"][0].get("x-some-extension"), Some(&expected));
    }

    #[test]
    fn allof_with_extensions() {
        let expected = json!("value");
        let extensions = extensions::Extensions::default().add("x-some-extension", expected.clone());
        let json_value = Object::builder()
            .all_of(Object::builder().extensions(extensions).build())
            .build();

        let value = serde_json::to_value(&json_value).unwrap();
        assert_eq!(value["allOf"][0].get("x-some-extension"), Some(&expected));
    }

    #[test]
    fn anyof_with_extensions() {
        let expected = json!("value");
        let extensions = extensions::Extensions::default().add("x-some-extension", expected.clone());
        let json_value = Object::builder()
            .any_of(Object::builder().extensions(extensions).build())
            .build();

        let value = serde_json::to_value(&json_value).unwrap();
        assert_eq!(value["anyOf"][0].get("x-some-extension"), Some(&expected));
    }
}