vespera_macro 0.3.0

Procedural macros for Vespera - automatic route discovery and OpenAPI generation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
use rstest::rstest;

use super::*;

#[test]
fn test_parse_struct_to_schema_required_optional() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r"
            struct User {
                id: i32,
                name: Option<String>,
            }
        ",
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let props = schema.properties.as_ref().unwrap();
    assert!(props.contains_key("id"));
    assert!(props.contains_key("name"));
    assert!(
        schema
            .required
            .as_ref()
            .unwrap()
            .contains(&"id".to_string())
    );
    assert!(
        !schema
            .required
            .as_ref()
            .unwrap()
            .contains(&"name".to_string())
    );
}

#[test]
fn test_parse_struct_to_schema_rename_all_and_field_rename() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r#"
            #[serde(rename_all = "camelCase")]
            struct Profile {
                #[serde(rename = "id")]
                user_id: i32,
                display_name: Option<String>,
            }
        "#,
    )
    .unwrap();

    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let props = schema.properties.as_ref().expect("props missing");
    assert!(props.contains_key("id")); // field-level rename wins
    assert!(props.contains_key("displayName")); // rename_all applied
    let required = schema.required.as_ref().expect("required missing");
    assert!(required.contains(&"id".to_string()));
    assert!(!required.contains(&"displayName".to_string())); // Option makes it optional
}

#[rstest]
#[case("struct Wrapper(i32);")]
#[case("struct Empty;")]
fn test_parse_struct_to_schema_tuple_and_unit_structs(#[case] struct_src: &str) {
    let struct_item: syn::ItemStruct = syn::parse_str(struct_src).unwrap();
    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    assert!(schema.properties.is_none());
    assert!(schema.required.is_none());
}

#[test]
fn test_parse_struct_to_schema_serde_transparent_named_wrapper_uses_inner_schema() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r"
            #[serde(transparent)]
            struct Wrapper {
                value: Box<String>,
            }
        ",
    )
    .unwrap();

    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    assert_eq!(schema.schema_type, Some(SchemaType::String));
    assert!(schema.properties.is_none());
}

#[test]
fn test_parse_struct_to_schema_schema_ref_override() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r#"
            #[schema(ref = "UserSchema", nullable)]
            struct Wrapper {
                value: Option<String>,
            }
        "#,
    )
    .unwrap();

    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    assert_eq!(
        schema.ref_path.as_deref(),
        Some("#/components/schemas/UserSchema")
    );
    assert_eq!(schema.nullable, Some(true));
}

// Test struct with skip field
#[test]
fn test_parse_struct_to_schema_with_skip_field() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r"
            struct User {
                id: i32,
                #[serde(skip)]
                internal_data: String,
                name: String,
            }
        ",
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let props = schema.properties.as_ref().unwrap();
    assert!(props.contains_key("id"));
    assert!(props.contains_key("name"));
    assert!(!props.contains_key("internal_data")); // Should be skipped
}

#[test]
fn test_parse_struct_to_schema_skip_takes_precedence_over_skip_serializing_if() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r#"
            struct User {
                id: i32,
                #[serde(skip, skip_serializing_if = "Option::is_none")]
                email2: Option<String>,
                name: String,
            }
        "#,
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let props = schema.properties.as_ref().unwrap();
    assert!(props.contains_key("id"));
    assert!(props.contains_key("name"));
    assert!(!props.contains_key("email2"));
}

// Test struct with default and skip_serializing_if
// Required is determined solely by nullability (Option<T>), not by defaults.
#[test]
fn test_parse_struct_to_schema_with_default_fields() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r#"
            struct Config {
                required_field: i32,
                #[serde(default)]
                with_default: String,
                #[serde(skip_serializing_if = "Option::is_none")]
                maybe_skip: Option<i32>,
            }
        "#,
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let props = schema.properties.as_ref().unwrap();
    assert!(props.contains_key("required_field"));
    assert!(props.contains_key("with_default"));
    assert!(props.contains_key("maybe_skip"));

    let required = schema.required.as_ref().unwrap();
    assert!(required.contains(&"required_field".to_string()));
    // Non-nullable fields are always required, even with #[serde(default)]
    assert!(required.contains(&"with_default".to_string()));
    // Option<T> fields are not required (nullable)
    assert!(!required.contains(&"maybe_skip".to_string()));
}

