klaviyo_generated 0.1.0

Generated Klaviyo REST API client (used by klaviyo)
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
//! Generates the Klaviyo types and operation markers from `openapi.json`.
//!
//! The spec is normalised first so progenitor can generate it: unused
//! operations are dropped, media types and error responses are unified, and
//! request objects are made `Default` where their fields allow it.

use std::collections::BTreeSet;
use std::env;
use std::fs;
use std::path::PathBuf;

use heck::{ToPascalCase, ToSnakeCase};
use openapiv3::OpenAPI;
use serde_json::{json, Map, Value};

/// Operations to generate, by `operationId`. An ID missing from the spec
/// fails the build.
const OPERATIONS: &[&str] = &[
    // Accounts
    "get_accounts",
    "get_account",
    // Campaigns
    "get_campaigns",
    "get_campaign",
    "create_campaign",
    "update_campaign",
    "delete_campaign",
    "create_campaign_clone",
    "get_messages_for_campaign",
    "get_campaign_message",
    "update_campaign_message",
    "assign_template_to_campaign_message",
    "get_template_for_campaign_message",
    "send_campaign",
    "get_campaign_send_job",
    "cancel_campaign_send",
    "refresh_campaign_recipient_estimation",
    "get_campaign_recipient_estimation_job",
    "get_campaign_recipient_estimation",
    // Events
    "get_events",
    "get_event",
    "create_event",
    "bulk_create_events",
    // Flows
    "get_flows",
    "get_flow",
    "update_flow",
    // Lists
    "get_lists",
    "get_list",
    "create_list",
    "update_list",
    "delete_list",
    "get_profiles_for_list",
    "add_profiles_to_list",
    "remove_profiles_from_list",
    // Metrics
    "get_metrics",
    "get_metric",
    "query_metric_aggregates",
    // Profiles
    "get_profiles",
    "get_profile",
    "create_profile",
    "update_profile",
    "create_or_update_profile",
    "bulk_import_profiles",
    "get_bulk_import_profiles_job",
    "get_errors_for_bulk_import_profiles_job",
    "bulk_subscribe_profiles",
    "bulk_unsubscribe_profiles",
    "bulk_suppress_profiles",
    "bulk_unsuppress_profiles",
    // Reporting
    "query_campaign_values",
    "query_flow_values",
    "query_flow_series",
    // Segments
    "get_segments",
    "get_segment",
    "get_profiles_for_segment",
    // Templates
    "get_templates",
    "get_template",
    "create_template",
    "update_template",
    "delete_template",
    "render_template",
    "clone_template",
    "get_all_universal_content",
    "get_universal_content",
    "create_universal_content",
    "update_universal_content",
    "delete_universal_content",
];

/// The keys of an OpenAPI path item that hold an operation.
const HTTP_METHODS: &[&str] = &[
    "get", "put", "post", "delete", "options", "head", "patch", "trace",
];

/// The prefix of a `$ref` pointing at a component schema.
const SCHEMA_REF_PREFIX: &str = "#/components/schemas/";

/// The component every 4XX and 5XX response is pointed at.
const ERROR_SCHEMA: &str = "ErrorDocument";

type BuildResult<T> = Result<T, Box<dyn std::error::Error>>;

