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
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
//! The ledger: one case per diagnostic the derives raise.
//!
//! Each derive is an attribute grammar over a closed key set, so what it owes
//! is a case per rule rather than a generator that would re-derive its match
//! arms — see `docs/testing.md`. The rows here assert *which* diagnostic fired;
//! the wording is held by the snapshots in `crates/kynos/tests/ui/macros/`,
//! where a reader sees it rendered.
//!
//! One ledger rather than one test module per derive, because the counters are
//! the point and they are the same counter six times over. A rule added to any
//! grammar without a row fails the build.
use proc_macro2::TokenStream as TokenStream2;
use syn::DeriveInput;
/// What was written, and a fragment of what the derive must say about it.
struct Case {
description: &'static str,
input: DeriveInput,
expects: &'static str,
}
fn case(description: &'static str, declaration: TokenStream2, expects: &'static str) -> Case {
Case {
description,
input: syn::parse2(declaration).expect("the case itself must parse"),
expects,
}
}
/// Runs a ledger against the expansion that owns it.
fn each_case_is_refused(ledger: Vec<Case>, expand: fn(&DeriveInput) -> syn::Result<TokenStream2>) {
for Case {
description,
input,
expects,
} in ledger
{
let Err(error) = expand(&input) else {
panic!("{description} must be rejected");
};
let reported = error.to_string();
assert!(
reported.contains(expects),
"{description}: expected a diagnostic containing {expects:?}, got {reported:?}"
);
}
}
/// Counts a ledger against the diagnostic sites of the file it covers.
///
/// A count, not a mapping: it catches the drift that happens — a rule added
/// without a case — and not a row rewritten to reach a site another covers.
fn every_diagnostic_has_a_case(file: &str, source: &str, cases: usize) {
let sites = source.matches("syn::Error::new(").count() + source.matches("meta.error(").count();
assert_eq!(
cases, sites,
"`{file}` raises {sites} diagnostic(s) and {cases} have a case; a grammar rule added \
without one is a rule that can stop firing silently"
);
}
mod schema {
use super::{
Case, DeriveInput, TokenStream2, case, each_case_is_refused, every_diagnostic_has_a_case,
};
use crate::derive::schema::expand_inner;
fn ledger() -> Vec<Case> {
vec![
case(
"a union, which no JSON value corresponds to",
quote::quote!(
union Payload {
a: u32,
}
),
"cannot describe a union",
),
case(
"`format`, which states what a value is rather than constraining it",
quote::quote!(
struct Order {
#[schema(format = "uuid")]
id: String,
}
),
"`format` says what a value",
),
case(
"`unique_items` given a value, when it is a flag",
quote::quote!(
struct Order {
#[schema(unique_items = 1)]
tags: Vec<String>,
}
),
"is a flag",
),
case(
"a numeric constraint given a string",
quote::quote!(
struct Order {
#[schema(minimum = "x")]
total: u32,
}
),
"takes a number",
),
case(
"a count constraint given a string",
quote::quote!(
struct Order {
#[schema(min_length = "x")]
name: String,
}
),
"takes a non-negative whole number",
),
case(
"a key outside the constraint grammar",
quote::quote!(
struct Order {
#[schema(nonsense = 1)]
total: u32,
}
),
"is not part of the `#[schema(...)]` grammar",
),
case(
"`#[schema(open)]` on a second flattened field of one container",
quote::quote!(
struct Thing {
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
#[serde(flatten)]
#[schema(open)]
more: BTreeMap<String, String>,
}
),
"may appear once per container",
),
case(
"`#[schema(open)]` on a field that is not flattened",
quote::quote!(
struct Thing {
#[schema(open)]
extra: BTreeMap<String, String>,
}
),
"only a flattened field has anything",
),
]
}
/// The serde attributes whose wire form the schema could not follow.
///
/// A second function rather than more rows in the first, for the reason
/// `security_scheme`'s `oauth2_ledger` gives: one list of every diagnostic
/// had outgrown what Clippy will accept. These are the refusals the
/// derive's rustdoc lists as serde and the schema disagreeing.
fn serde_ledger() -> Vec<Case> {
vec![
case(
"an untagged enum, which has no describable decoding rule",
quote::quote!(
#[serde(untagged)]
enum Payload {
Number(u32),
Text(String),
}
),
"an untagged enum",
),
case(
"`serialize_with` on a field, whose wire form its type no longer predicts",
quote::quote!(
struct Reading {
#[serde(serialize_with = "as_string")]
count: u64,
}
),
"does not predict",
),
case(
"a `#[serde(other)]` catch-all, which only 3.2's `defaultMapping` could describe",
quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
id: u64,
},
#[serde(other)]
Unknown,
}
),
"`#[serde(other)]` accepts",
),
case(
"`skip_serializing_if` on a field serde still requires on read",
quote::quote!(
struct Draft {
#[serde(skip_serializing_if = "String::is_empty")]
elided: String,
}
),
"still requires it on read",
),
case(
"`skip_serializing_if` on a flattened field that is not an open map",
quote::quote!(
struct Wrapper {
id: u64,
#[serde(flatten, skip_serializing_if = "Audit::is_empty")]
audit: Audit,
}
),
"on a flattened field is refused unless it is `#[schema(open)]`",
),
case(
"`into` and `from` on a struct, which serde writes and reads as another type",
quote::quote!(
#[serde(into = "String", from = "String")]
struct Celsius {
degrees: i64,
}
),
"as the type it names",
),
case(
"`#[serde(transparent)]` written through one field and read through another",
quote::quote!(
#[serde(transparent)]
struct Split {
#[serde(skip_deserializing)]
a: u64,
#[serde(skip_serializing)]
b: String,
}
),
"`#[serde(transparent)]` makes serde write through",
),
case(
"`skip_serializing` alone on a tuple member, which serde writes one way only",
quote::quote!(
struct Pair(u64, #[serde(skip_serializing)] u64);
),
"leaves this member out in one direction only",
),
case(
"`skip_serializing_if` on a tuple member that is not the last one",
quote::quote!(
struct Reading(
#[serde(default, skip_serializing_if = "is_zero")] u64,
#[serde(default)] String,
);
),
"`skip_serializing_if` on a tuple member is refused unless",
),
]
}
/// The refusals that depend on an enum's variants: how they are tagged, and
/// the variant skips no one schema is true of in both directions.
///
/// A third function for the reason `serde_ledger` gives: that list is at
/// the length Clippy accepts.
fn variant_ledger() -> Vec<Case> {
vec![
case(
"an untagged variant, which serde writes as its bare payload",
quote::quote!(
enum Reading {
Labelled {
value: u64,
},
#[serde(untagged)]
Bare(u64),
}
),
"an untagged variant",
),
case(
"a skipped non-`Option` member of an adjacently tagged newtype variant",
quote::quote!(
#[serde(tag = "t", content = "c")]
enum Reading {
Count(u64),
Hidden(#[serde(skip)] u64),
}
),
"writes the variant as its tag alone",
),
case(
"`skip_deserializing` alone on a variant, which serde writes and never reads",
quote::quote!(
enum Channel {
Web,
#[serde(skip_deserializing)]
Fax,
}
),
"makes serde write this variant and refuse to read it back",
),
case(
"a variant's own name an earlier variant's `alias` claims",
quote::quote!(
#[serde(tag = "kind")]
enum Signal {
#[serde(alias = "Stop")]
Start,
Stop,
}
),
"this variant's own name",
),
]
}
/// The named-field skips no one schema is true of in both directions.
///
/// A fourth function, so that a row about a field does not sit in a ledger
/// named for variants.
fn field_ledger() -> Vec<Case> {
vec![
case(
"`skip_deserializing` alone on a field of an object `deny_unknown_fields` closes",
quote::quote!(
#[serde(deny_unknown_fields)]
struct Thing {
id: u64,
#[serde(skip_deserializing)]
stamp: u64,
}
),
"refuses a member the schema does not name",
),
case(
"an open flattened map in an object serde reads under `deny_unknown_fields`",
quote::quote!(
#[serde(deny_unknown_fields)]
struct Thing {
id: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
),
"reads the map empty",
),
]
}
#[test]
fn each_case_raises_the_diagnostic_it_names() {
each_case_is_refused(ledger(), expand_inner);
each_case_is_refused(serde_ledger(), expand_inner);
each_case_is_refused(variant_ledger(), expand_inner);
each_case_is_refused(field_ledger(), expand_inner);
}
#[test]
fn every_schema_diagnostic_has_a_case() {
every_diagnostic_has_a_case(
"schema.rs",
include_str!("schema.rs"),
ledger().len() + serde_ledger().len() + variant_ledger().len() + field_ledger().len(),
);
}
/// `#[serde(untagged)]` on a struct is serde's diagnostic to raise, not ours.
///
/// The refusal exists because an untagged *enum* has no describable
/// decoding rule. A struct has no variants to choose between, so the
/// sentence does not apply to one -- and serde already refuses the
/// attribute there, in its own words. Raising a second diagnostic that
/// calls a struct an enum is this derive restating a serde shape rule and
/// getting the noun wrong, which is exactly what `Container` reads serde's
/// attributes rather than re-deriving them in order to avoid.
#[test]
fn untagged_on_a_struct_is_left_to_serde() {
let input: syn::DeriveInput = syn::parse2(quote::quote!(
#[serde(untagged)]
struct Receipt {
total: u32,
}
))
.expect("the case itself must parse");
let Err(error) = expand_inner(&input) else {
return;
};
assert!(
!error.to_string().contains("untagged enum"),
"a struct was refused with a sentence about enums: {error}"
);
}
/// An untagged variant serde skips both ways is in no schema, so it has no
/// decoding rule to be ambiguous about and is accepted, as a skipped
/// `#[serde(other)]` variant is.
#[test]
fn untagged_on_a_variant_skipped_both_ways_is_accepted() {
let input: syn::DeriveInput = syn::parse2(quote::quote!(
enum Reading {
Labelled {
value: u64,
},
#[serde(skip, untagged)]
Bare(u64),
}
))
.expect("the case itself must parse");
if let Err(error) = expand_inner(&input) {
panic!("a variant in no schema was refused: {error}");
}
}
/// A name only an earlier variant's `alias` shares is dropped from the
/// later one rather than refused, and one a variant serde skips both ways
/// claims is claimed by nothing serde reads.
#[test]
fn a_shared_name_serde_still_reads_back_is_accepted() {
for declaration in [
quote::quote!(
enum Signal {
Start,
#[serde(alias = "Start")]
Stop,
}
),
quote::quote!(
enum Signal {
#[serde(alias = "go")]
Start,
#[serde(alias = "go")]
Stop,
}
),
quote::quote!(
enum Signal {
#[serde(skip, alias = "Stop")]
Start,
Stop,
}
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
if let Err(error) = expand_inner(&input) {
panic!("a name serde reads back was refused: {error}");
}
}
}
/// Each of serde's three wire-form overrides is refused wherever serde
/// accepts it, on a field and on a variant alike.
///
/// One row per key and placement, each written out: the ledger's single row
/// proves the site fires, and this proves the scan reaches every key and
/// names the one that was written.
#[test]
fn every_wire_form_override_is_refused() {
each_case_is_refused(
vec![
case(
"`with` on a field",
quote::quote!(
struct Reading {
#[serde(with = "as_string")]
count: u64,
}
),
"`with` reads or writes this field",
),
case(
"`serialize_with` on a field",
quote::quote!(
struct Reading {
#[serde(serialize_with = "as_string")]
count: u64,
}
),
"`serialize_with` reads or writes this field",
),
case(
"`deserialize_with` on a field",
quote::quote!(
struct Reading {
#[serde(deserialize_with = "from_string")]
count: u64,
}
),
"`deserialize_with` reads or writes this field",
),
case(
"`with` on a variant",
quote::quote!(
enum Reading {
#[serde(with = "as_string")]
Count(u64),
}
),
"`with` reads or writes this variant",
),
case(
"`serialize_with` on a variant",
quote::quote!(
enum Reading {
#[serde(serialize_with = "as_string")]
Count(u64),
}
),
"`serialize_with` reads or writes this variant",
),
case(
"`deserialize_with` on a variant",
quote::quote!(
enum Reading {
#[serde(deserialize_with = "from_string")]
Count(u64),
}
),
"`deserialize_with` reads or writes this variant",
),
// A member serde skips in one direction only is still scanned,
// and a newtype's member is written whatever it skips, so each
// override here is refused before any skip rule is read.
case(
"`serialize_with` on a skipped member of a tuple struct",
quote::quote!(
struct Pair(
u64,
#[serde(skip_deserializing, serialize_with = "as_string")] u64,
);
),
"`serialize_with` reads or writes this field",
),
case(
"`serialize_with` on the skipped member of a newtype",
quote::quote!(
struct Sku(#[serde(skip_deserializing, serialize_with = "as_string")] u64);
),
"`serialize_with` reads or writes this field",
),
case(
"`serialize_with` on a skipped member of a tuple variant",
quote::quote!(
enum Reading {
Count(
u64,
#[serde(skip_deserializing, serialize_with = "as_string")] u64,
),
}
),
"`serialize_with` reads or writes this field",
),
],
expand_inner,
);
}
/// A wire-form override on something no schema describes is left alone.
///
/// The refusal exists because the schema would describe a value the wire
/// never carries. A skipped named field, every field of a skipped variant,
/// and a member of a tuple, tuple variant or newtype variant that serde
/// skips both ways are in no schema at all, so there is nothing for the
/// override to contradict.
#[test]
fn a_wire_form_override_on_an_undescribed_field_is_left_alone() {
for declaration in [
quote::quote!(
struct Pair(u64, #[serde(skip, with = "as_string")] u64);
),
quote::quote!(
enum Reading {
Count(u64, #[serde(skip, with = "as_string")] u64),
}
),
quote::quote!(
enum Reading {
Total(u64),
Count(#[serde(skip, with = "as_string")] u64),
}
),
// A transparent struct is its one described member, and the other
// is skipped both ways on a two-member tuple.
quote::quote!(
#[serde(transparent)]
struct Pair(u64, #[serde(skip, with = "as_string")] u64);
),
// serde neither writes nor reads through a transparent tuple's
// unpicked member, whatever it skips, as with its named twin.
quote::quote!(
#[serde(transparent)]
struct Pair(
u64,
#[serde(default, skip_serializing, with = "as_string")] u64,
);
),
quote::quote!(
struct Reading {
total: u64,
#[serde(skip, with = "as_string")]
count: u64,
}
),
quote::quote!(
#[serde(tag = "kind")]
enum Reading {
Total {
total: u64,
},
#[serde(skip)]
Count {
#[serde(with = "as_string")]
count: u64,
},
}
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
// Expansion must succeed outright: checking only that the error is
// not this refusal would pass for a declaration refused for any
// other reason.
if let Err(error) = expand_inner(&input) {
panic!("an override nothing describes must expand, and was refused: {error}");
}
}
}
/// A wire-form override on a newtype struct's member is refused whatever
/// serde skips.
///
/// serde ignores skip attributes on a newtype struct and still writes its
/// member through the function, so the exemption a skipped tuple member
/// gets does not reach it.
#[test]
fn a_wire_form_override_on_a_newtype_member_is_refused_whatever_it_skips() {
each_case_is_refused(
vec![case(
"`serialize_with` on the member of a newtype that skips it both ways",
quote::quote!(
struct Sku(#[serde(skip, serialize_with = "as_string")] u64);
),
"`serialize_with` reads or writes this field",
)],
expand_inner,
);
}
/// Each of serde's three container conversions is refused on a struct and
/// on an enum alike.
///
/// One row per key and shape, each written out: the ledger's single row
/// proves the site fires, and this proves the scan reaches every key, both
/// shapes, and names the first key written rather than the first one it
/// looks for.
#[test]
fn every_container_conversion_is_refused() {
each_case_is_refused(
vec![
case(
"`into` on a struct",
quote::quote!(
#[serde(into = "String")]
struct Celsius {
degrees: i64,
}
),
"`into` makes serde read or write this struct as the type it names",
),
case(
"`from` on a struct",
quote::quote!(
#[serde(from = "String")]
struct Celsius {
degrees: i64,
}
),
"`from` makes serde read or write this struct as the type it names",
),
case(
"`try_from` on a struct",
quote::quote!(
#[serde(try_from = "String")]
struct Even {
value: u64,
}
),
"`try_from` makes serde read or write this struct as the type it names",
),
case(
"`into` on an internally tagged enum",
quote::quote!(
#[serde(tag = "kind", into = "String")]
enum Speed {
Fast,
Slow,
}
),
"`into` makes serde read or write this enum as the type it names",
),
case(
"`from` written before `into`, in separate attributes",
quote::quote!(
#[serde(from = "String")]
#[serde(into = "String")]
struct Celsius {
degrees: i64,
}
),
"`from` makes serde read or write this struct as the type it names",
),
// The untagged-enum refusal would also fire here. The
// conversion is reported instead, because every other rule
// reads a declaration the conversion says the wire does not
// follow.
case(
"`from` on an untagged enum, which the untagged refusal also refuses",
quote::quote!(
#[serde(untagged, from = "String")]
enum Payload {
Number(u32),
Text(String),
}
),
"`from` makes serde read or write this enum as the type it names",
),
],
expand_inner,
);
}
/// `#[serde(remote = ...)]` is left alone.
///
/// It derives serde's traits for the type it names as inherent functions on
/// this one, whose fields mirror that type's, so the declaration still
/// predicts the wire form and there is no disagreement to refuse.
#[test]
fn a_remote_container_is_left_alone() {
let input: syn::DeriveInput = syn::parse2(quote::quote!(
#[serde(remote = "Duration")]
struct DurationDef {
secs: u64,
nanos: u32,
}
))
.expect("the case itself must parse");
// Expansion must succeed outright, for the reason
// `a_wire_form_override_on_an_undescribed_field_is_left_alone` gives.
if let Err(error) = expand_inner(&input) {
panic!("a remote container must expand, and was refused: {error}");
}
}
/// A transparent struct serde writes through one field and reads through
/// another is refused.
///
/// `serde_derive`'s `allow_transparent` writes through the field without
/// `skip_serializing` and reads through the field without
/// `skip_deserializing` or a field-level `default`, never a `PhantomData`.
/// Each row is a declaration serde accepts under `Serialize`, `Deserialize`
/// and both, and names the field each direction picks.
#[test]
fn a_transparent_struct_serde_writes_and_reads_apart_is_refused() {
each_case_is_refused(
vec![
case(
"written through `a`, read through `b`",
quote::quote!(
#[serde(transparent)]
struct Hole {
#[serde(default)]
a: u64,
#[serde(skip_serializing)]
b: String,
}
),
"this struct writes through `a` and reads through `b`",
),
case(
// A `default` member may follow one without it.
"a tuple struct written through one member and read through another",
quote::quote!(
#[serde(transparent)]
struct Pair(#[serde(skip_serializing)] u64, #[serde(default)] u64);
),
"this struct writes through field 1 and reads through field 0",
),
],
expand_inner,
);
}
/// A transparent struct serde picks a single field for is described by that
/// field.
///
/// Where both directions pick one field it is the same one. Where only one
/// direction does, serde refuses the other derive by itself, so the struct
/// compiles with that direction's derive alone and its one field is all
/// serde writes, or reads. Each row names the type the schema must resolve
/// and the type of the field it must not.
#[test]
fn a_transparent_struct_serde_picks_one_field_for_is_described_by_it() {
for (declaration, described, other) in [
// A default on a field neither direction picks changes nothing.
(
quote::quote!(
#[serde(transparent)]
struct Labels {
inner: u64,
#[serde(default, skip)]
extra: String,
}
),
"u64",
"String",
),
// Both skips spelled apart are `skip`.
(
quote::quote!(
#[serde(transparent)]
struct Labels {
#[serde(skip_serializing, skip_deserializing)]
extra: String,
inner: u64,
}
),
"u64",
"String",
),
(
quote::quote!(
#[serde(transparent)]
struct Handle(u64, #[serde(skip)] String);
),
"u64",
"String",
),
// Written through `a`, read through no field: serde refuses
// `Deserialize` and accepts `Serialize` alone.
(
quote::quote!(
#[serde(transparent)]
struct Hole {
#[serde(default)]
a: u64,
#[serde(skip)]
b: String,
}
),
"u64",
"String",
),
// Written through no field, read through `a`: serde refuses
// `Serialize` and accepts `Deserialize` alone.
(
quote::quote!(
#[serde(transparent)]
struct Hole {
#[serde(skip_serializing)]
a: u64,
#[serde(skip_serializing, skip_deserializing)]
b: String,
}
),
"u64",
"String",
),
// Written through `a`, read through both: serde refuses
// `Deserialize` and accepts `Serialize` alone.
(
quote::quote!(
#[serde(transparent)]
struct Hole {
a: u64,
#[serde(skip_serializing)]
b: String,
}
),
"u64",
"String",
),
// Written through both, read through `b`: serde refuses
// `Serialize` and accepts `Deserialize` alone.
(
quote::quote!(
#[serde(transparent)]
struct Hole {
#[serde(skip_deserializing)]
a: u64,
b: String,
}
),
"String",
"u64",
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
let expanded = match expand_inner(&input) {
Ok(tokens) => tokens.to_string(),
Err(error) => {
panic!("a transparent struct with one picked field must expand: {error}")
}
};
assert!(
expanded.contains(&format!("resolve :: < {described} >"))
&& !expanded.contains(&format!("resolve :: < {other} >")),
"the schema must describe the `{described}` field alone: {expanded}"
);
}
}
/// A `PhantomData` a macro passed through a `$t:ty` fragment is still a
/// `PhantomData`.
///
/// rustc hands such a type to the derive inside an invisible group, which
/// `syn` parses as `Type::Group`, and serde's transparent check unwraps it.
/// Unrecognised, the marker would count as a member serde writes and reads,
/// and the derive would describe it and demand `PhantomData<T>: Schema`.
#[test]
fn a_phantom_member_a_macro_wraps_in_a_group_is_not_described() {
let marker =
proc_macro2::Group::new(proc_macro2::Delimiter::None, quote::quote!(PhantomData<T>));
let input: syn::DeriveInput = syn::parse2(quote::quote!(
#[serde(transparent)]
struct Id<T>(u64, #marker);
))
.expect("the case itself must parse");
// Without the group there is nothing here to test.
let syn::Data::Struct(data) = &input.data else {
panic!("the case is a struct");
};
assert!(
data.fields
.iter()
.any(|field| matches!(field.ty, syn::Type::Group(_))),
"the marker did not parse as a `Type::Group`"
);
let expanded = match expand_inner(&input) {
Ok(tokens) => tokens.to_string(),
Err(error) => panic!("a transparent struct beside a marker must expand: {error}"),
};
assert!(
!expanded.contains("PhantomData"),
"the marker reached the expansion: {expanded}"
);
}
/// A transparent struct serde refuses in both directions is serde's to
/// refuse.
///
/// With no single field to write through and none to read through, serde
/// raises its own error for either derive, so a second one here would
/// restate a serde shape rule, for the reason
/// `untagged_on_a_struct_is_left_to_serde` gives.
#[test]
fn a_transparent_struct_serde_refuses_both_ways_is_left_to_serde() {
for declaration in [
quote::quote!(
#[serde(transparent)]
struct Two {
a: u64,
b: String,
}
),
quote::quote!(
#[serde(transparent)]
struct Empty {
#[serde(skip)]
a: u64,
}
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
let Err(error) = expand_inner(&input) else {
continue;
};
assert!(
!error.to_string().contains("`#[serde(transparent)]`"),
"a struct serde refuses both ways drew a second refusal: {error}"
);
}
}
/// A catch-all serde never writes is refused all the same.
///
/// `#[serde(other)]` is not about the variant's own branch:
/// `skip_serializing` keeps the variant out of what serde writes, but
/// deserialization still routes every tag the enum does not name to it, so
/// the schema's closed `oneOf` still disagrees with what the type accepts.
#[test]
fn a_catch_all_on_a_skipped_variant_is_still_refused() {
each_case_is_refused(
vec![case(
"a `#[serde(other)]` catch-all that is never serialized",
quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
id: u64,
},
#[serde(skip_serializing)]
#[serde(other)]
Unknown,
}
),
"`#[serde(other)]` accepts",
)],
expand_inner,
);
}
/// A catch-all on a variant serde never reads is left alone.
///
/// serde draws the fallthrough only from the variants it reads, so
/// `#[serde(other)]` on one it skips on read catches nothing, and the
/// refusal's "accepts every tag this enum does not name" would be false. A
/// lone `skip_deserializing` never reaches the check, since
/// `reject_unread_variant` refuses it first.
#[test]
fn a_catch_all_on_a_variant_serde_never_reads_is_left_alone() {
for declaration in [
quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
id: u64,
},
#[serde(skip)]
#[serde(other)]
Unknown,
}
),
quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
id: u64,
},
#[serde(skip_serializing)]
#[serde(skip_deserializing)]
#[serde(other)]
Unknown,
}
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
if let Err(error) = expand_inner(&input) {
panic!("a catch-all serde never reads must expand: {error}");
}
}
}
/// `skip_serializing_if` is accepted wherever serde may leave the field out
/// in both directions, or never reads it at all.
///
/// Beside an `Option` or a `#[serde(default)]`, an absent field reads as
/// well as it writes, so `required` can leave it out truthfully. A field
/// that is never read is in no schema, so nothing can disagree with it.
#[test]
fn skip_serializing_if_beside_an_option_or_a_default_is_accepted() {
for declaration in [
quote::quote!(
struct Draft {
#[serde(skip_serializing_if = "Option::is_none")]
maybe: Option<u64>,
}
),
quote::quote!(
struct Draft {
#[serde(default, skip_serializing_if = "String::is_empty")]
elided: String,
}
),
quote::quote!(
struct Draft {
#[serde(skip_deserializing, skip_serializing_if = "String::is_empty")]
elided: String,
}
),
// A container `default` fills every missing field from `Default`
// on read, so each field is as absent-tolerant as a field-level
// `default` would make it.
quote::quote!(
#[serde(default)]
struct Draft {
#[serde(skip_serializing_if = "String::is_empty")]
elided: String,
}
),
// A flattened open map: serde reads its absence as an empty map,
// and no flattened field is ever listed in `required`.
quote::quote!(
struct Draft {
id: u64,
#[serde(flatten, skip_serializing_if = "HashMap::is_empty")]
#[schema(open)]
extra: HashMap<String, String>,
}
),
// The same open map with a default, which a flattened field is
// decided without: `#[schema(open)]` alone accepts it.
quote::quote!(
struct Draft {
id: u64,
#[serde(flatten, default, skip_serializing_if = "HashMap::is_empty")]
#[schema(open)]
extra: HashMap<String, String>,
}
),
// A transparent struct is its one field's value, which serde
// writes whatever `skip_serializing_if` says, so there is no
// `required` list for the field to contradict.
quote::quote!(
#[serde(transparent)]
struct Draft {
#[serde(skip_serializing_if = "String::is_empty")]
elided: String,
}
),
// The same rule inside an internally tagged struct variant: an
// `Option` field may be absent both ways.
quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
#[serde(skip_serializing_if = "Option::is_none")]
note: Option<String>,
},
}
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
if let Err(error) = expand_inner(&input) {
panic!("a field serde may omit in both directions must expand: {error}");
}
}
}
/// A flattened field that skips itself on write is refused unless it is
/// `#[schema(open)]`, whatever default it or its struct carries.
///
/// The refusal reads attributes, not types, so a map and a struct meet the
/// same site. A flattened struct is written whole or not at all, and serde
/// ignores `#[serde(default)]` on a flattened field at field and container
/// level alike, so no default covers it. A flattened map is decided by
/// `#[schema(open)]` alone, which the map row below leaves off.
#[test]
fn a_flattened_field_skipped_on_write_is_refused_unless_open() {
each_case_is_refused(
vec![
case(
"`skip_serializing_if` on a flattened struct with no default",
quote::quote!(
struct Draft {
id: u64,
#[serde(flatten, skip_serializing_if = "Audit::is_empty")]
audit: Audit,
}
),
"on a flattened field is refused unless it is `#[schema(open)]`",
),
case(
"`skip_serializing_if` on a flattened struct with a field-level default",
quote::quote!(
struct Wrapper {
id: u64,
#[serde(flatten, default, skip_serializing_if = "Audit::is_empty")]
audit: Audit,
}
),
"on a flattened field is refused unless it is `#[schema(open)]`",
),
case(
"`skip_serializing_if` on a flattened struct under a container default",
quote::quote!(
#[serde(default)]
struct Wrapper {
id: u64,
#[serde(flatten, skip_serializing_if = "Audit::is_empty")]
audit: Audit,
}
),
"on a flattened field is refused unless it is `#[schema(open)]`",
),
case(
"`skip_serializing_if` on a flattened map that is not `#[schema(open)]`",
quote::quote!(
struct Tagged {
id: u64,
#[serde(flatten, skip_serializing_if = "HashMap::is_empty")]
extra: HashMap<String, String>,
}
),
"on a flattened field is refused unless it is `#[schema(open)]`",
),
],
expand_inner,
);
}
/// `skip_serializing_if` on a field of an enum variant follows the rule a
/// struct's field does.
///
/// The refusal walks every variant serde writes, because an internally
/// tagged struct variant is an object with its own `required` list.
#[test]
fn a_variant_field_skipped_on_write_is_refused_like_a_struct_field() {
each_case_is_refused(
vec![case(
"`skip_serializing_if` on a non-`Option` variant field with no default",
quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
#[serde(skip_serializing_if = "String::is_empty")]
note: String,
},
}
),
"still requires it on read",
)],
expand_inner,
);
}
/// A member serde leaves out in one direction only is refused wherever it
/// holds a position or a variant's payload, under each key and tagging.
///
/// One row per key and placement: the ledger's single row proves the site
/// fires, and this proves the walk reaches every placement and names the
/// key that was written.
#[test]
fn every_one_way_member_skip_is_refused() {
each_case_is_refused(
vec![
case(
"`skip_serializing` on a tuple member",
quote::quote!(
struct Pair(u64, #[serde(skip_serializing)] u64);
),
"`skip_serializing` leaves this member out in one direction only",
),
case(
"`skip_deserializing` on a tuple member",
quote::quote!(
struct Pair(#[serde(skip_deserializing)] u64, String);
),
"`skip_deserializing` leaves this member out in one direction only",
),
case(
"`skip_serializing` on a tuple-variant member",
quote::quote!(
enum Reading {
Count(u64, #[serde(skip_serializing)] u64),
}
),
"`skip_serializing` leaves this member out in one direction only",
),
case(
"`skip_deserializing` on a tuple-variant member",
quote::quote!(
enum Reading {
Count(#[serde(skip_deserializing)] u64, u64),
}
),
"`skip_deserializing` leaves this member out in one direction only",
),
case(
"`skip_serializing` on an externally tagged newtype-variant member",
quote::quote!(
enum Reading {
Count(#[serde(skip_serializing)] u64),
}
),
"`skip_serializing` leaves this member out in one direction only",
),
case(
"`skip_deserializing` on an adjacently tagged newtype-variant member",
quote::quote!(
#[serde(tag = "t", content = "c")]
enum Reading {
Count(#[serde(skip_deserializing)] u64),
}
),
"`skip_deserializing` leaves this member out in one direction only",
),
case(
"`skip_serializing` on an internally tagged newtype-variant member",
quote::quote!(
#[serde(tag = "t")]
enum Reading {
Count(#[serde(skip_serializing)] Audit),
}
),
"`skip_serializing` leaves this member out in one direction only",
),
],
expand_inner,
);
}
/// `skip_serializing_if` on a tuple member is refused unless it is the last
/// position and carries `#[serde(default)]`, on a tuple struct and a tuple
/// variant alike.
#[test]
fn every_misplaced_skip_serializing_if_on_a_tuple_member_is_refused() {
each_case_is_refused(
vec![
case(
"`skip_serializing_if` on a member a later position follows",
quote::quote!(
struct Reading(
#[serde(default, skip_serializing_if = "is_zero")] u64,
#[serde(default)] String,
);
),
"`skip_serializing_if` on a tuple member is refused unless",
),
case(
"`skip_serializing_if` on the last member, without a default",
quote::quote!(
struct Reading(u64, #[serde(skip_serializing_if = "is_zero")] u64);
),
"`skip_serializing_if` on a tuple member is refused unless",
),
case(
"`skip_serializing_if` on a tuple-variant member a later position follows",
quote::quote!(
enum Reading {
Count(
#[serde(default, skip_serializing_if = "is_zero")] u64,
#[serde(default)] u64,
),
}
),
"`skip_serializing_if` on a tuple member is refused unless",
),
],
expand_inner,
);
}
/// A skipped member of an adjacently tagged newtype variant is refused
/// unless its type is an `Option`, however the skip is spelt.
///
/// serde writes such a variant as the tag alone and reads it only beside
/// its content, which an `Option` member alone may leave out. A variant
/// serde reads and never writes is described by the same tag-only branch,
/// which serde still refuses to read, so it is refused as well.
#[test]
fn every_skipped_adjacently_tagged_payload_is_refused_unless_optional() {
each_case_is_refused(
vec![
case(
"`skip` on an adjacently tagged newtype-variant member",
quote::quote!(
#[serde(tag = "t", content = "c")]
enum Reading {
Hidden(#[serde(skip)] u64),
}
),
"`skip` leaves out the only member",
),
case(
"`skip_serializing` beside `skip_deserializing` on that member",
quote::quote!(
#[serde(tag = "t", content = "c")]
enum Reading {
Hidden(#[serde(skip_serializing, skip_deserializing)] String),
}
),
"`skip_serializing` leaves out the only member",
),
case(
"`skip` on a member whose path type is not an `Option`",
quote::quote!(
#[serde(tag = "t", content = "c", rename_all = "snake_case")]
enum Reading {
Count(u64),
Hidden(#[serde(skip)] std::vec::Vec<u64>),
}
),
"`skip` leaves out the only member",
),
case(
"`skip` on the member of a variant serde reads and never writes",
quote::quote!(
#[serde(tag = "t", content = "c")]
enum Reading {
Count(u64),
#[serde(skip_serializing)]
Hidden(#[serde(skip)] u64),
}
),
"`skip` leaves out the only member",
),
],
expand_inner,
);
}
/// Every member skip serde honours in both directions expands.
///
/// Each row is a placement the refusal must not reach: a newtype struct,
/// whose skips serde ignores; a member skipped both ways, however it is
/// spelt; a trailing `skip_serializing_if` beside its default, last among
/// the positions even when a skipped member follows it; a newtype
/// variant's `skip_serializing_if`, which serde ignores; a skipped
/// variant; and a transparent struct, which holds no positions.
#[test]
fn a_member_skip_serde_honours_both_ways_is_accepted() {
for declaration in [
quote::quote!(
struct Sku(#[serde(skip_serializing)] u64);
),
quote::quote!(
struct Sku(#[serde(skip_deserializing)] u64);
),
quote::quote!(
struct Sku(#[serde(skip_serializing_if = "is_zero")] u64);
),
quote::quote!(
struct Pair(#[serde(skip)] u64, String);
),
quote::quote!(
struct Pair(
u64,
#[serde(skip_serializing)]
#[serde(skip_deserializing)]
u64,
);
),
quote::quote!(
struct Tally(u64, #[serde(default, skip_serializing_if = "is_zero")] u64);
),
quote::quote!(
struct Tally(
u64,
#[serde(default, skip_serializing_if = "is_zero")] u64,
#[serde(skip)] u64,
);
),
quote::quote!(
enum Reading {
Count(#[serde(skip_serializing_if = "is_zero")] u64),
}
),
quote::quote!(
#[serde(tag = "t")]
enum Reading {
Total(Audit),
#[serde(skip)]
Count(#[serde(skip_serializing)] u64, u64),
}
),
// `default` keeps serde from reading through the second member, so
// the transparent refusal accepts it and only this one could fire.
quote::quote!(
#[serde(transparent)]
struct Handle(u64, #[serde(default, skip_serializing)] u64);
),
// A container `default` fills every missing trailing element.
quote::quote!(
#[serde(default)]
struct Tally(u64, #[serde(skip_serializing_if = "is_zero")] u64);
),
// A skipped newtype variant serde reads back as it writes: an
// `Option` member under adjacent tagging, and any member under
// external or internal tagging.
quote::quote!(
#[serde(tag = "t", content = "c")]
enum Reading {
Hidden(#[serde(skip)] Option<u64>),
}
),
quote::quote!(
enum Reading {
Hidden(#[serde(skip)] u64),
}
),
quote::quote!(
#[serde(tag = "t")]
enum Reading {
Total(Audit),
Hidden(#[serde(skip)] u64),
}
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
if let Err(error) = expand_inner(&input) {
panic!("a member skip serde honours both ways must expand: {error}");
}
}
}
/// A variant skip serde reads through expands, and so does whatever serde
/// never reaches inside a variant it never writes.
///
/// serde's `Serialize` arm for a `skip_serializing` variant errors before it
/// touches a field, so a field's `serialize_with`, any `skip_serializing_if`
/// and a member's lone `skip_serializing` change nothing serde does with the
/// variant, and refusing one would be a false refusal. The two-attribute
/// spelling of a skip both ways is here so it is not read as either half.
#[test]
fn a_variant_skip_serde_reads_through_is_accepted() {
for declaration in [
quote::quote!(
enum Channel {
Web,
#[serde(skip_serializing)]
Fax,
}
),
quote::quote!(
enum Channel {
Web,
#[serde(skip_serializing)]
#[serde(skip_deserializing)]
Fax,
}
),
quote::quote!(
enum Reading {
Total(u64),
#[serde(skip_serializing)]
Count {
#[serde(serialize_with = "as_string")]
count: u64,
},
}
),
quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
at: String,
},
#[serde(skip_serializing)]
Amended {
#[serde(skip_serializing_if = "String::is_empty")]
note: String,
},
}
),
quote::quote!(
enum Reading {
Total(u64),
#[serde(skip_serializing)]
Count(u64, #[serde(skip_serializing)] u64),
}
),
quote::quote!(
enum Reading {
Total(u64),
#[serde(skip_serializing)]
Count(#[serde(skip_serializing_if = "is_zero")] u64, u64),
}
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
if let Err(error) = expand_inner(&input) {
panic!("a variant skip serde reads through must expand: {error}");
}
}
}
/// Inside a variant serde reads and never writes, what serde reads through
/// is refused as it is in a variant serde writes.
#[test]
fn a_read_override_or_skip_inside_a_variant_serde_never_writes_is_refused() {
each_case_is_refused(
vec![
case(
"`deserialize_with` on a field of a variant serde never writes",
quote::quote!(
enum Reading {
Total(u64),
#[serde(skip_serializing)]
Count {
#[serde(deserialize_with = "from_string")]
count: u64,
},
}
),
"`deserialize_with` reads or writes this field",
),
case(
"`with` on a field of a variant serde never writes",
quote::quote!(
enum Reading {
Total(u64),
#[serde(skip_serializing)]
Count {
#[serde(with = "as_string")]
count: u64,
},
}
),
"`with` reads or writes this field",
),
case(
"`deserialize_with` on a variant serde never writes",
quote::quote!(
enum Reading {
Total(u64),
#[serde(skip_serializing, deserialize_with = "from_string")]
Count(u64),
}
),
"`deserialize_with` reads or writes this variant",
),
case(
"`skip_deserializing` alone on a member of a variant serde never writes",
quote::quote!(
enum Reading {
Total(u64),
#[serde(skip_serializing)]
Count(u64, #[serde(skip_deserializing)] u64),
}
),
"`skip_deserializing` leaves this member out in one direction only",
),
],
expand_inner,
);
}
/// A named field serde reads and never writes is refused wherever serde
/// still requires it on read, by the rule `skip_serializing_if` meets.
///
/// `skip_serializing` alone is `skip_serializing_if` with a condition that
/// always holds: serde leaves the field out of every object it writes, and
/// without a default refuses an object without it on read. One row per
/// placement: a struct, a struct variant under each tagging, and a
/// flattened struct, which no default covers.
#[test]
fn a_named_field_serde_requires_but_never_writes_is_refused() {
each_case_is_refused(
vec![
case(
"`skip_serializing` alone on a struct field with no default",
quote::quote!(
struct Draft {
plain: u64,
#[serde(skip_serializing)]
elided: u64,
}
),
"`skip_serializing` lets serde leave this field out of what it writes",
),
case(
"`skip_serializing` alone on an externally tagged variant field",
quote::quote!(
enum Event {
Created {
at: u64,
#[serde(skip_serializing)]
note: u64,
},
}
),
"`skip_serializing` lets serde leave this field out of what it writes",
),
case(
"`skip_serializing` alone on an internally tagged variant field",
quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
at: u64,
#[serde(skip_serializing)]
note: u64,
},
}
),
"`skip_serializing` lets serde leave this field out of what it writes",
),
case(
"`skip_serializing` alone on an adjacently tagged variant field",
quote::quote!(
#[serde(tag = "kind", content = "body")]
enum Event {
Created {
at: u64,
#[serde(skip_serializing)]
note: u64,
},
}
),
"`skip_serializing` lets serde leave this field out of what it writes",
),
case(
"`skip_serializing` alone on a flattened struct, beside a default",
quote::quote!(
struct Wrapper {
id: u64,
#[serde(flatten, default, skip_serializing)]
audit: Audit,
}
),
"`skip_serializing` on a flattened field is refused unless it is \
`#[schema(open)]`",
),
],
expand_inner,
);
}
/// On a named field serde reads and never writes, the overrides serde
/// reads through are refused, as they are inside a variant serde never
/// writes.
#[test]
fn a_read_override_on_a_named_field_serde_never_writes_is_refused() {
each_case_is_refused(
vec![
case(
"`deserialize_with` on a field serde never writes",
quote::quote!(
struct Reading {
total: u64,
#[serde(skip_serializing, default, deserialize_with = "from_string")]
count: u64,
}
),
"`deserialize_with` reads or writes this field",
),
case(
"`with` on a field serde never writes",
quote::quote!(
struct Reading {
total: u64,
#[serde(skip_serializing, default, with = "as_string")]
count: u64,
}
),
"`with` reads or writes this field",
),
],
expand_inner,
);
}
/// A named field serde skips in one direction expands wherever one schema is
/// true of both.
///
/// Read and never written, it is a property `required` leaves out beside an
/// `Option` or a default, a property of a variant serde never writes, and a
/// flattened open map; `serialize_with` on it changes nothing serde does.
/// Written and never read, it is left out of an object that constrains no
/// member it does not name, whatever it writes through. A transparent struct
/// is scanned over the fields serde writes or reads through, and no other.
#[test]
fn a_named_field_serde_skips_in_one_direction_is_accepted() {
for declaration in [
quote::quote!(
struct Draft {
plain: u64,
#[serde(skip_serializing, default)]
elided: u64,
}
),
quote::quote!(
struct Draft {
plain: u64,
#[serde(skip_serializing)]
maybe: Option<u64>,
}
),
quote::quote!(
#[serde(default)]
struct Draft {
plain: u64,
#[serde(skip_serializing)]
elided: u64,
}
),
quote::quote!(
enum Reading {
Total(u64),
#[serde(skip_serializing)]
Count {
#[serde(skip_serializing)]
count: u64,
},
}
),
quote::quote!(
struct Draft {
id: u64,
#[serde(flatten, skip_serializing)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
),
quote::quote!(
struct Reading {
total: u64,
#[serde(skip_serializing, default, serialize_with = "as_string")]
count: u64,
}
),
quote::quote!(
struct Draft {
plain: u64,
#[serde(skip_deserializing)]
stamp: u64,
}
),
quote::quote!(
struct Reading {
total: u64,
#[serde(skip_deserializing, serialize_with = "as_string")]
count: u64,
}
),
// serde writes and reads through `a` alone, so `b` reaches neither
// direction and nothing it reads through can contradict the schema.
quote::quote!(
#[serde(transparent)]
struct Skipped {
a: u64,
#[serde(default, skip_serializing, deserialize_with = "from_string")]
b: u64,
}
),
quote::quote!(
#[serde(transparent)]
struct Handle(
u64,
#[serde(default, skip_serializing, deserialize_with = "from_string")] u64,
);
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
if let Err(error) = expand_inner(&input) {
panic!("a named field one schema describes both ways must expand: {error}");
}
}
}
/// Beside an open flattened field, a field serde never writes, or never
/// writes and never reads, expands.
///
/// Skipped both ways, however it is spelt, the field is in nothing serde
/// writes. An open map serde never reads is in no schema, so it gives the
/// object no `unevaluatedProperties`. Inside a variant serde never writes,
/// serde reads the field into the map, whose value schema the object
/// applies to it.
#[test]
fn a_field_serde_never_writes_beside_an_open_map_is_accepted() {
for declaration in [
quote::quote!(
struct Thing {
id: u64,
#[serde(skip)]
stamp: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
),
quote::quote!(
struct Thing {
id: u64,
#[serde(skip_serializing)]
#[serde(skip_deserializing)]
stamp: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
),
quote::quote!(
struct Thing {
id: u64,
#[serde(flatten, skip_deserializing)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
),
quote::quote!(
enum Event {
Now(u64),
#[serde(skip_serializing)]
Queued {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
},
}
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
if let Err(error) = expand_inner(&input) {
panic!("a field serde never writes beside an open map must expand: {error}");
}
}
}
/// The `AdmitsAny` and `OpenMap` witnesses the expansion asserts for the
/// input, in that order.
fn open_witnesses_in(declaration: TokenStream2) -> (usize, usize) {
let input: DeriveInput = syn::parse2(declaration).expect("the case itself must parse");
let expansion = match expand_inner(&input) {
Ok(expansion) => expansion.to_string(),
Err(error) => panic!("the case must expand: {error}"),
};
(
expansion.matches("admits_any ::").count(),
expansion.matches("is_open_map ::").count(),
)
}
/// Beside a named field serde writes and never reads, an open flattened
/// field is bounded by `AdmitsAny` rather than refused, in every object
/// serde writes.
///
/// Left out of the schema, the field is a member the object does not name,
/// so what refuses it is the `unevaluatedProperties` the open field's type
/// hoists, if it hoists one: `Unchecked` does not, a map does. That is the
/// type's answer, so the rule is a bound. One row per placement, as for the
/// closed object: a field, a flattened struct, and a struct variant under
/// the tagging that nests it and the one that does not.
#[test]
fn a_field_serde_never_reads_bounds_the_open_field_beside_it_by_admits_any() {
for declaration in [
quote::quote!(
struct Thing {
id: u64,
#[serde(skip_deserializing)]
stamp: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
),
quote::quote!(
struct Thing {
id: u64,
#[serde(flatten, skip_deserializing)]
audit: Audit,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
),
quote::quote!(
enum Event {
Created {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
},
}
),
quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
},
}
),
] {
assert_eq!(open_witnesses_in(declaration), (1, 0));
}
// Without such a field beside it, the open field answers to `OpenMap`
// alone, and so does one in a variant serde never writes.
assert_eq!(
open_witnesses_in(quote::quote!(
struct Thing {
id: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
)),
(0, 1)
);
assert_eq!(
open_witnesses_in(quote::quote!(
enum Event {
Now(u64),
#[serde(skip_serializing)]
Queued {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
},
}
)),
(0, 1)
);
}
/// A `#[serde(transparent)]` struct is its one field's value, with no
/// object for an open field's `unevaluatedProperties` to reach a field
/// serde never reads in, so the open field keeps its `OpenMap` bound alone.
///
/// serde writes this one through both fields, which serde itself refuses
/// for `Serialize`, and reads it through `extra` alone, which is the
/// derive it accepts; `reject_transparent_without_one_field` leaves the
/// disagreement to serde, so the struct reaches the witnesses.
#[test]
fn a_transparent_struct_bounds_no_open_field_by_admits_any() {
assert_eq!(
open_witnesses_in(quote::quote!(
#[serde(transparent)]
struct Thing {
#[serde(skip_deserializing)]
stamp: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
)),
(0, 1)
);
}
/// A named field serde writes and never reads is refused in every object
/// `deny_unknown_fields` closes, since the closed object refuses what serde
/// writes of it. One row per placement: a struct, and a struct variant
/// under each tagging.
#[test]
fn a_field_serde_never_reads_in_a_closed_object_is_refused() {
let expects = "`#[serde(deny_unknown_fields)]` gives this object";
each_case_is_refused(
vec![
case(
"`skip_deserializing` alone on a field of a closed struct",
quote::quote!(
#[serde(deny_unknown_fields)]
struct Thing {
id: u64,
#[serde(skip_deserializing)]
stamp: u64,
}
),
expects,
),
case(
"`skip_deserializing` alone on a flattened struct in a closed struct",
quote::quote!(
#[serde(deny_unknown_fields)]
struct Thing {
id: u64,
#[serde(flatten, skip_deserializing)]
audit: Audit,
}
),
expects,
),
case(
"`skip_deserializing` alone in a closed externally tagged variant",
quote::quote!(
#[serde(deny_unknown_fields)]
enum Event {
Created {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
},
}
),
expects,
),
case(
"`skip_deserializing` alone in a closed internally tagged variant",
quote::quote!(
#[serde(deny_unknown_fields, tag = "kind")]
enum Event {
Created {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
},
}
),
expects,
),
case(
"`skip_deserializing` alone in a closed adjacently tagged variant",
quote::quote!(
#[serde(deny_unknown_fields, tag = "kind", content = "value")]
enum Event {
Created {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
},
}
),
expects,
),
],
expand_inner,
);
}
/// An open map is refused wherever serde reads it into an object
/// `deny_unknown_fields` closes, including a variant serde never writes,
/// since the object is closed on read alone. `open` on a field that is not
/// flattened keeps the diagnostic naming that mistake.
#[test]
fn an_open_map_in_a_closed_object_is_refused_in_every_group() {
each_case_is_refused(
vec![
case(
"an open map in a closed internally tagged variant",
quote::quote!(
#[serde(deny_unknown_fields, tag = "kind")]
enum Event {
Created {
at: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
},
}
),
"reads the map empty",
),
case(
"an open map in a closed variant serde never writes",
quote::quote!(
#[serde(deny_unknown_fields)]
enum Event {
Now(u64),
#[serde(skip_serializing)]
Queued {
at: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
},
}
),
"reads the map empty",
),
case(
"`open` on a field that is not flattened, in a closed object",
quote::quote!(
#[serde(deny_unknown_fields)]
struct Thing {
#[schema(open)]
extra: BTreeMap<String, String>,
}
),
"only a flattened field has anything",
),
],
expand_inner,
);
}
/// Under `deny_unknown_fields`, what serde reads and writes alike, or
/// neither, expands: a field it skips both ways, a flattened struct, an
/// `alias` on a field it never reads, and a `#[serde(transparent)]` struct,
/// whose wire form is its one field's value rather than a closed object.
/// So does an `alias` on a field it reads, in a struct and in a variant it
/// never writes alike, since the closed object names every alias.
#[test]
fn a_closed_object_serde_agrees_with_expands() {
for declaration in [
quote::quote!(
#[serde(deny_unknown_fields)]
struct Thing {
#[serde(alias = "identifier")]
id: u64,
}
),
quote::quote!(
#[serde(deny_unknown_fields)]
enum Event {
Now(u64),
#[serde(skip_serializing)]
Queued {
#[serde(alias = "when")]
at: u64,
},
}
),
quote::quote!(
#[serde(deny_unknown_fields)]
struct Thing {
id: u64,
#[serde(skip)]
cache: u64,
#[serde(flatten)]
audit: Audit,
}
),
quote::quote!(
#[serde(deny_unknown_fields)]
struct Thing {
id: u64,
#[serde(skip, alias = "cached")]
cache: u64,
}
),
quote::quote!(
#[serde(deny_unknown_fields, transparent)]
struct Thing {
#[serde(alias = "identifier")]
id: u64,
}
),
] {
let input: syn::DeriveInput =
syn::parse2(declaration).expect("the case itself must parse");
if let Err(error) = expand_inner(&input) {
panic!("a closed object serde agrees with must expand: {error}");
}
}
}
/// A shape `deny_unknown_fields` closes does not claim to be flattenable,
/// and one it leaves open still does.
///
/// A closed object's `additionalProperties` or `unevaluatedProperties`
/// would, inside the `allOf` one level up, refuse the members the outer
/// object declared itself. serde leaves an internally tagged unit variant
/// open. An externally tagged enum claims nothing either way, since its
/// object branches are closed without the attribute.
#[test]
fn a_closed_shape_does_not_claim_flatten() {
assert!(!claims_flatten(quote::quote!(
#[serde(deny_unknown_fields)]
struct Audit {
at: String,
}
)));
assert!(!claims_flatten(quote::quote!(
#[serde(deny_unknown_fields, tag = "kind")]
enum Shape {
Circle { radius: f64 },
Empty,
}
)));
assert!(!claims_flatten(quote::quote!(
#[serde(deny_unknown_fields, tag = "kind", content = "value")]
enum Payload {
Number(u32),
}
)));
assert!(claims_flatten(quote::quote!(
#[serde(deny_unknown_fields, tag = "kind")]
enum Marker {
On,
Off,
}
)));
assert!(!claims_flatten(quote::quote!(
#[serde(deny_unknown_fields)]
enum Command {
Move { x: u64 },
}
)));
}
/// Whether the expansion claims `kynos::schema::Flatten` for the input.
///
/// Read off the emitted tokens rather than by calling the predicate, so
/// what is asserted is the implementation a user receives. `to_string` on a
/// `TokenStream` separates every token with a space, which is why the
/// needle is spelt out that way.
fn claims_flatten(declaration: TokenStream2) -> bool {
let input: DeriveInput = syn::parse2(declaration).expect("the case itself must parse");
let expansion = expand_inner(&input).expect("the case itself must expand");
expansion
.to_string()
.contains(":: kynos :: schema :: Flatten for")
}
/// The shapes whose description is an object naming its own members.
///
/// A closed enumeration, and the one this change introduced: `Flatten` is
/// a claim, so a shape that gets the implementation without naming its
/// members is a lie the compiler then trusts. Each arm of the decision is
/// asserted from both sides, because a predicate that returned `true`
/// everywhere would pass every positive case on its own.
#[test]
fn only_a_shape_naming_its_members_claims_flatten() {
// A struct: named fields name them, and no other shape does.
assert!(claims_flatten(quote::quote!(
struct Audit {
at: String,
}
)));
assert!(!claims_flatten(quote::quote!(
struct Sku(String);
)));
assert!(!claims_flatten(quote::quote!(
struct Span(u32, u32);
)));
assert!(!claims_flatten(quote::quote!(
struct Marker;
)));
// Adjacently tagged: every branch is an object of a tag property and a
// content property, whatever the variant holds.
assert!(claims_flatten(quote::quote!(
#[serde(tag = "kind", content = "value")]
enum Payload {
Number(u32),
Named { width: u32 },
Nothing,
}
)));
// Internally tagged: a named or unit variant becomes an object naming
// its own members plus the tag. A newtype variant composes with
// whatever its payload resolves to, which is the unknown the trait
// exists to refuse.
assert!(claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Shape {
Circle { radius: f64 },
Point,
}
)));
assert!(!claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Shape {
Circle { radius: f64 },
Raw(String),
}
)));
// Externally tagged: an object branch admits its variant key alone,
// which inside an `allOf` one level up would refuse the members the
// outer object declared itself; a unit variant is a bare string.
assert!(!claims_flatten(quote::quote!(
enum Event {
Created { at: String },
Renamed(String),
}
)));
assert!(!claims_flatten(quote::quote!(
enum Event {
Created { at: String },
Deleted,
}
)));
// Every variant a unit is the compact `enum` of names, which is a
// string schema and not an object at all.
assert!(!claims_flatten(quote::quote!(
enum Currency {
Gbp,
Jpy,
}
)));
// A skipped variant reaches no branch, so it cannot disqualify one.
assert!(claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
at: String,
},
#[serde(skip)]
Raw(String),
}
)));
// An enum with no branch at all describes nothing to flatten.
assert!(!claims_flatten(quote::quote!(
enum Never {}
)));
assert!(!claims_flatten(quote::quote!(
#[serde(tag = "kind", content = "value")]
enum Never {}
)));
assert!(!claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Never {}
)));
}
/// A container that is itself open does not claim to be flattenable.
///
/// The subtle one, and the defect re-entering by the back door: an open
/// container carries `unevaluatedProperties` of its own, and one level up
/// that keyword sits inside an `allOf` branch where it reaches the outer
/// object's own properties -- exactly what this change exists to stop.
#[test]
fn an_open_container_does_not_claim_flatten() {
// The same shape without the attribute does claim it, so the case
// isolates the attribute rather than the shape.
assert!(claims_flatten(quote::quote!(
struct Thing {
id: u64,
#[serde(flatten)]
audit: Audit,
}
)));
assert!(!claims_flatten(quote::quote!(
struct Thing {
id: u64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
}
)));
// And through a variant, which is a field group like any other.
assert!(!claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Shape {
Circle {
radius: f64,
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
},
}
)));
}
/// A transparent struct does not claim to be flattenable.
///
/// serde writes a `#[serde(transparent)]` struct as its one field's value,
/// so what flattening it contributes is that field's members, not the
/// struct's. Named fields are the shape of the declaration and say nothing
/// about the wire, and a transparent wrapper over a map would otherwise
/// carry the map straight past the bound.
#[test]
fn a_transparent_struct_does_not_claim_flatten() {
// The same declaration without the attribute does claim it, so the
// case isolates the attribute rather than the shape.
assert!(claims_flatten(quote::quote!(
struct Labels {
inner: BTreeMap<String, String>,
}
)));
assert!(!claims_flatten(quote::quote!(
#[serde(transparent)]
struct Labels {
inner: BTreeMap<String, String>,
}
)));
}
/// A skipped variant cannot cost an enum its claim to Flatten.
///
/// serde never writes a `#[serde(skip)]` variant, so the derive describes no
/// branch for it and nothing its fields declare reaches the schema. An open
/// field inside one is therefore not an open member of the enum.
#[test]
fn a_skipped_variant_does_not_disqualify_the_enum() {
// The same variant unskipped does disqualify it, so the case isolates
// the skip rather than the shape.
assert!(!claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
at: String,
},
Internal {
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
},
}
)));
assert!(claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
at: String,
},
#[serde(skip)]
Internal {
#[serde(flatten)]
#[schema(open)]
extra: BTreeMap<String, String>,
},
}
)));
}
/// A variant serde reads and never writes has a branch, so it decides the
/// claim to Flatten as a variant serde writes does.
#[test]
fn a_variant_serde_only_reads_counts_toward_the_flatten_claim() {
assert!(!claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Shape {
Circle {
radius: f64,
},
#[serde(skip_serializing)]
Raw(Audit),
}
)));
// Skipped both ways, the same newtype variant reaches no branch, so the
// case isolates the direction rather than the shape.
assert!(claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Shape {
Circle {
radius: f64,
},
#[serde(skip_serializing)]
#[serde(skip_deserializing)]
Raw(Audit),
}
)));
}
/// A shape carrying a named field serde writes and never reads, where serde
/// writes it beside the members it contributes, does not claim Flatten.
///
/// The field is left out of the schema, so one level up it is a member the
/// flattened schema does not name, and an open map beside it refuses what
/// serde writes. A struct and an internally tagged struct variant serde
/// writes put the field there. An adjacently tagged variant nests it under
/// the content key, and a variant serde never writes writes nothing, so
/// neither withdraws the claim.
#[test]
fn a_field_serde_never_reads_withdraws_the_flatten_claim() {
assert!(!claims_flatten(quote::quote!(
struct Stamped {
id: u64,
#[serde(skip_deserializing)]
stamp: u64,
}
)));
// Skipped both ways, or only never written, the same field leaves the
// claim, so the case isolates the direction rather than the shape.
assert!(claims_flatten(quote::quote!(
struct Stamped {
id: u64,
#[serde(skip)]
stamp: u64,
}
)));
assert!(claims_flatten(quote::quote!(
struct Stamped {
id: u64,
#[serde(skip_serializing, default)]
stamp: u64,
}
)));
assert!(!claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
},
}
)));
assert!(claims_flatten(quote::quote!(
#[serde(tag = "kind")]
enum Event {
Created {
at: u64,
},
#[serde(skip_serializing)]
Amended {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
},
}
)));
assert!(claims_flatten(quote::quote!(
#[serde(tag = "kind", content = "data")]
enum Event {
Created {
at: u64,
#[serde(skip_deserializing)]
stamp: u64,
},
}
)));
}
/// How many `Flatten` witnesses the expansion asserts for the input, read
/// off the emitted tokens as [`claims_flatten`] reads its claim.
fn flatten_witnesses_in(declaration: TokenStream2) -> usize {
let input: DeriveInput = syn::parse2(declaration).expect("the case itself must parse");
let expansion = expand_inner(&input).expect("the case itself must expand");
expansion.to_string().matches("is_flattenable ::").count()
}
/// A variant serde reads and never writes composes what serde reads, so its
/// payload and its flattened fields answer to the bound a written variant's
/// do.
#[test]
fn a_variant_serde_only_reads_bounds_what_it_composes() {
assert_eq!(
flatten_witnesses_in(quote::quote!(
#[serde(tag = "kind")]
enum Shape {
Circle {
radius: f64,
},
#[serde(skip_serializing)]
Raw(Audit),
}
)),
1
);
assert_eq!(
flatten_witnesses_in(quote::quote!(
enum Event {
Created {
at: String,
},
#[serde(skip_serializing)]
Amended {
at: String,
#[serde(flatten)]
audit: Audit,
},
}
)),
1
);
assert_eq!(
flatten_witnesses_in(quote::quote!(
#[serde(tag = "kind")]
enum Shape {
Circle {
radius: f64,
},
#[serde(skip)]
Raw(Audit),
}
)),
0
);
}
}
mod api_error {
use super::{Case, case, each_case_is_refused, every_diagnostic_has_a_case};
use crate::derive::api_error::expand_inner;
fn ledger() -> Vec<Case> {
vec![
case(
"a union",
quote::quote!(
union StoreError {
a: u32,
}
),
"cannot describe a union",
),
case(
"a status on the enum, where variants answer with their own",
quote::quote!(
#[problem(status = 404)]
enum StoreError {
#[problem(status = 404)]
NotFound,
}
),
"a status belongs on each variant",
),
case(
"a variant that never says what status it produces",
quote::quote!(
enum StoreError {
NotFound,
}
),
"does not say what status it produces",
),
case(
"a struct that never says what status it produces",
quote::quote!(
struct StoreError;
),
"does not say what status it produces",
),
case(
"a status outside the range a problem detail may carry",
quote::quote!(
#[problem(status = 200)]
struct StoreError;
),
"its status is between",
),
case(
"two statuses on one error, when a response has one",
quote::quote!(
#[problem(status = 404, status = 410)]
struct StoreError;
),
"already declares a status",
),
case(
"`base` on a variant, when it is the prefix the type shares",
quote::quote!(
enum StoreError {
#[problem(status = 404, base = "https://example.com/")]
NotFound,
}
),
"belongs on the type",
),
case(
"`extension` on the type, when it marks a field",
quote::quote!(
#[problem(status = 404, extension)]
struct StoreError;
),
"belongs on a field",
),
case(
"a key outside the problem grammar",
quote::quote!(
#[problem(status = 404, titel = "User not found")]
struct StoreError;
),
"is not part of the `#[problem(...)]` grammar",
),
case(
"an extension on a field with no name to publish it under",
quote::quote!(
enum StoreError {
#[problem(status = 404)]
NotFound(#[problem(extension)] u64),
}
),
"published under its field's name",
),
]
}
#[test]
fn each_case_raises_the_diagnostic_it_names() {
each_case_is_refused(ledger(), expand_inner);
}
#[test]
fn every_api_error_diagnostic_has_a_case() {
every_diagnostic_has_a_case("api_error.rs", include_str!("api_error.rs"), ledger().len());
}
/// Every URI a declaration resolves reaches the emitted responses.
///
/// What is checked here is *resolution*: an explicit `type`, a slug hung
/// under `base`, and two variants sharing a status. Whether the narrowing
/// is well-formed is `crates/kynos/tests/derives.rs`'s, which reads the
/// emitted schema; this reads the tokens, so it names the failing URI
/// without a compile.
///
/// It deliberately does not assert *which* helper the expansion calls. A
/// path is not a behaviour, and pinning one here would redden this test
/// for a move that no document could observe.
#[test]
fn the_declared_type_reaches_the_emitted_responses() {
let input: syn::DeriveInput = syn::parse2(quote::quote!(
#[problem(base = "https://errors.example.com/")]
enum StoreError {
#[problem(status = 404, title = "User not found")]
NotFound,
#[problem(status = 404)]
TenantMissing,
#[problem(status = 409, type = "https://errors.example.com/email-taken")]
Conflict,
}
))
.expect("the case itself must parse");
let expansion = expand_inner(&input)
.expect("a well-formed declaration expands")
.to_string();
for uri in [
"https://errors.example.com/not-found",
"https://errors.example.com/tenant-missing",
"https://errors.example.com/email-taken",
] {
assert!(
expansion.contains(uri),
"`{uri}` never reached the emitted responses: {expansion}"
);
}
}
}
mod reply {
use super::{Case, case, each_case_is_refused, every_diagnostic_has_a_case};
use crate::derive::reply::expand_inner;
fn ledger() -> Vec<Case> {
vec![
case(
"a struct, when a reply is a closed set of responses",
quote::quote!(
struct CreateReply;
),
"closed set of responses",
),
case(
"a union",
quote::quote!(
union CreateReply {
a: u32,
}
),
"needs an enum",
),
case(
"a status on the enum, where variants answer with their own",
quote::quote!(
#[reply(status = 200)]
enum CreateReply {
#[reply(status = 200)]
Ok(u32),
}
),
"a status belongs on each variant",
),
case(
"a variant that never says what status it produces",
quote::quote!(
enum CreateReply {
Ok(u32),
}
),
"does not say what status it produces",
),
case(
"two variants answering with one status",
quote::quote!(
enum UploadReply {
#[reply(status = 202)]
Queued(u32),
#[reply(status = 202)]
AlreadyQueued(u32),
}
),
"already answers with",
),
case(
"a status outside the range a handler may answer with",
quote::quote!(
enum CreateReply {
#[reply(status = 99)]
Ok(u32),
}
),
"its status is between",
),
case(
"two statuses on one variant",
quote::quote!(
enum CreateReply {
#[reply(status = 200, status = 201)]
Ok(u32),
}
),
"already declares a status",
),
case(
"a key outside the reply grammar",
quote::quote!(
enum CreateReply {
#[reply(status = 200, nonsense = "x")]
Ok(u32),
}
),
"is not part of the `#[reply(...)]` grammar",
),
case(
"a struct variant, when a body is one described type",
quote::quote!(
enum CreateReply {
#[reply(status = 201)]
Created { id: u32, revision: u32 },
}
),
"carries its response body",
),
]
}
#[test]
fn each_case_raises_the_diagnostic_it_names() {
each_case_is_refused(ledger(), expand_inner);
}
#[test]
fn every_reply_diagnostic_has_a_case() {
every_diagnostic_has_a_case("reply.rs", include_str!("reply.rs"), ledger().len());
}
}
mod security_scheme {
use super::{Case, case, each_case_is_refused, every_diagnostic_has_a_case};
use crate::derive::security_scheme::expand_inner;
fn ledger() -> Vec<Case> {
vec![
case(
"a scheme that never says what kind it is",
quote::quote!(
struct Bearer;
),
"must say what kind it is",
),
case(
"two kinds, when a scheme has exactly one",
quote::quote!(
#[security(bearer, basic)]
struct Bearer;
),
"exactly one kind",
),
case(
"a key outside the security grammar",
quote::quote!(
#[security(nonsense)]
struct Bearer;
),
"is not part of the `#[security(...)]` grammar",
),
case(
"an API key that never says where it travels",
quote::quote!(
#[security(api_key(name = "X-Api-Key"))]
struct ApiKey;
),
"must say where it travels",
),
case(
"an API key travelling somewhere it cannot",
quote::quote!(
#[security(api_key(in = "path", name = "key"))]
struct ApiKey;
),
"not `path`",
),
case(
"an API key that never says which field carries it",
quote::quote!(
#[security(api_key(in = "header"))]
struct ApiKey;
),
"must say which field carries it",
),
case(
"an API key claiming a header the specification reserves",
quote::quote!(
#[security(api_key(in = "header", name = "authorization"))]
struct ApiKey;
),
"must not be declared as a parameter",
),
]
}
/// The refusals the `oauth2` flow grammar adds.
///
/// A second function rather than more rows in the first: the two are read
/// together everywhere below, and one list of every diagnostic this derive
/// raises had outgrown what a reader can hold — and what Clippy will accept.
fn oauth2_ledger() -> Vec<Case> {
vec![
case(
"an OAuth 2.0 scheme declaring no flow at all",
quote::quote!(
#[security(oauth2(metadata_url = "https://auth.example.com/meta"))]
struct Delegated;
),
"must declare at least one flow",
),
case(
"a flow OAuth 2.0 does not define",
quote::quote!(
#[security(oauth2(magic_link(token_url = "https://auth.example.com/token")))]
struct Delegated;
),
"is not an OAuth 2.0 flow",
),
case(
"a flow missing a URL its own grant needs",
quote::quote!(
#[security(oauth2(authorization_code(
token_url = "https://auth.example.com/token"
)))]
struct Delegated;
),
"authorization_url",
),
case(
"a carrier setting that is not the one word it takes",
quote::quote!(
#[security(bearer)]
#[security(carrier = automatic)]
struct Bearer;
),
"takes only `manual`",
),
case(
"one flow declared twice",
quote::quote!(
#[security(oauth2(
client_credentials(token_url = "https://auth.example.com/a"),
client_credentials(token_url = "https://auth.example.com/b"),
))]
struct Delegated;
),
"already declared",
),
]
}
/// The two diagnostics that fire only where the document model has no field
/// to hold the answer.
///
/// Under `openapi32` both constructs are legal, so neither can be provoked
/// and neither has a row. The count below adds them back, which is what
/// keeps the ledger honest in both builds rather than in the one that
/// happens to run first.
#[cfg(not(feature = "openapi32"))]
fn version_gated_ledger() -> Vec<Case> {
vec![
case(
"a device authorization flow, which only 3.2 defines",
quote::quote!(
#[security(oauth2(device_authorization(
device_authorization_url = "https://auth.example.com/device",
token_url = "https://auth.example.com/token"
)))]
struct Delegated;
),
"openapi32",
),
case(
"an authorization server metadata URL, which only 3.2 carries",
quote::quote!(
#[security(oauth2(
client_credentials(token_url = "https://auth.example.com/token"),
metadata_url = "https://auth.example.com/meta",
))]
struct Delegated;
),
"openapi32",
),
case(
"a deprecation, which only 3.2 has a field for",
quote::quote!(
#[security(http(scheme = "bearer"), deprecated)]
struct Legacy;
),
"openapi32",
),
]
}
#[cfg(feature = "openapi32")]
fn version_gated_ledger() -> Vec<Case> {
Vec::new()
}
/// How many diagnostics this build cannot provoke.
///
/// Three, under `openapi32`: the constructs they refuse are legal there.
const UNREACHABLE_HERE: usize = if cfg!(feature = "openapi32") { 3 } else { 0 };
#[test]
fn each_case_raises_the_diagnostic_it_names() {
each_case_is_refused(ledger(), expand_inner);
each_case_is_refused(oauth2_ledger(), expand_inner);
each_case_is_refused(version_gated_ledger(), expand_inner);
}
#[test]
fn every_security_scheme_diagnostic_has_a_case() {
every_diagnostic_has_a_case(
"security_scheme.rs",
include_str!("security_scheme.rs"),
ledger().len()
+ oauth2_ledger().len()
+ version_gated_ledger().len()
+ UNREACHABLE_HERE,
);
}
/// A declared flow reaches the expansion.
///
/// The defect this closes: `of_kind` built `OAuthFlows::default()`
/// unconditionally and `check_kind` sent every flow to `skip_value`, so
/// `#[security(oauth2(authorization_code(..)))]` described a scheme with no
/// flows at all — and `examples/security_schemes.rs` shipped exactly that,
/// emitting `{"type":"oauth2","flows":{}}` while presenting itself as the
/// demonstration of delegated authorization.
///
/// `kynos-macros` cannot depend on `kynos`, so the assertion is on the
/// tokens rather than on the description they build;
/// `crates/kynos/tests/derives.rs` is where the expansion is compiled.
#[test]
fn a_declared_flow_reaches_the_expansion() {
let input: syn::DeriveInput = syn::parse_quote!(
#[security(oauth2(authorization_code(
authorization_url = "https://auth.example.com/authorize",
token_url = "https://auth.example.com/token",
refresh_url = "https://auth.example.com/token",
scopes("users:read", "users:write"),
)))]
struct Delegated;
);
let expanded = expand_inner(&input)
.expect("a well-formed oauth2 scheme")
.to_string();
for expected in [
"with_authorization_code",
"https://auth.example.com/authorize",
"https://auth.example.com/token",
"users:read",
"users:write",
] {
assert!(
expanded.contains(expected),
"the expansion never mentions {expected:?}: {expanded}"
);
}
}
}
mod provider {
use super::{Case, case, each_case_is_refused, every_diagnostic_has_a_case};
use crate::derive::provider::expand_inner;
fn ledger() -> Vec<Case> {
vec![
case(
"two fields of one type, which a handler could not tell apart",
quote::quote!(
struct App {
primary: Pool,
replica: Pool,
}
),
"are both",
),
// Two fields, because a lone type-parameter field has no sibling
// implementation to overlap and is left to coherence.
case(
"a field typed by one of the context's own type parameters",
quote::quote!(
struct App<T> {
pool: Pool,
value: T,
}
),
"own type parameters",
),
]
}
#[test]
fn each_case_raises_the_diagnostic_it_names() {
each_case_is_refused(ledger(), expand_inner);
}
#[test]
fn every_provider_diagnostic_has_a_case() {
every_diagnostic_has_a_case("provider.rs", include_str!("provider.rs"), ledger().len());
}
}
mod headers {
use super::{Case, case, each_case_is_refused, every_diagnostic_has_a_case};
use crate::derive::headers::expand_inner;
fn ledger() -> Vec<Case> {
vec![case(
"a header the framework already negotiates",
quote::quote!(
struct Negotiation {
accept: String,
}
),
"must not be declared as a header parameter",
)]
}
#[test]
fn each_case_raises_the_diagnostic_it_names() {
each_case_is_refused(ledger(), expand_inner);
}
#[test]
fn every_headers_diagnostic_has_a_case() {
every_diagnostic_has_a_case("headers.rs", include_str!("headers.rs"), ledger().len());
}
}
mod tag {
use super::{Case, each_case_is_refused, every_diagnostic_has_a_case};
use crate::derive::tag::expand_inner;
/// The 3.2-only members, which a 3.1 build refuses.
///
/// Empty under `openapi32`, where all three are legal — the same shape
/// [`super::security_scheme`] uses, and for the same reason: a diagnostic
/// that only one build can provoke still has to be counted in both.
#[cfg(not(feature = "openapi32"))]
fn version_gated_ledger() -> Vec<Case> {
use super::case;
vec![
case(
"a summary, which only 3.2 gives a tag",
quote::quote!(
#[tag(summary = "Everything about orders")]
struct Orders;
),
"openapi32",
),
case(
"a kind, which only 3.2 gives a tag",
quote::quote!(
#[tag(kind = "nav")]
struct Orders;
),
"openapi32",
),
case(
"a parent, which only 3.2 gives a tag",
quote::quote!(
#[tag(parent = Catalogue)]
struct Orders;
),
"openapi32",
),
]
}
#[cfg(feature = "openapi32")]
fn version_gated_ledger() -> Vec<Case> {
Vec::new()
}
/// How many diagnostics this build cannot provoke.
///
/// One, under `openapi32`: the three members share a single site, and what
/// it refuses is legal there.
const UNREACHABLE_HERE: usize = if cfg!(feature = "openapi32") { 1 } else { 0 };
#[test]
fn each_case_raises_the_diagnostic_it_names() {
each_case_is_refused(version_gated_ledger(), expand_inner);
}
#[test]
fn every_tag_diagnostic_has_a_case() {
// The three members are refused from one `syn::Error::new`, so the
// ledger's three cases meet one site. Counting the *site* is the point:
// a fourth 3.2 member added without a case fails here.
let covered = usize::from(!version_gated_ledger().is_empty()) + UNREACHABLE_HERE;
every_diagnostic_has_a_case("tag.rs", include_str!("tag.rs"), covered);
}
}