// Tests for struct with doc comments
#[test]
fn test_parse_struct_to_schema_with_description() {
    let struct_src = r"
            /// User struct description
            struct User {
                /// User ID
                id: i32,
                /// User name
                name: String,
            }
        ";
    let struct_item: syn::ItemStruct = syn::parse_str(struct_src).unwrap();
    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    assert_eq!(
        schema.description,
        Some("User struct description".to_string())
    );
    // Check field descriptions
    let props = schema.properties.unwrap();
    if let SchemaRef::Inline(id_schema) = props.get("id").unwrap() {
        assert_eq!(id_schema.description, Some("User ID".to_string()));
    }
    if let SchemaRef::Inline(name_schema) = props.get("name").unwrap() {
        assert_eq!(name_schema.description, Some("User name".to_string()));
    }
}

#[test]
fn test_parse_struct_to_schema_field_with_ref_and_description() {
    let struct_src = r"
            struct Container {
                /// The user reference
                user: User,
            }
        ";
    let struct_item: syn::ItemStruct = syn::parse_str(struct_src).unwrap();
    let mut struct_defs = HashMap::new();
    struct_defs.insert("User".to_string(), "struct User { id: i32 }".to_string());
    let mut known = HashSet::new();
    known.insert("User".to_string());
    let schema = parse_struct_to_schema(&struct_item, &known, &struct_defs);
    let props = schema.properties.unwrap();
    // Field with $ref and description should use allOf
    if let SchemaRef::Inline(user_schema) = props.get("user").unwrap() {
        assert_eq!(
            user_schema.description,
            Some("The user reference".to_string())
        );
        assert!(user_schema.all_of.is_some());
    }
}

#[test]
fn test_parse_struct_to_schema_description_strips_slash_prefix() {
    // When doc attributes have "/ " prefix (without leading space), descriptions should be clean.
    // This can happen in certain TokenStream roundtrip scenarios.
    let struct_item: syn::ItemStruct = syn::parse_str(
        r#"
            #[doc = "/ Struct description"]
            struct Admin {
                #[doc = "/ Field description"]
                id: i32,
            }
        "#,
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    assert_eq!(schema.description, Some("Struct description".to_string()));
    let props = schema.properties.unwrap();
    if let SchemaRef::Inline(id_schema) = props.get("id").unwrap() {
        assert_eq!(id_schema.description, Some("Field description".to_string()));
    }
}

#[test]
fn test_parse_struct_to_schema_with_flatten() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r"
            struct UserListRequest {
                filter: String,
                #[serde(flatten)]
                pagination: Pagination,
            }
        ",
    )
    .unwrap();

    let mut struct_defs = HashMap::new();
    struct_defs.insert(
        "Pagination".to_string(),
        "struct Pagination { page: i32 }".to_string(),
    );
    let mut known = HashSet::new();
    known.insert("Pagination".to_string());

    let schema = parse_struct_to_schema(&struct_item, &known, &struct_defs);

    // Should have allOf
    assert!(
        schema.all_of.is_some(),
        "Schema should have allOf for flatten"
    );
    let all_of = schema.all_of.as_ref().unwrap();
    assert_eq!(all_of.len(), 2, "allOf should have 2 elements");

    // First element should be the object with non-flattened properties
    if let SchemaRef::Inline(obj_schema) = &all_of[0] {
        let props = obj_schema.properties.as_ref().unwrap();
        assert!(props.contains_key("filter"), "Should have filter property");
        assert!(
            !props.contains_key("pagination"),
            "Should NOT have pagination property"
        );
    } else {
        panic!("First allOf element should be inline schema");
    }

    // Second element should be $ref to Pagination
    if let SchemaRef::Ref(reference) = &all_of[1] {
        assert_eq!(reference.ref_path, "#/components/schemas/Pagination");
    } else {
        panic!("Second allOf element should be $ref");
    }
}