fn main() -> BuildResult<()> {
    let spec_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("openapi.json");
    let mut spec: Value = serde_json::from_str(&fs::read_to_string(&spec_path)?)?;

    keep_operations(&mut spec)?;
    let operations = operation_markers(&spec)?;
    rewrite_media_types(&mut spec);
    name_error_schema(&mut spec)?;
    prune_schemas(&mut spec);
    default_single_value_enums(&mut spec);
    let request_schemas = hoist_request_objects(&mut spec);
    strip_optional_defaults(&mut spec, &request_schemas);
    let default_types = defaultable_with_required_fields(&spec, &request_schemas);

    let revision = spec["info"]["version"]
        .as_str()
        .ok_or("spec has no `info.version`")?
        .to_owned();
    let spec: OpenAPI = serde_json::from_value(spec)?;

    // typify may PascalCase a schema name and ignores patches for names it
    // doesn't use, so register both spellings.
    let mut default_patch = progenitor::TypePatch::default();
    default_patch.with_derive("Default");
    let mut settings = progenitor::GenerationSettings::default();
    for name in &default_types {
        settings.with_patch(name, &default_patch);
        let pascal = name.to_pascal_case();
        if &pascal != name {
            settings.with_patch(pascal, &default_patch);
        }
    }

    let mut generator = progenitor::Generator::new(&settings);
    let tokens = generator.generate_tokens(&spec)?;

    let out_dir = PathBuf::from(env::var("OUT_DIR")?);
    fs::write(
        out_dir.join("generated.rs"),
        format!(
            "{tokens}\n\
             /// The spec's API revision.\n\
             pub const API_REVISION: &str = {revision:?};\n"
        ),
    )?;
    fs::write(out_dir.join("operations.rs"), operations)?;

    println!("cargo:rerun-if-changed=openapi.json");
    println!("cargo:rerun-if-changed=build.rs");

    Ok(())
}

/// Drops every operation not in [`OPERATIONS`], then every path left empty.
fn keep_operations(spec: &mut Value) -> BuildResult<()> {
    let wanted: BTreeSet<&str> = OPERATIONS.iter().copied().collect();
    let mut found = BTreeSet::new();

    let paths = spec
        .get_mut("paths")
        .and_then(Value::as_object_mut)
        .ok_or("spec has no `paths` object")?;
    for item in paths.values_mut() {
        let Some(item) = item.as_object_mut() else {
            continue;
        };
        item.retain(|method, operation| {
            if !HTTP_METHODS.contains(&method.as_str()) {
                return true;
            }
            match operation.get("operationId").and_then(Value::as_str) {
                Some(id) if wanted.contains(id) => {
                    found.insert(id.to_owned());
                    true
                }
                _ => false,
            }
        });
    }
    paths.retain(|_, item| {
        item.as_object()
            .is_some_and(|item| item.keys().any(|k| HTTP_METHODS.contains(&k.as_str())))
    });

    let missing: Vec<_> = wanted.iter().filter(|id| !found.contains(**id)).collect();
    if !missing.is_empty() {
        return Err(format!("operations not found in openapi.json: {missing:?}").into());
    }
    Ok(())
}

/// One marker type per operation, with its method, path, and accepted query
/// options. The traits are in `src/lib.rs`.
fn operation_markers(spec: &Value) -> BuildResult<String> {
    let paths = spec
        .get("paths")
        .and_then(Value::as_object)
        .ok_or("spec has no `paths` object")?;

    let mut source = String::new();
    for (path, item) in paths {
        let Some(item) = item.as_object() else {
            continue;
        };
        let shared = item.get("parameters").and_then(Value::as_array);
        for (method, operation) in item
            .iter()
            .filter(|(method, _)| HTTP_METHODS.contains(&method.as_str()))
        {
            let id = operation["operationId"]
                .as_str()
                .ok_or_else(|| format!("{method} {path} has no operationId"))?;
            let parameters: Vec<&Value> = shared
                .into_iter()
                .flatten()
                .chain(operation["parameters"].as_array().into_iter().flatten())
                .collect();
            source.push_str(&operation_marker(id, method, path, &parameters));
        }
    }
    Ok(source)
}

fn operation_marker(id: &str, method: &str, path: &str, parameters: &[&Value]) -> String {
    let name = id.to_pascal_case();
    let http_method = method.to_uppercase();

    let mut arguments = Vec::new();
    let mut segments = Vec::new();
    for segment in path.split('/').filter(|segment| !segment.is_empty()) {
        match segment
            .strip_prefix('{')
            .and_then(|segment| segment.strip_suffix('}'))
        {
            Some(parameter) => {
                let argument = rust_identifier(&parameter.to_snake_case());
                segments.push(argument.clone());
                arguments.push(argument);
            }
            None => segments.push(format!("{segment:?}")),
        }
    }
    let (generics, signature, lifetime) = if arguments.is_empty() {
        ("", String::new(), "'static")
    } else {
        let signature = arguments
            .iter()
            .map(|argument| format!("{argument}: &'p str"))
            .collect::<Vec<_>>()
            .join(", ");
        ("<'p>", signature, "'p")
    };

    let mut source = format!(
        "/// `{http_method} {path}`\n\
         #[derive(Debug)]\n\
         pub enum {name} {{}}\n\
         impl {name} {{\n\
         /// The path with its parameters filled in.\n\
         pub fn path{generics}({signature}) -> Path<{lifetime}, Self> {{\n\
         Path::new(::std::vec![{segments}])\n\
         }}\n\
         }}\n\
         impl Operation for {name} {{\n\
         const ID: &'static str = {id:?};\n\
         const METHOD: ::reqwest::Method = ::reqwest::Method::{http_method};\n\
         }}\n",
        segments = segments.join(", "),
    );

    let query: BTreeSet<&str> = parameters
        .iter()
        .filter(|parameter| parameter["in"] == "query")
        .filter_map(|parameter| parameter["name"].as_str())
        .collect();
    let mut capability = |accepted: bool, trait_name: &str| {
        if accepted {
            source.push_str(&format!("impl {trait_name} for {name} {{}}\n"));
        }
    };
    capability(query.iter().any(|q| q.starts_with("fields[")), "Fields");
    capability(
        query.iter().any(|q| q.starts_with("additional-fields[")),
        "AdditionalFields",
    );
    capability(query.contains("include"), "Include");
    capability(query.contains("filter"), "Filter");
    capability(query.contains("sort"), "Sort");
    capability(query.contains("page[size]"), "PageSize");
    if let Some(cursor) = ["page[cursor]", "page_cursor"]
        .into_iter()
        .find(|cursor| query.contains(cursor))
    {
        source.push_str(&format!(
            "impl Paginated for {name} {{ const CURSOR_PARAM: &'static str = {cursor:?}; }}\n"
        ));
    }
    source
}

fn rust_identifier(name: &str) -> String {
    const KEYWORDS: &[&str] = &[
        "as", "async", "await", "box", "break", "const", "continue", "crate", "dyn", "else",
        "enum", "extern", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move",
        "mut", "pub", "ref", "return", "static", "struct", "trait", "type", "unsafe", "use",
        "where", "while", "yield",
    ];
    if KEYWORDS.contains(&name) {
        format!("r#{name}")
    } else {
        name.to_owned()
    }
}

/// progenitor doesn't treat `application/vnd.api+json` as JSON.
fn rewrite_media_types(value: &mut Value) {
    match value {
        Value::Object(map) => {
            if let Some(Value::Object(content)) = map.get_mut("content") {
                if let Some(media) = content.remove("application/vnd.api+json") {
                    content.insert("application/json".to_owned(), media);
                }
            }
            map.values_mut().for_each(rewrite_media_types);
        }
        Value::Array(items) => items.iter_mut().for_each(rewrite_media_types),
        _ => {}
    }
}

/// progenitor names an inline error schema `<OperationId>Response`, which
/// collides with success schemas such as `GetAccountResponse`.
fn name_error_schema(spec: &mut Value) -> BuildResult<()> {
    if spec["components"]["schemas"].get(ERROR_SCHEMA).is_some() {
        return Err(format!("openapi.json already defines `{ERROR_SCHEMA}`").into());
    }

    let mut error_schema = None;
    if let Some(responses) = spec["components"]["responses"].as_object_mut() {
        for response in responses.values_mut() {
            let Some(content) = response.get_mut("content").and_then(Value::as_object_mut) else {
                continue;
            };
            for media in content.values_mut() {
                if let Some(schema) = media.get_mut("schema") {
                    if schema.get("$ref").is_none() {
                        let inline = std::mem::replace(
                            schema,
                            json!({ "$ref": format!("{SCHEMA_REF_PREFIX}{ERROR_SCHEMA}") }),
                        );
                        error_schema.get_or_insert(inline);
                    }
                }
            }
        }
    }

    let error_schema = error_schema.ok_or("no inline error response schema found")?;
    spec["components"]["schemas"]
        .as_object_mut()
        .ok_or("spec has no `components/schemas` object")?
        .insert(ERROR_SCHEMA.to_owned(), error_schema);
    Ok(())
}