#[test]
fn test_parse_struct_to_schema_with_multiple_flatten() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r"
            struct Combined {
                name: String,
                #[serde(flatten)]
                pagination: Pagination,
                #[serde(flatten)]
                metadata: Metadata,
            }
        ",
    )
    .unwrap();

    let mut struct_defs = HashMap::new();
    struct_defs.insert("Pagination".to_string(), "struct Pagination {}".to_string());
    struct_defs.insert("Metadata".to_string(), "struct Metadata {}".to_string());
    let mut known = HashSet::new();
    known.insert("Pagination".to_string());
    known.insert("Metadata".to_string());

    let schema = parse_struct_to_schema(&struct_item, &known, &struct_defs);

    assert!(schema.all_of.is_some());
    let all_of = schema.all_of.as_ref().unwrap();
    assert_eq!(
        all_of.len(),
        3,
        "allOf should have 3 elements (1 inline + 2 refs)"
    );
}

#[test]
fn flatten_only_schema_omits_empty_properties_and_required() {
    let struct_item: syn::ItemStruct =
        syn::parse_str(r"struct Combined { #[serde(flatten)] pagination: Pagination }").unwrap();
    let known = HashSet::from(["Pagination".to_string()]);
    let schema = parse_struct_to_schema(&struct_item, &known, &HashMap::<String, String>::new());

    let all_of = schema.all_of.unwrap();
    assert_eq!(all_of.len(), 2);
    let SchemaRef::Inline(inline_schema) = &all_of[0] else {
        panic!("first allOf element should be inline");
    };
    assert_eq!(inline_schema.schema_type, Some(SchemaType::Object));
    assert!(inline_schema.properties.is_none());
    assert!(inline_schema.required.is_none());
}

#[test]
fn test_parse_struct_to_schema_no_flatten() {
    // Existing struct without flatten should NOT use allOf
    let struct_item: syn::ItemStruct = syn::parse_str(
        r"
            struct Simple {
                name: String,
                age: i32,
            }
        ",
    )
    .unwrap();

    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    assert!(
        schema.all_of.is_none(),
        "Simple struct should not have allOf"
    );
    assert!(schema.properties.is_some());
}

#[test]
fn test_parse_struct_to_schema_transparent_tuple_wrapper_uses_ref_schema() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r"
            #[serde(transparent)]
            struct Wrapper(User);
        ",
    )
    .unwrap();

    let mut struct_defs = HashMap::new();
    struct_defs.insert("User".to_string(), "struct User { id: i32 }".to_string());
    let mut known = HashSet::new();
    known.insert("User".to_string());

    let schema = parse_struct_to_schema(&struct_item, &known, &struct_defs);
    assert!(schema.all_of.is_some());
    let all_of = schema.all_of.unwrap();
    assert_eq!(all_of.len(), 1);
    match &all_of[0] {
        SchemaRef::Ref(reference) => {
            assert_eq!(reference.ref_path, "#/components/schemas/User");
        }
        SchemaRef::Inline(_) => {
            panic!("expected $ref wrapper for transparent tuple known schema")
        }
    }
}

#[test]
fn test_parse_struct_to_schema_transparent_multi_field_tuple_falls_back() {
    let struct_item: syn::ItemStruct = syn::parse_str(
        r"
            #[serde(transparent)]
            struct Wrapper(String, String);
        ",
    )
    .unwrap();

    let schema = parse_struct_to_schema(
        &struct_item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    assert_eq!(schema.schema_type, Some(SchemaType::Object));
    assert!(schema.properties.is_none());
    assert!(schema.all_of.is_none());
}

// ── field-level `#[schema(...)]` constraint propagation ─────────

fn field_schema<'a>(schema: &'a Schema, field: &str) -> &'a Schema {
    let props = schema.properties.as_ref().expect("properties missing");
    let entry = props.get(field).expect("field missing");
    match entry {
        SchemaRef::Inline(boxed) => boxed.as_ref(),
        SchemaRef::Ref(_) => panic!("expected inline schema for field '{field}'"),
    }
}