/// Otherwise progenitor generates all ~1300 Klaviyo schemas.
fn prune_schemas(spec: &mut Value) {
    let mut reachable = BTreeSet::new();
    let mut pending = Vec::new();
    collect_schema_refs(&spec["paths"], &mut pending);
    collect_schema_refs(&spec["components"]["responses"], &mut pending);

    while let Some(name) = pending.pop() {
        if !reachable.insert(name.clone()) {
            continue;
        }
        collect_schema_refs(&spec["components"]["schemas"][&name], &mut pending);
    }

    if let Some(schemas) = spec["components"]["schemas"].as_object_mut() {
        schemas.retain(|name, _| reachable.contains(name));
    }
}

fn collect_schema_refs(value: &Value, out: &mut Vec<String>) {
    match value {
        Value::Object(map) => {
            if let Some(name) = schema_ref(value) {
                out.push(name.to_owned());
            }
            map.values()
                .for_each(|child| collect_schema_refs(child, out));
        }
        Value::Array(items) => items
            .iter()
            .for_each(|child| collect_schema_refs(child, out)),
        _ => {}
    }
}

fn schema_ref(value: &Value) -> Option<&str> {
    value
        .get("$ref")
        .and_then(Value::as_str)
        .and_then(|r| r.strip_prefix(SCHEMA_REF_PREFIX))
}

/// A `default` on single-value enums such as JSON:API `type` (`"campaign"`)
/// makes typify implement `Default` for them. Optional properties are
/// skipped: typify always serializes a property that has a `default`.
fn default_single_value_enums(spec: &mut Value) {
    if let Some(schemas) = spec["components"]["schemas"].as_object_mut() {
        for schema in schemas.values_mut() {
            add_enum_default(schema);
            default_required_inline_enums(schema);
        }
    }
    default_required_inline_enums(&mut spec["paths"]);
}

fn add_enum_default(schema: &mut Value) {
    let Some(map) = schema.as_object_mut() else {
        return;
    };
    if map.contains_key("default") {
        return;
    }
    let single = match map.get("enum").and_then(Value::as_array) {
        Some(values) if values.len() == 1 => values[0].clone(),
        _ => return,
    };
    map.insert("default".to_owned(), single);
}

fn default_required_inline_enums(value: &mut Value) {
    match value {
        Value::Object(map) => {
            let required = required_names(map);
            if let Some(Value::Object(properties)) = map.get_mut("properties") {
                for (name, property) in properties.iter_mut() {
                    if required.contains(name) {
                        add_enum_default(property);
                    }
                }
            }
            map.values_mut().for_each(default_required_inline_enums);
        }
        Value::Array(items) => items.iter_mut().for_each(default_required_inline_enums),
        _ => {}
    }
}

fn required_names(schema: &Map<String, Value>) -> BTreeSet<String> {
    match schema.get("required") {
        Some(Value::Array(names)) => names
            .iter()
            .filter_map(Value::as_str)
            .map(str::to_owned)
            .collect(),
        _ => BTreeSet::new(),
    }
}

/// Names the inline objects reachable from request bodies the way typify
/// would, and moves them into components so they can be patched. Returns
/// every schema reachable from a request body. Nullable objects stay inline,
/// since a `$ref` loses `nullable`.
fn hoist_request_objects(spec: &mut Value) -> BTreeSet<String> {
    let mut pending = Vec::new();
    if let Some(paths) = spec["paths"].as_object() {
        for operation in paths
            .values()
            .filter_map(Value::as_object)
            .flat_map(|item| {
                item.iter()
                    .filter(|(method, _)| HTTP_METHODS.contains(&method.as_str()))
                    .map(|(_, operation)| operation)
            })
        {
            if let Some(content) = operation["requestBody"]["content"].as_object() {
                for media in content.values() {
                    if let Some(name) = schema_ref(&media["schema"]) {
                        pending.push(name.to_owned());
                    }
                }
            }
        }
    }

    let mut reachable = BTreeSet::new();
    let Some(schemas) = spec["components"]["schemas"].as_object_mut() else {
        return reachable;
    };
    while let Some(name) = pending.pop() {
        if reachable.contains(&name) {
            continue;
        }
        let Some(mut schema) = schemas.remove(&name) else {
            continue;
        };
        reachable.insert(name.clone());

        let mut hoisted = Vec::new();
        hoist_properties(&mut schema, &name, schemas, &mut hoisted);
        collect_schema_refs(&schema, &mut pending);
        schemas.insert(name, schema);
        for (hoisted_name, hoisted_schema) in hoisted {
            schemas.insert(hoisted_name.clone(), hoisted_schema);
            pending.push(hoisted_name);
        }
    }
    reachable
}

fn hoist_properties(
    schema: &mut Value,
    parent: &str,
    taken: &Map<String, Value>,
    hoisted: &mut Vec<(String, Value)>,
) {
    let Some(Value::Object(properties)) = schema.get_mut("properties") else {
        return;
    };
    for (property, child) in properties.iter_mut() {
        hoist_value(
            child,
            format!("{parent}{}", property.to_pascal_case()),
            taken,
            hoisted,
        );
    }
}

fn hoist_value(
    slot: &mut Value,
    name: String,
    taken: &Map<String, Value>,
    hoisted: &mut Vec<(String, Value)>,
) {
    if is_inline_object(slot) && slot.get("nullable") != Some(&Value::Bool(true)) {
        let name = unique_name(name, taken, hoisted);
        let schema = std::mem::replace(
            slot,
            json!({ "$ref": format!("{SCHEMA_REF_PREFIX}{name}") }),
        );
        hoisted.push((name, schema));
    } else if slot.get("type").and_then(Value::as_str) == Some("array") {
        if let Some(items) = slot.get_mut("items") {
            hoist_value(items, format!("{name}Item"), taken, hoisted);
        }
    }
}

fn is_inline_object(schema: &Value) -> bool {
    schema.get("$ref").is_none()
        && schema.get("type").and_then(Value::as_str) == Some("object")
        && schema.get("properties").is_some()
}

fn unique_name(name: String, taken: &Map<String, Value>, hoisted: &[(String, Value)]) -> String {
    let is_free = |candidate: &str| {
        !taken.contains_key(candidate) && hoisted.iter().all(|(n, _)| n != candidate)
    };
    if is_free(&name) {
        return name;
    }
    let mut n = 2;
    loop {
        let candidate = format!("{name}{n}");
        if is_free(&candidate) {
            return candidate;
        }
        n += 1;
    }
}

/// typify always serializes an optional property that has a `default`, so a
/// PATCH built with `..Default::default()` would overwrite settings such as
/// `use_smart_sending`.
fn strip_optional_defaults(spec: &mut Value, request: &BTreeSet<String>) {
    let Some(schemas) = spec["components"]["schemas"].as_object_mut() else {
        return;
    };
    for (name, schema) in schemas.iter_mut() {
        if request.contains(name) {
            strip_defaults_of_optional_properties(schema);
        }
    }
}