#[test]
fn schema_constraints_min_max_length_and_pattern_on_string_field() {
    let s: syn::ItemStruct = syn::parse_str(
        r#"
            struct CreateUser {
                #[schema(min_length = 3, max_length = 32, pattern = "^[a-z]+$")]
                username: String,
            }
            "#,
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &s,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let field = field_schema(&schema, "username");
    assert_eq!(field.min_length, Some(3));
    assert_eq!(field.max_length, Some(32));
    assert_eq!(field.pattern.as_deref(), Some("^[a-z]+$"));
}

#[test]
fn schema_constraints_minimum_maximum_on_numeric_field() {
    let s: syn::ItemStruct = syn::parse_str(
        r"
            struct Profile {
                #[schema(minimum = 0, maximum = 150)]
                age: u32,
            }
            ",
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &s,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let field = field_schema(&schema, "age");
    assert_eq!(field.minimum, Some(0.0));
    assert_eq!(field.maximum, Some(150.0));
}

#[test]
fn schema_constraints_format_email_on_string_field() {
    let s: syn::ItemStruct = syn::parse_str(
        r#"
            struct Contact {
                #[schema(format = "email")]
                email: String,
            }
            "#,
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &s,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let field = field_schema(&schema, "email");
    assert_eq!(field.format.as_deref(), Some("email"));
}

#[test]
fn schema_constraints_read_only_write_only_example() {
    let s: syn::ItemStruct = syn::parse_str(
        r#"
            struct User {
                #[schema(read_only, example = "abc-123")]
                id: String,
                #[schema(write_only)]
                password: String,
            }
            "#,
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &s,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let id_field = field_schema(&schema, "id");
    assert_eq!(id_field.read_only, Some(true));
    assert_eq!(id_field.example, Some(serde_json::json!("abc-123")));
    let pw_field = field_schema(&schema, "password");
    assert_eq!(pw_field.write_only, Some(true));
}

#[test]
fn schema_constraints_min_max_items_unique_on_vec_field() {
    let s: syn::ItemStruct = syn::parse_str(
        r"
            struct Post {
                #[schema(min_items = 1, max_items = 5, unique_items)]
                tags: Vec<String>,
            }
            ",
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &s,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let field = field_schema(&schema, "tags");
    assert_eq!(field.min_items, Some(1));
    assert_eq!(field.max_items, Some(5));
    assert_eq!(field.unique_items, Some(true));
}

#[test]
fn schema_constraints_exclusive_bounds_and_multiple_of() {
    let s: syn::ItemStruct = syn::parse_str(
        r"
            struct Price {
                #[schema(minimum = 0, exclusive_minimum, multiple_of = 0.01)]
                amount: f64,
            }
            ",
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &s,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let field = field_schema(&schema, "amount");
    assert_eq!(field.minimum, Some(0.0));
    assert_eq!(field.exclusive_minimum, Some(0.0));
    assert_eq!(field.multiple_of, Some(0.01));
}

#[test]
fn schema_constraints_on_ref_field_promote_to_allof_wrapper() {
    // A field referencing a known component schema must keep its
    // `$ref` but gain the constraints via an `allOf` wrapper so the
    // OpenAPI consumer still sees the reference.
    let mut known = HashSet::new();
    known.insert("Address".to_string());
    let s: syn::ItemStruct = syn::parse_str(
        r"
            struct Order {
                #[schema(read_only)]
                shipping: Address,
            }
            ",
    )
    .unwrap();
    let schema = parse_struct_to_schema(&s, &known, &HashMap::<String, String>::new());
    let field = field_schema(&schema, "shipping");
    assert_eq!(field.read_only, Some(true));
    let all_of = field.all_of.as_ref().expect("allOf wrap missing");
    assert_eq!(all_of.len(), 1);
    assert!(matches!(all_of[0], SchemaRef::Ref(_)));
}

#[test]
fn schema_constraints_coexist_with_doc_comment_on_ref_field() {
    // When BOTH a doc comment AND constraints are present on a
    // `$ref` field, the doc comment converts it to allOf first, then
    // constraints are layered onto the same wrapper.
    let mut known = HashSet::new();
    known.insert("Address".to_string());
    let s: syn::ItemStruct = syn::parse_str(
        r"
            struct Order {
                /// Shipping address — must be present.
                #[schema(read_only, write_only = false)]
                shipping: Address,
            }
            ",
    )
    .unwrap();
    let schema = parse_struct_to_schema(&s, &known, &HashMap::<String, String>::new());
    let field = field_schema(&schema, "shipping");
    assert!(field.description.is_some(), "doc comment lost");
    assert_eq!(field.read_only, Some(true));
    assert_eq!(field.write_only, Some(false));
    assert!(field.all_of.is_some(), "allOf wrap lost");
}

#[test]
fn schema_constraints_unknown_keys_on_field_are_silently_ignored() {
    // Struct-level keys (e.g. `name`) accidentally placed on a field
    // attribute should not trip the parser nor produce constraints.
    let s: syn::ItemStruct = syn::parse_str(
        r#"
            struct Account {
                #[schema(name = "Stray", min_length = 4)]
                pin: String,
            }
            "#,
    )
    .unwrap();
    let schema = parse_struct_to_schema(
        &s,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let field = field_schema(&schema, "pin");
    assert_eq!(field.min_length, Some(4));
}

#[test]
fn schema_exclusive_maximum_and_minimum_land_on_emitted_field_schema() {
    // `exclusive_minimum` / `exclusive_maximum` / `multiple_of` /
    // `unique_items` are OpenAPI-only annotations (no garde rule
    // counterpart).  The struct-schema parser still propagates them
    // onto the per-field `Schema` so the resulting `openapi.json`
    // carries them verbatim.
    let s: syn::ItemStruct = syn::parse_str(
            r"
            struct Price {
                #[schema(minimum = 0, maximum = 100, exclusive_minimum, exclusive_maximum, multiple_of = 0.5)]
                amount: f64,

                #[schema(min_items = 1, max_items = 5, unique_items)]
                tags: Vec<String>,
            }
            ",
        )
        .unwrap();
    let schema = parse_struct_to_schema(
        &s,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let amount = field_schema(&schema, "amount");
    assert_eq!(amount.exclusive_minimum, Some(0.0));
    assert_eq!(amount.exclusive_maximum, Some(100.0));
    assert_eq!(amount.multiple_of, Some(0.5));
    let tags = field_schema(&schema, "tags");
    assert_eq!(tags.unique_items, Some(true));
}

#[test]
fn malformed_named_field_uses_unknown_property_name() {
    let mut item: syn::ItemStruct = syn::parse_quote! { struct Broken { value: String } };
    let syn::Fields::Named(fields) = &mut item.fields else {
        panic!("named fields");
    };
    fields.named.first_mut().expect("field").ident = None;

    let schema = parse_struct_to_schema(
        &item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    let properties = schema.properties.expect("properties");
    assert!(properties.contains_key("unknown"));
    assert_eq!(
        schema.required.as_deref(),
        Some(["unknown".to_string()].as_slice())
    );
}

#[test]
fn schema_any_field_forces_object_without_type_conversion() {
    let item: syn::ItemStruct = syn::parse_quote! {
        struct Opaque { #[schema(any)] payload: CustomType }
    };
    let schema = parse_struct_to_schema(
        &item,
        &HashSet::<String>::new(),
        &HashMap::<String, String>::new(),
    );
    assert_eq!(
        field_schema(&schema, "payload").schema_type,
        Some(SchemaType::Object)
    );
}