fn strip_defaults_of_optional_properties(value: &mut Value) {
    match value {
        Value::Object(map) => {
            let required = required_names(map);
            if let Some(Value::Object(properties)) = map.get_mut("properties") {
                for (name, property) in properties.iter_mut() {
                    if !required.contains(name) {
                        if let Some(property) = property.as_object_mut() {
                            property.remove("default");
                        }
                    }
                }
            }
            map.values_mut()
                .for_each(strip_defaults_of_optional_properties);
        }
        Value::Array(items) => items
            .iter_mut()
            .for_each(strip_defaults_of_optional_properties),
        _ => {}
    }
}

/// Request-side objects to patch with `Default`: those with a required field,
/// all of whose required fields implement `Default`. typify implements it
/// itself only for objects without required fields.
fn defaultable_with_required_fields(spec: &Value, request: &BTreeSet<String>) -> Vec<String> {
    let schemas = &spec["components"]["schemas"];
    let mut defaultable: BTreeSet<&str> = request
        .iter()
        .map(String::as_str)
        .filter(|name| is_object_schema(&schemas[*name]))
        .collect();

    // Start from every candidate and drop the ones that fail until nothing
    // changes, so schemas that refer to themselves are kept rather than
    // rejected.
    loop {
        let snapshot = defaultable.clone();
        defaultable.retain(|name| required_fields_default(&schemas[*name], schemas, &snapshot));
        if defaultable.len() == snapshot.len() {
            break;
        }
    }

    defaultable
        .into_iter()
        .filter(|name| has_required_field(&schemas[*name]))
        .map(str::to_owned)
        .collect()
}

fn is_object_schema(schema: &Value) -> bool {
    schema.get("properties").is_some()
        && schema.get("oneOf").is_none()
        && schema.get("anyOf").is_none()
        && schema.get("allOf").is_none()
}

fn required_fields_default(schema: &Value, schemas: &Value, defaultable: &BTreeSet<&str>) -> bool {
    let Some(map) = schema.as_object() else {
        return false;
    };
    required_names(map)
        .iter()
        .all(|name| field_has_default(&schema["properties"][name], schemas, defaultable))
}

fn has_required_field(schema: &Value) -> bool {
    schema
        .as_object()
        .is_some_and(|map| !required_names(map).is_empty())
}

/// Whether typify's type for `field` implements `Default`. Errs towards
/// `false`: a wrong `true` fails the build.
fn field_has_default(field: &Value, schemas: &Value, defaultable: &BTreeSet<&str>) -> bool {
    if field.is_null() {
        return false;
    }
    // openapiv3 drops the siblings of a `$ref`, `nullable` included, so the
    // field is the bare type, not an `Option`.
    if let Some(name) = schema_ref(field) {
        let target = &schemas[name];
        return defaultable.contains(name)
            || (!is_object_schema(target) && field_has_default(target, schemas, defaultable));
    }
    if field.get("nullable") == Some(&Value::Bool(true)) {
        return true;
    }
    if ["oneOf", "anyOf", "allOf"]
        .iter()
        .any(|k| field.get(*k).is_some())
    {
        return false;
    }
    let ty = field.get("type").and_then(Value::as_str);
    if field.get("enum").is_some() {
        // typify implements `Default` for a string enum with a `default`.
        return matches!(ty, Some("string") | None) && field.get("default").is_some();
    }
    match ty {
        // typify wraps constrained strings in newtypes without `Default`.
        Some("string") => ["pattern", "minLength", "maxLength"]
            .iter()
            .all(|k| field.get(*k).is_none()),
        // typify maps integers that can't be zero to `NonZero*`.
        Some("integer") => !cannot_be_zero(field),
        Some("number" | "boolean" | "array") => true,
        Some("object") => field.get("properties").is_none(),
        Some(_) => false,
        None => true,
    }
}

fn cannot_be_zero(field: &Value) -> bool {
    let exclusive = field.get("exclusiveMinimum") == Some(&Value::Bool(true));
    field
        .get("minimum")
        .and_then(Value::as_f64)
        .is_some_and(|minimum| minimum > 0.0 || (exclusive && minimum >= 0.0))
}